Vim Commands
Motions
Section titled “Motions”+— first non-blank character of the next line0— start of the line$— end of the lineG— last line of the file1G,gg,:1or1gg— first line of the file2G— line 2;<n>G— line n/word— search forward forword;nrepeats the searchggVG— select the whole file:ggmoves to the first line,Vstarts line-wise visual mode,Gextends the selection to the last line
Entering insert mode
Section titled “Entering insert mode”i— insert before the cursora— insert after the cursorA— insert at the end of the lineo— open a new line below the cursor and insertGo— jump to the last line, then open a new line below it: insert at the end of the file
While in insert mode, Ctrl-o runs exactly one normal-mode command and then returns to insert
mode — useful for a single motion without abandoning the edit in progress.
Deleting and changing
Section titled “Deleting and changing”x— delete the character under the cursordw— delete to the start of the next worddt"— delete forward up to, but not including, the next"di"— delete everything inside the quotesda"— delete everything inside the quotes, including the quotes themselvesd$— delete from the cursor to the end of the linedG— delete from the cursor to the end of the fileu— undo;Ctrl-r— redo
di and da work with any delimiter pair — di(, da{, di< — and with w, s and p for
word, sentence and paragraph. Substituting c for d deletes and enters insert mode.
Substitution
Section titled “Substitution”:s/toReplace/withWord— first match on the current line:%s/toReplace/withWord— first match on every line of the file:3,4s/toReplace/withWord— first match on lines 3 to 4
Append g to replace every match on each line rather than the first, and c to confirm each
replacement.
Opening a file at a given line
Section titled “Opening a file at a given line”vim +42 myfile.txt— open positioned on line 42vim + myfile.txt— open positioned on the last line, in normal mode. The space matters:+followed immediately by text is read as an Ex command to run rather than as a line number, sovim +myfile.txtopens an empty buffer and failsvim +/pattern myfile.txt— open positioned at the first matchvim "+set number" myfile.txt— run an Ex command after the file loads
Visual mode
Section titled “Visual mode”Commenting a block out
Section titled “Commenting a block out”- Press
Esc - Press
Ctrl-vto start visual-block mode - Select the lines
- Press
I - Type the comment character
- Press
Esc— the character is inserted at the start of every selected line
Commenting a block back in
Section titled “Commenting a block back in”- Press
Esc - Press
Ctrl-v - Select the comment characters
- Press
x - Press
Esc
Selecting and deleting a range of lines
Section titled “Selecting and deleting a range of lines”- Go to the first line
- Press
V - Move down to the last line
- Press
d