Tooling · Vim

The Vim keys worth learning

Vim looks like a wall of single-letter commands until you notice they combine: a verb, an optional count, and a motion. This cheat sheet groups the keys by task, starting with that grammar, so you can learn a few and guess the rest.

8 min read

  • vim
  • neovim
  • editor
  • cheat sheet
gist.github.com/sawissac/8d4b65ba69d6ad6d8bb76106a6cdba63

Modes first

Vim has modes, and most confusion comes from being in the wrong one.

ModeYou're in it whenGet there with
NormalKeys are commands. Vim starts hereEsc
InsertKeys type texti, a, o and others
VisualKeys extend a selectionv, V, Ctrl+v
CommandYou're typing after a :, / or ?:, /, ?

When in doubt, press Esc. It always takes you back to Normal mode.

The grammar

Most editing in Vim is operator + motion:

  • d delete, c change (delete then insert), y yank (copy)
  • w to the next word, $ to the end of the line, } to the next paragraph

So dw deletes a word, c$ changes to the end of the line, and y} copies to the end of the paragraph. Put a number in front to repeat it: 3dw deletes three words.

Doubling an operator applies it to the whole line: dd, cc, yy. Every motion you learn below works with every operator, which is why learning Vim gets faster the further you go.

Moving around

By character and line

KeyMoves to
h j k lLeft, down, up, right
0Start of the line
^First non-blank character of the line
$End of the line
42G or :42Line 42
gg / GFirst line / last line

By word

KeyMoves to
w / bStart of the next / previous word
eEnd of the word
W B ESame, but a "word" is anything between spaces

By screen

KeyDoes
Ctrl+f / Ctrl+bForward / back one full screen
Ctrl+d / Ctrl+uDown / up half a screen
Ctrl+e / Ctrl+yScroll one line without moving the cursor
H M LTop, middle, bottom of the screen (high, middle, low)
zzScroll so the cursor line sits in the middle

Inserting text

KeyStarts inserting
i / aBefore / after the cursor
I / AAt the start / end of the line
o / OOn a new line below / above
eaAt the end of the current word
EscStop inserting, back to Normal mode

Editing

KeyDoes
xDelete the character under the cursor
rReplace one character, then stay in Normal mode
sDelete one character and start inserting
ccReplace the whole line
C or c$Replace from the cursor to the end of the line
cwReplace to the end of the word
JJoin the next line onto this one, with a space
gJJoin without the space
~Toggle the case of the character under the cursor
u / Ctrl+rUndo / redo
.Repeat the last change

. is the one to build habits around. Make a change once, move, press ., move, press ..

Cut, copy and paste

KeyDoes
yyCopy the line
5yyCopy five lines
ddCut the line
5ddCut five lines
p / PPaste after / before the cursor

Vim's "delete" is a cut: whatever d, c or x removes goes into the same register p pastes from.

Visual mode and text objects

KeySelects
vCharacters
VWhole lines
Ctrl+vA rectangular block
oJump to the other end of the selection

Once something is selected:

KeyDoes
y / dCopy / cut the selection
cChange the selection
> / <Indent / outdent
u / ULowercase / uppercase

Text objects select a thing by what it is rather than where it ends. a takes the thing including its delimiters or surrounding space, i takes only the inside.

ObjectSelects
aw / iwA word
ab or a( / ibA ( ) block
aB or a{ / iBA { } block
a[ / i[A [ ] block
a< / i<A < > block
at / itAn HTML or XML tag pair and its contents
a" / i"A double-quoted string

They work after any operator too, without entering Visual mode first. ci" replaces the text inside the quotes the cursor is in. dit empties a tag. yi{ copies a function body.

Search

KeyDoes
/patternSearch forward
?patternSearch backward
n / NNext match / previous match
* / #Next / previous occurrence of the word under the cursor
:%s/old/new/gReplace every old with new in the file

Add c to the end, :%s/old/new/gc, to confirm each replacement.

Save and quit

CommandDoes
:wSave
:wq, :x or ZZSave and quit
:qQuit (fails if there are unsaved changes)
:q! or ZQQuit and throw away changes
:w new_nameWrite a copy to new_name, keep editing the original
:sav new_nameSave as new_name and keep editing the new file
:w !sudo tee %Save a file you opened without permission to write it

The sudo tee trick works in Vim but not in Neovim, which can't prompt for the password that way. In Neovim, save to a temporary file and move it with sudo.

Buffers, splits and windows

Every open file is a buffer. A window is a view onto a buffer, and you can split the screen into several.

CommandDoes
:e fileOpen a file in a new buffer
:lsList open buffers
:bn / :bpNext / previous buffer
:b 3Go to buffer 3
:b nameGo to a buffer by (part of) its name
:b#Go back to the buffer you were just in
:bdClose the buffer
:sp fileOpen a file in a horizontal split
:vs fileOpen a file in a vertical split
:vert baShow every buffer in vertical splits

Window commands all start with Ctrl+w, released, then one more key:

KeysDoes
Ctrl+w sSplit horizontally
Ctrl+w vSplit vertically
Ctrl+w wMove to the next window
Ctrl+w h j k lMove to the window in that direction
Ctrl+w xSwap with the next window
Ctrl+w =Make all windows the same size
Ctrl+w qClose the window

Marks and jumps

A mark is a bookmark you set with m and a letter.

KeyDoes
maSet mark a in this file (lowercase marks are per file)
mASet mark A across files (uppercase marks are global)
`aJump to the exact position of mark a
'aJump to the start of the line of mark a
y`aCopy from the cursor to mark a
`.Jump to the last change
`0Jump to where you were when you last quit Vim
``Jump back to where you were before the last jump
:marksList all marks

Vim also keeps two histories you can walk through:

KeyDoes
Ctrl+o / Ctrl+iBack / forward through the jump list (:jumps)
g; / g,Back / forward through the change list (:changes)

Ctrl+o is the one you'll use constantly: jump to a definition, search for something, then Ctrl+o your way back.

Macros

KeyDoes
qaStart recording into register a
qStop recording
@aPlay macro a
@@Play the last macro again
10@aPlay macro a ten times

A macro is a recording of keystrokes. Record one edit on one line, ending with j to move to the next line, then replay it down the file.

Colour schemes

CommandDoes
:colorscheme nameSwitch to a colour scheme
:colorscheme Ctrl+dList the installed schemes (note the space before Ctrl+d)

Don't try to learn the whole page. Start with the grammar, ciw, . and Ctrl+o, and come back for the rest when you notice yourself pressing the same key ten times.