A Vim Cheat Sheet for Commands I Keep Forgetting
Vim becomes useful once you stop treating every command as a separate keyboard shortcut. Motions describe where to go, operators describe what to do, and the two can be combined. Until that feels natural, a cheat sheet helps.
Press Esc if you are unsure which mode you are in. It is not elegant advice, but it works surprisingly often.
Move around
h,j,k,l: move left, down, up, and rightw: move forward by a wordb: move backward by a word0: move to the start of the line$: move to the end of the linegg: move to the start of the fileG: move to the end of the file{and}: move by paragraphCtrl-uandCtrl-d: move half a screen up or down
Add a count to repeat many commands. 5j moves down five lines, while 3w moves forward three words.
Insert and edit text
i: insert before the cursorI: insert at the start of the linea: append after the cursorA: append at the end of the lineo: open a line belowO: open a line abover: replace the character under the cursorx: delete the character under the cursordd: delete the current lineyy: yank the current linep: put the last yanked or deleted text after the cursoru: undoCtrl-r: redo
Operators combine with motions. dw deletes to the next word, d$ deletes to the end of the line, and y} yanks to the next paragraph boundary. This is the part of Vim worth learning; memorising exotic commands can wait.
Select in visual mode
v: select by characterV: select by lineCtrl-v: select a rectangular blocky: yank the selectiond: delete the selection:s/old/new/g: replace everyoldin the selected lines
Search and replace
/searchterm: search forward?searchterm: search backwardn: repeat the search in the same directionN: repeat it in the opposite direction:%s/old/new/g: replace everyoldin the file:%s/old/new/gc: do the same, but ask before each replacement
The final c has prevented enough ambitious replacements that I usually add it first and remove it only when I am certain.
Save, open, and quit
:w: save:wqor:x: save and quit:q: quit when there are no unsaved changes:q!: quit and discard changes:e filename: edit another file:e!: reload the current file and discard unsaved changes
Splits, tabs, and useful settings
:sp: split the window horizontally:vsp: split it verticallyCtrl-w w: move between windows:tabnew: open a tabgtandgT: move to the next or previous tab:set number: show line numbers:set nonumber: hide line numbers:set syntax=python: set Python syntax highlighting
Put lasting settings and mappings in .vimrc. Start with a small configuration you understand. Copying a giant setup on the first day is an efficient way to turn someone else’s editor into your own mystery.