Overview
Vim commands compose as operator + motion (or text object), optionally with a count: d3w deletes three words. Terminal multiplexing is in tmux-commands; the regex flavor differs from other tools, see regex-patterns.
Modes and basics
Stay in Normal mode and enter Insert only to type.
| Key | Effect |
|---|---|
Esc, Ctrl-[ | Normal mode (press twice when unsure). |
i a o I A O | Insert before / after / new line below / at line start / at line end / new line above. |
v, V, Ctrl-v | Visual: characters, lines, block. |
:w, :q, :wq, :q! | Write, quit, both, quit and discard. |
u, Ctrl-r, . | Undo, redo, repeat the last change. |
Motions
Use counts and f or t motions instead of repeated h and l.
| Motion | Moves to |
|---|---|
h j k l | Left, down, up, right. |
w b e | Next word start / previous word start / word end (W B E use whitespace-delimited WORDs). |
0 ^ $ | Line start / first non-blank / line end. |
gg G :42 | File start / end / line 42. |
{ } % | Previous / next blank line, matching bracket. |
f{c} t{c} | On / just before the next c on the line (F T go backward; ; , repeat). |
Ctrl-d Ctrl-u | Half page down / up. |
Operators and text objects
Compose an operator with a text object instead of selecting by hand.
| Operator | Action |
|---|---|
d c y | Delete, change (then insert), yank. |
> < = | Indent, de-indent, auto-indent. |
gU gu | Uppercase, lowercase. |
dd cc yy | Whole-line forms. |
| Text object | Selects |
|---|---|
iw aw | Word without / with surrounding space. |
i" a", i( a(, i{ a{ | Inside / around quotes, parentheses, braces. |
it at | Inside / around an HTML or XML tag. |
ip ap | Paragraph. |
ci" changes everything inside double quotes; da( deletes a parenthesized expression with its parentheses.
Search and replace
Add c to confirm replacements across a file.
| Command | Effect |
|---|---|
/pat, ?pat, n, N | Search forward, backward, next, previous. |
*, # | Search for the word under the cursor. |
:%s/old/new/g | Replace in the whole file; add c to confirm each. |
:10,20s/old/new/g | Replace in a line range. |
:'<,'>s/old/new/g | Replace in the visual selection. |
:noh | Clear search highlighting. |
Start a pattern with \v (“very magic”) to use + ( ) | unescaped; @ is then special, so write a literal one as \@: :%s/\v(\w+)\@(\w+)/\1 at \2/g.
Registers and macros
Record macros that end where the next run can start.
| Command | Effect |
|---|---|
"ayy, "ap | Yank a line into register a, paste from it. |
"+y, "+p | Yank to, paste from the system clipboard. |
"_dd | Delete into the black-hole register; yank register untouched. |
:reg | Show registers. |
qa … q, @a, @@, 10@a | Record into a, play, replay last, play ten times. |
Gotchas
Avoid these; each loses a yank, an edit, or a pattern match.
| Gotcha | Fix |
|---|---|
x and dd overwrite the unnamed register, losing your last yank. | Yank into a named register, or delete with "_. |
:q! discards changes without asking. | Use :wq, or :wqa to save every buffer. |
Without \v, +, (, and | are literals in a pattern. | Prefix \v, or escape them. |
| Pasting from the system clipboard can stair-step indentation. | Modern Vim and Neovim handle bracketed paste; otherwise :set paste then :set nopaste. |
| A macro that ends mid-line replays badly with a count. | End it on a motion that sets up the next run. |