Reference
Vim cheat sheet
Every command the course teaches, plus the basics everyone looks up first. ClickTap one to practise it in its lesson.
Make it stick
Knowing a command isn't the habit yet. Drill it, play it, or try it on its own.
Save, quit and the basics
- Esc back to Normal mode, where every command below starts
- :w ↵ save (write) the file
- :q ↵ quit (refuses while there are unsaved changes)
- ZZ :wq ↵ :x ↵ save and quit
- ZQ :q! ↵ quit without saving: throw the changes away
- :qa! ↵ quit every window without saving
- :w {file} ↵ save as (write to another file)
- :e {file} ↵ open (edit) a file
- u Ctrl-r undo / redo
- yy p copy (yank) a line, paste it below
- dd delete (cut) a line
- "+y "+p copy to / paste from the system clipboard
- /pattern ↵ search; n and N for the next and previous match
- :%s/old/new/g ↵ find and replace in the whole file (add c to confirm each)
- G :42 ↵ go to the last line / to line 42
- :set number ↵ show line numbers (relativenumber for relative ones)
- :noh ↵ clear the search highlighting
- :help {topic} ↵ Vim's own manual, on any command (:help cw)
Foundations
- i insert before the cursor
- Esc back to Normal mode (also Ctrl-[)
- x delete the character under the cursor
- r{c} replace one character with {c}
- h j k l left, down, up, right
- {n}j {n}k a count multiplies a motion: 5j is five lines down
- w b e next word, back a word, end of word
- W B E the same for space-separated WORDs (leap over punctuation)
- ge end of the previous word
- a append after the cursor
- I A insert at the line start / append at the line end
- o O open a new line below / above
- 0 ^ $ line start, first non-blank, line end
- f{c} F{c} jump to the next / previous {c} on the line
- t{c} T{c} jump till (just before) {c}
- ; , repeat the last f/t forwards / backwards
The Vim Way
- . repeat the last change
- j. n. ;. the dot formula: one key to move, one to repeat
- C change to the end of the line
- s substitute the character under the cursor (delete it, start typing)
- S substitute the whole line, keeping its indent
- cc change the whole line
- * # search for the word under the cursor
- cgn change the next match (then . for each one after)
- n N next / previous match
- u undo
- Ctrl-r redo
- U undo every change on the last line
- Ctrl-a Ctrl-x add to / subtract from the number under or after the cursor
- gCtrl-a (Visual) count up across the selected lines
Operators
- d{motion} delete
- c{motion} change (delete, then insert)
- y{motion} yank (copy)
- p P put after / before
- dw de db delete to the next word / word end / back
- cw change to the end of the word (like ce)
- dd yy delete / yank the whole line (doubling means the line)
- D Y delete to the end of the line / yank the line
- J gJ join lines (gJ without a space)
- gp put and leave the cursor after the text
- xp ddp swap two characters / two lines
- d3w 3dd counts work on operators and motions alike
- > < = indent / dedent / re-indent ({motion} or doubled)
- >> << indent / dedent the line
- gU gu g~ uppercase / lowercase / toggle case
- ~ toggle the case of the character and move on
Vertical movement
- {n}j {n}k jump by the distance the gutter shows (relative numbers)
- gg G first line, last line
- {n}G :{n} go to line n
- { } previous / next blank line (paragraph)
- Ctrl-d Ctrl-u scroll half a page down / up
- Ctrl-f Ctrl-b scroll a whole page forward / back
- Ctrl-e Ctrl-y scroll one line, keeping the cursor
- zz zt zb put the cursor line in the middle / top / bottom of the window
- H M L top, middle, bottom of the window
Search
- /pattern ↵ ?pattern ↵ search forward / backward
- g* g# search the word under the cursor, partial matches too
- d/pat↵ a search is a motion: delete up to the match
- /pat/e ↵ land on the end of the match (s+2, b-1 also work)
- //e ↵ the last pattern again, with a new offset
- \v very magic: write regexes like most languages do
- \< \> start / end of a word
- \zs \ze start / end the match here (keep the rest as context)
- \c ignore case for this search
Text objects
- iw aw inner word / a word with its space
- iW aW the same for WORDs
- i" a" inside / around double quotes
- i' i` inside single quotes / backticks
- i( a( ib ab inside / around parentheses (b is the same pair)
- i[ i{ iB i< inside brackets, braces (B is the same pair), angle brackets
- 2i( a count reaches the next pair out
- it at inside / around an HTML or JSX tag
- ip ap inner paragraph / paragraph plus its blank line
- is as inner sentence / sentence
- ( ) previous / next sentence
- ci" . change inside, then repeat it from anywhere in the next one
Visual mode
- v Visual mode (characters), then any operator
- V Visual Line mode
- o (Visual) jump to the other end of the selection
- gv select the last selection again
- Ctrl-v Visual Block mode (columns)
- Ctrl-vI Ctrl-vA insert / append on every line of the block
- Ctrl-v$A append at each line end, however ragged
- daw vs vawd prefer the operator: it repeats with .
Insert mode power
- Ctrl-w Ctrl-u (Insert mode) delete the word / everything typed before the cursor
- Ctrl-h (Insert mode) backspace
- Ctrl-o (Insert mode) run one Normal-mode command
- Ctrl-r{reg} (Insert mode) paste a register
- Ctrl-r= (Insert mode) insert the result of a calculation
- Ctrl-n Ctrl-p (Insert mode) complete a word from the file
- R Replace mode: type over the text
- r↵ split the line here
Registers
Macros
- qa … q record a macro into register a
- @a @@ play macro a / play the last macro again
- {n}@a play it n times (it stops when a motion fails)
- qA append to macro a
- :put a ↵ 0"ay$ lay a macro out on a line to edit it, then put it back
- :normal @a run a macro on every line of a range
- Ctrl-a inside a macro: number a list as you go
Ex & substitution
- :%s/a/b/g ↵ replace a with b everywhere
- :'<,'> :.,+3 ranges: the selection, this line and the next three
- :s///c ↵ confirm each replacement (y, n, a, q, l)
- \1 \u \U in the replacement: a group, next letter / rest uppercase
- & g& repeat the last :s on this line / everywhere
- :g/re/d ↵ delete every matching line
- :v/re/d ↵ delete every line that does not match
- :%norm A; ↵ run Normal-mode keys on each line
- :t. :m0 copy the line below / move it to the top
- :sort ↵ sort lines (n numeric, u unique)
- \= in the replacement: compute it (submatch(0) + 1)
Real-world refactors
Plugins & your editor
The same keys in another editor: VS Code, JetBrains IDEs, Zed, Obsidian. Which Vim to run: Vim vs Neovim.