Vim
Vim
Vim Command Guide
This guide covers the most useful Vim commands for real day-to-day work.
1. Vim Modes
| Mode | What it does | Enter | Exit |
|---|---|---|---|
| Normal | Navigate, edit, run commands | Esc | N/A |
| Insert | Type text | i, a, o, I, A, O | Esc |
| Visual | Select text | v, V, Ctrl-v | Esc |
| Command-line | Run : commands, search | :, /, ? | Enter or Esc |
2. Open, Save, Quit
| Command | Action |
|---|---|
vim file.txt | Open file |
:e file.txt | Open another file |
:w | Save |
:w newname.txt | Save as |
:q | Quit |
:q! | Quit without saving |
:wq | Save and quit |
:x | Save and quit only if changed |
ZZ | Save and quit (Normal mode) |
:wa | Save all open files |
:qa | Quit all |
:qa! | Force quit all |
3. Movement (Core Productivity)
Basic movement
| Command | Action |
|---|---|
h j k l | Left, down, up, right |
w / W | Next word start |
b / B | Previous word start |
e / E | End of word |
0 | Start of line |
^ | First non-blank in line |
$ | End of line |
gg | Start of file |
G | End of file |
42G | Go to line 42 |
% | Jump to matching ()[]{} |
Fast character and line jumps
| Command | Action |
|---|---|
fX | Find character X forward |
FX | Find character X backward |
tX | Move before X |
TX | Move after X backward |
; | Repeat last f/F/t/T |
, | Repeat last f/F/t/T opposite direction |
Screen movement
| Command | Action |
|---|---|
Ctrl-d / Ctrl-u | Half-page down/up |
Ctrl-f / Ctrl-b | Full-page down/up |
zz | Center current line |
zt | Current line to top |
zb | Current line to bottom |
H / M / L | Top/middle/bottom visible line |
4. Insert and Replace
| Command | Action |
|---|---|
i | Insert before cursor |
a | Insert after cursor |
I | Insert at first non-blank |
A | Insert at end of line |
o | New line below |
O | New line above |
rX | Replace one character with X |
R | Replace mode (overwrite until Esc) |
5. Editing Operators (Most Important Concept)
Pattern: operator + motion
Example: dw = delete to next word.
Operators
| Command | Action |
|---|---|
d | Delete |
c | Change (delete then insert) |
y | Yank (copy) |
gU | Uppercase |
gu | Lowercase |
> | Indent right |
< | Indent left |
= | Auto-indent |
High-value combos
| Command | Action |
|---|---|
dd | Delete line |
cc | Change entire line |
yy | Yank line |
D | Delete to end of line |
C | Change to end of line |
x | Delete character under cursor |
X | Delete character before cursor |
s | Substitute char and enter Insert |
J | Join line below with space |
gJ | Join without adding space |
6. Text Objects (Essential)
Pattern: operator + text object
Examples: ciw, di(, ya".
| Command | Action |
|---|---|
iw / aw | Inner word / around word |
iW / aW | Inner WORD / around WORD |
i" / a" | Inside quotes / around quotes |
i' / a' | Inside single quotes / around |
i( / a( | Inside parentheses / around |
i[ / a[ | Inside brackets / around |
i{ / a{ | Inside braces / around |
it / at | Inside tag / around tag |
ip / ap | Inner paragraph / around paragraph |
7. Visual Mode
| Command | Action |
|---|---|
v | Character-wise selection |
V | Line-wise selection |
Ctrl-v | Block (column) selection |
o | Switch selection end |
gv | Reselect previous visual selection |
d / c / y | Delete/change/yank selection |
> / < | Indent selection |
gU / gu | Upper/lowercase selection |
8. Copy, Paste, Registers, Clipboard
| Command | Action |
|---|---|
p / P | Paste after / before cursor |
"ayy | Yank line into register a |
"ap | Paste register a |
:reg | Show registers |
"+y | Yank to system clipboard |
"+p | Paste from system clipboard |
9. Undo, Redo, Repeat
| Command | Action |
|---|---|
u | Undo |
Ctrl-r | Redo |
. | Repeat last change |
5. | Repeat last change 5 times |
g- / g+ | Move older/newer in undo tree |
10. Search and Replace
Search navigation
| Command | Action |
|---|---|
/pattern | Search forward |
?pattern | Search backward |
n / N | Next / previous match |
* / # | Search current word forward/backward |
:nohlsearch or :noh | Clear highlighted matches |
Substitute
| Command | Action |
|---|---|
:s/old/new/ | Replace first match in current line |
:s/old/new/g | Replace all matches in current line |
:%s/old/new/g | Replace all matches in file |
:%s/old/new/gc | Replace all with confirm |
:'<,'>s/old/new/g | Replace in visual selection |
Useful search settings
| Command | Action |
|---|---|
:set hlsearch | Highlight matches |
:set incsearch | Show match while typing |
:set ignorecase | Case-insensitive by default |
:set smartcase | Case-sensitive when uppercase used |
11. Buffers, Windows, Tabs
Buffers (open files in memory)
| Command | Action |
|---|---|
:ls or :buffers | List buffers |
:bnext / :bprev | Next/previous buffer |
:b 3 | Switch to buffer 3 |
:bd | Delete current buffer |
:e! | Reload file, discard unsaved edits |
Windows (splits)
| Command | Action |
|---|---|
:split | Horizontal split |
:vsplit | Vertical split |
Ctrl-w h/j/k/l | Move between splits |
Ctrl-w w | Cycle splits |
Ctrl-w q | Close split |
Ctrl-w o | Keep only current split |
Ctrl-w = | Equalize split sizes |
Ctrl-w + / Ctrl-w - | Resize height |
Ctrl-w > / Ctrl-w < | Resize width |
Tabs
| Command | Action |
|---|---|
:tabnew | New tab |
:tabedit file.txt | Open file in new tab |
gt / gT | Next/previous tab |
:tabn / :tabp | Next/previous tab |
:tabclose | Close tab |
:tabonly | Keep only current tab |
12. Marks, Jumps, Change Navigation
| Command | Action |
|---|---|
ma | Set mark a |
`a | Jump to exact mark position |
'a | Jump to mark line |
:marks | List marks |
Ctrl-o / Ctrl-i | Back/forward in jump list |
g; / g, | Older/newer in change list |
13. Macros (Automation)
| Command | Action |
|---|---|
qa | Start recording macro in register a |
q | Stop recording |
@a | Run macro in register a |
@@ | Re-run last macro |
10@a | Run macro 10 times |
14. Indentation and Formatting
| Command | Action |
|---|---|
>> / << | Indent/unindent line |
>G | Indent from cursor to end of file |
=% | Re-indent matching block |
gg=G | Re-indent whole file |
15. Folding
| Command | Action |
|---|---|
za | Toggle fold |
zo / zc | Open/close fold |
zR / zM | Open all / close all folds |
zf{motion} | Create fold over motion |
zd | Delete one fold |
16. Command-line Ranges and Global Edits
| Command | Action |
|---|---|
:1,10d | Delete lines 1-10 |
:%d | Delete all lines |
:g/pattern/d | Delete lines matching pattern |
:v/pattern/d | Delete lines not matching pattern |
:%!sort | Filter whole file through sort command |
17. Line Endings and ^M
^M means carriage return (\r) is present.
| Command | Action |
|---|---|
:set ff? | Show file format (unix or dos) |
:%s/\r$// | Remove trailing \r on all lines |
:set ff=unix | Save with LF endings |
:set ff=dos | Save with CRLF endings |
:set list | Show invisible characters |
:set nolist | Hide invisible characters |
18. Built-in Help
| Command | Action |
|---|---|
:help | Open help |
:h {topic} | Help for topic |
:h :s | Help for substitute command |
:h w | Help for w motion |
:h text-objects | Help for text objects |
Ctrl-] | Jump to help tag under cursor |
Ctrl-o | Jump back from help |
19. Minimal Starter Vim Config
Use _vimrc on Windows, ~/.vimrc on Linux/macOS.
| |