Vim cheat sheet

03 Nov 2014

You still can’t remember all of those weird Vim commands.

How to move around

Command Move to …
b previous word
w next word
^ beginning of text in line
$ end of line
⌃ ctrlB page up
⌃ ctrlF page down
1G beginning of file
G end of file
n G line number n

How to delete stuff

Command Delete …
dd whole line
n dd n lines
d$ from cursor until end of line

How to copy/cut/paste

Command Action
v start selecting characters, or
V start selecting lines,
  … move around as needed, then …
y copy, or
d cut,
  … move around as needed, and finally …
P paste before the cursor

How to search/replace

Command Action
/ regex ⏎ return search regex
/ ⏎ return search again
:%s/ regex / str /gc ⏎ return replace all regex with str (asks to confirm)
:%s/\s\+//g ⏎ return kill all trailing spaces
: / browse recent commands

And, of course, vim also made up its own regex syntax.

Regex command Matches …
. any character
a \? 0 or 1 times a
a * 0 or more times a
a \+ 1 or more times a
a \{-} 0 or more times a non-greedily
a \{-1,} 1 or more times a non-greedily
\( regex \) regex and stores it in a group
\n new line character

In the replacement text

Escape sequence Expands to …
\0 text matched
\1, \2, etc. first, second, etc. group matched
\r new line character (why not \n? don’t ask)

How to do other useful stuff

Command Action
u undo
⌃ ctrlR redo
>> indent line
num >> indent num lines
<< unindent line
num << unindent num lines

Useful config settings

You usually put these in your ~/.vimrc file.

syntax on                            " enable syntax highlighting
set tabstop=2 shiftwidth=2 expandtab " indent with two spaces (no tabs)
set colorcolumn=80                   " highlight column at the 80 characters mark