vi cheat sheet
The commands defined for vi itself, so they work in whichever vi a server or a container gives you: classic vi, nvi, BusyBox vi, or the small Vim that Debian and Ubuntu install as vi. On a machine with full Vim, the Vim cheat sheet has much more.
Everything starts in command mode: press Esc if you're not sure where you are. Stuck? Esc then :q! and Enter quits without saving (every way out).
Save and quit
- :w ↵ save (write) the file
- :wq ↵ save and quit
- ZZ :x ↵ save if changed, then quit
- :q ↵ quit (refuses with unsaved changes)
- :q! ↵ quit and throw the changes away
- :e! ↵ reload the file, dropping every change
- :w {file} ↵ save to another file
Insert text
- i a insert before / after the cursor
- I A insert at the start / end of the line
- o O open a new line below / above
- Esc back to command mode
- R type over the text (replace mode)
Move
- h j k l left, down, up, right
- w b e next word, back a word, end of word
- 0 ^ $ line start, first non-blank, line end
- f{c} t{c} to / just before the next {c} on the line
- ; , repeat the last f or t, forward / back
- 1G G first line / last line
- {n}G go to line {n}
- H M L top, middle, bottom of the screen
- Ctrl-f Ctrl-b page forward / back
- Ctrl-d Ctrl-u half a page down / up
- % the matching bracket
- { } previous / next paragraph
Delete and change
- x delete the character under the cursor
- dd delete the line
- {n}dd delete {n} lines
- dw D delete a word / to the end of the line
- cw C change a word / to the end of the line
- cc S change the whole line
- r{c} replace one character with {c}
- J join the next line onto this one
- ~ switch the case of a character
- >> << indent / outdent the line
Copy and paste
- yy copy (yank) the line
- yw copy a word
- p P paste after / before the cursor
- "ayy "ap copy into / paste from named buffer a (a to z)
- xp ddp swap two characters / two lines
Undo and repeat
- u undo the last change; press again to undo the undo
- U restore the whole line as it was
- . repeat the last change
- @a run buffer a as commands
Search and replace
- /pattern ↵ search forward
- ?pattern↵ search backward
- n N next / previous match
- :s/old/new/g ↵ replace on this line
- :%s/old/new/g ↵ replace in the whole file
- :g/pattern/d ↵ delete every line that matches
- & repeat the last :s on this line
Marks, files and settings
- ma set mark a
- 'a `a jump to mark a: its line / its exact spot
- :e {file} ↵ open another file
- :r {file} ↵ insert a file below the cursor
- :!{cmd} ↵ run a shell command
- :set nu ↵ show line numbers (nonu to hide)
- :set ic ↵ ignore case when searching
- Ctrl-g show the file name and where you are
Not in every vi
Vim added these; vi itself doesn't define them, so a minimal vi may not have them. What to use instead:
- Redo with Ctrl-r classic vi has one level of undo: press u again to redo
- gg use 1G
- Visual mode (v, V, Ctrl-v) use counts and motions: d3w, 5dd, y}
- Text objects (ciw, di") use motions: bcw, or f" then ct"
- * and # type the search:
/word - Recording macros with q type the keys on a line, "ayy it into a buffer, then @a runs it
Which vi is this?
Type :version and Enter. Vim answers with its version and the features it was built with; another vi shows an error or a one-line version. From the shell, vi --version does the same. On Debian and Ubuntu, vi is often Vim's smallest build in "compatible" mode: u twice undoes the undo, and the arrow keys in Insert mode can type letters (A, B…) instead of moving; installing the vim package gives you the full editor.
Everything above also works in Vim and Neovim, and the motions carry over to Vim modes in VS Code and JetBrains IDEs. The first lessons of the course teach them in a real editor, free.