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.
gist.github.com/sawissac/8d4b65ba69d6ad6d8bb76106a6cdba63Modes first
Vim has modes, and most confusion comes from being in the wrong one.
| Mode | You're in it when | Get there with |
|---|---|---|
| Normal | Keys are commands. Vim starts here | Esc |
| Insert | Keys type text | i, a, o and others |
| Visual | Keys extend a selection | v, V, Ctrl+v |
| Command | You'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:
ddelete,cchange (delete then insert),yyank (copy)wto 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
| Key | Moves to |
|---|---|
h j k l | Left, down, up, right |
0 | Start of the line |
^ | First non-blank character of the line |
$ | End of the line |
42G or :42 | Line 42 |
gg / G | First line / last line |
By word
| Key | Moves to |
|---|---|
w / b | Start of the next / previous word |
e | End of the word |
W B E | Same, but a "word" is anything between spaces |
By screen
| Key | Does |
|---|---|
Ctrl+f / Ctrl+b | Forward / back one full screen |
Ctrl+d / Ctrl+u | Down / up half a screen |
Ctrl+e / Ctrl+y | Scroll one line without moving the cursor |
H M L | Top, middle, bottom of the screen (high, middle, low) |
zz | Scroll so the cursor line sits in the middle |
Inserting text
| Key | Starts inserting |
|---|---|
i / a | Before / after the cursor |
I / A | At the start / end of the line |
o / O | On a new line below / above |
ea | At the end of the current word |
Esc | Stop inserting, back to Normal mode |
Editing
| Key | Does |
|---|---|
x | Delete the character under the cursor |
r | Replace one character, then stay in Normal mode |
s | Delete one character and start inserting |
cc | Replace the whole line |
C or c$ | Replace from the cursor to the end of the line |
cw | Replace to the end of the word |
J | Join the next line onto this one, with a space |
gJ | Join without the space |
~ | Toggle the case of the character under the cursor |
u / Ctrl+r | Undo / redo |
. | Repeat the last change |
. is the one to build habits around. Make a change once, move, press ., move, press ..
Cut, copy and paste
| Key | Does |
|---|---|
yy | Copy the line |
5yy | Copy five lines |
dd | Cut the line |
5dd | Cut five lines |
p / P | Paste 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
| Key | Selects |
|---|---|
v | Characters |
V | Whole lines |
Ctrl+v | A rectangular block |
o | Jump to the other end of the selection |
Once something is selected:
| Key | Does |
|---|---|
y / d | Copy / cut the selection |
c | Change the selection |
> / < | Indent / outdent |
u / U | Lowercase / 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.
| Object | Selects |
|---|---|
aw / iw | A word |
ab or a( / ib | A ( ) block |
aB or a{ / iB | A { } block |
a[ / i[ | A [ ] block |
a< / i< | A < > block |
at / it | An 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
| Key | Does |
|---|---|
/pattern | Search forward |
?pattern | Search backward |
n / N | Next match / previous match |
* / # | Next / previous occurrence of the word under the cursor |
:%s/old/new/g | Replace every old with new in the file |
Add c to the end, :%s/old/new/gc, to confirm each replacement.
Save and quit
| Command | Does |
|---|---|
:w | Save |
:wq, :x or ZZ | Save and quit |
:q | Quit (fails if there are unsaved changes) |
:q! or ZQ | Quit and throw away changes |
:w new_name | Write a copy to new_name, keep editing the original |
:sav new_name | Save 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.
| Command | Does |
|---|---|
:e file | Open a file in a new buffer |
:ls | List open buffers |
:bn / :bp | Next / previous buffer |
:b 3 | Go to buffer 3 |
:b name | Go to a buffer by (part of) its name |
:b# | Go back to the buffer you were just in |
:bd | Close the buffer |
:sp file | Open a file in a horizontal split |
:vs file | Open a file in a vertical split |
:vert ba | Show every buffer in vertical splits |
Window commands all start with Ctrl+w, released, then one more key:
| Keys | Does |
|---|---|
Ctrl+w s | Split horizontally |
Ctrl+w v | Split vertically |
Ctrl+w w | Move to the next window |
Ctrl+w h j k l | Move to the window in that direction |
Ctrl+w x | Swap with the next window |
Ctrl+w = | Make all windows the same size |
Ctrl+w q | Close the window |
Marks and jumps
A mark is a bookmark you set with m and a letter.
| Key | Does |
|---|---|
ma | Set mark a in this file (lowercase marks are per file) |
mA | Set mark A across files (uppercase marks are global) |
`a | Jump to the exact position of mark a |
'a | Jump to the start of the line of mark a |
y`a | Copy from the cursor to mark a |
`. | Jump to the last change |
`0 | Jump to where you were when you last quit Vim |
`` | Jump back to where you were before the last jump |
:marks | List all marks |
Vim also keeps two histories you can walk through:
| Key | Does |
|---|---|
Ctrl+o / Ctrl+i | Back / 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
| Key | Does |
|---|---|
qa | Start recording into register a |
q | Stop recording |
@a | Play macro a |
@@ | Play the last macro again |
10@a | Play 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
| Command | Does |
|---|---|
:colorscheme name | Switch to a colour scheme |
:colorscheme Ctrl+d | List 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.