Vim cheat sheet
The commands worth knowing by heart. Most take a count: 3dd deletes three lines, 5j moves five down.
Modes
- i a
- insert before / after the cursor
- I A
- insert at start / end of line
- o O
- open a line below / above
- v V Ctrl-v
- visual: characters / lines / block
- Esc
- back to normal mode
- :
- command line
Movement
- h j k l
- left, down, up, right
- w b e
- next word, previous word, end of word
- 0 ^ $
- line start, first non-blank, line end
- gg G 42G
- first line, last line, line 42
- fx tx ;
- to / before next x on the line, repeat
- { }
- previous / next paragraph
- %
- matching bracket
- Ctrl-d Ctrl-u
- half a page down / up
- Ctrl-o Ctrl-i
- back / forward in the jump list
Editing
- x
- delete a character
- dd D
- delete the line / to end of line
- cc C
- change the line / to end of line
- rx
- replace one character with x
- J
- join the next line
- >> <<
- indent / unindent
- ~
- switch case
- u Ctrl-r
- undo / redo
- .
- repeat the last change
Operators and text objects
- d c y
- delete, change, yank, followed by a motion
- dw d$ dG
- delete a word, to line end, to file end
- ciw caw
- change inner word / a word with its space
- ci" ci( ci{
- change inside quotes, parentheses, braces
- dap yip
- delete a paragraph, yank inner paragraph
- dit
- delete inside an XML tag
Copy and paste
- yy yw
- yank the line / a word
- p P
- paste after / before the cursor
- "ayy "ap
- yank into / paste from register a
- "+y "+p
- system clipboard
- "0p
- paste the last yank, not the last delete
- :reg
- show registers
Search and replace
- /text ?text
- search forward / backward
- n N
- next / previous match
- * #
- search the word under the cursor
- :s/a/b/g
- replace on the current line
- :%s/a/b/gc
- replace in the file, asking each time
- :noh
- clear the highlight
Files and buffers
- :w :q :wq
- write, quit, both
- :q!
- quit without saving
- :e file
- open a file
- :ls :b 2
- list buffers, switch to buffer 2
- :bn :bp :bd
- next, previous, close buffer
- Ctrl-^
- toggle between the last two buffers
Windows and tabs
- :sp :vsp
- split horizontally / vertically
- Ctrl-w h/j/k/l
- move between windows
- Ctrl-w =
- make windows equal
- Ctrl-w c / o
- close this window / all others
- :tabnew gt gT
- new tab, next, previous
Macros and marks
- qa … q
- record a macro into register a
- @a @@
- run macro a, run the last one again
- ma 'a
- set mark a, jump to its line
- ``
- back to where you jumped from
- :normal .
- repeat the last change on each selected line