Vim #
A modal editor: keys do different things depending on the mode. Press Esc to return to Normal mode, where most commands live. Commands compose, so a count and a motion combine: 3dw deletes three words.
Modes #
| Key | Mode |
|---|---|
Esc | Back to Normal mode |
i a | Insert before / after the cursor |
I A | Insert at line start / end |
o O | Open a new line below / above |
v V | Visual (character / line) select |
Ctrl-v | Visual block select |
: | Command-line mode |
Motions #
| Key | Moves to |
|---|---|
h j k l | Left, down, up, right |
w b e | Next word, back a word, end of word |
0 ^ $ | Line start, first non-blank, line end |
gg G | First line, last line |
{ } | Previous / next paragraph |
Ctrl-d Ctrl-u | Half page down / up |
% | Matching bracket |
f x t x | Jump to / before next x on the line |
Editing #
| Key | Action |
|---|---|
x r | Delete char / replace one char |
dd yy p | Delete line, yank line, paste |
dw cw | Delete / change a word |
d$ c$ | Delete / change to end of line |
ciw ci" | Change inner word / inside quotes |
>> << | Indent / outdent the line |
u Ctrl-r | Undo / redo |
. | Repeat the last change |
J | Join the next line up |
Text objects & marks #
| Key | Selects |
|---|---|
iw aw | Inner word / word plus its space |
i( a( | Inside parentheses / parentheses included |
i" a" | Inside quotes / quotes included |
it at | Inside an HTML tag / tag included |
ip ap | Inner paragraph / paragraph plus blank line |
ci( dat yip | Change in parens, delete a tag pair, yank a paragraph |
ma set mark a on the current line
'a jump to the mark's line `a jump to its exact spot
:marks list all marks
Ctrl-o Ctrl-i back / forward through the jump list
Registers & macros #
| Key | Action |
|---|---|
"ay "ap | Yank into / paste from register a |
"+y "+p | Copy to / paste from the system clipboard |
"0p | Paste the last yank (deletes never touch register 0) |
:registers | View all registers |
qa ... q | Record a macro into register a, stop with q |
@a @@ | Replay macro a / repeat the last replay |
5@a | Replay macro a five times |
Search & replace #
/pattern search forward ?pattern search backward
n N next / previous match
* # search word under the cursor
:%s/old/new/g replace all in the file
:%s/old/new/gc ... with confirmation per match
:s/old/new/g replace all on the current line
:noh clear search highlight
:g/pattern/d delete every line that matches
:g!/pattern/d delete every line that does not match (:v/pattern/d is the same)
Buffers, windows, tabs #
:e file open a file :w write :q quit :wq write+quit
:bn :bp next / prev buffer :ls list buffers
:sp :vsp split horizontal / vertical
Ctrl-w h/j/k/l move between splits Ctrl-w q close split
:tabnew new tab gt gT next / prev tab
Survival #
| Key | Action |
|---|---|
:q! | Quit without saving |
ZZ | Save and quit |
:w !sudo tee % | Save a file you opened without sudo |
q: | Open the command history window |
:help x | Help for command x |
gg=G | Re-indent the whole file |
Ctrl-o Ctrl-i | Jump to older / newer cursor position |