Vim Commands Cheat Sheet
Vim Commands Cheat Sheet
A Vim cheat sheet, listing some useful, essential and most often used Vim commands.
Open a new file. You can use the Tab key for automatic file name completion, just like at the shell
:e filename
command prompt.
Save changes to a file. If you don’t specify a file name, Vim saves as the file name you were editing. For
:w filename
saving the file under a different name, specify the file name.
Almost the same as :wq, write the file and exit if you’ve made changes to the file. If you haven’t made
any changes to the file, Vim exits without writing the file.
These Vim commands and keys work both in command mode and visual mode.
k or Down
Down one line.
Arrow
h or Left
Left one character.
Arrow
l or Right
Right one character.
Arrow
1/4
L To the the last line of the screen.
:n Jump to line number n. For example, to jump to line 42, you’d type :42
r Overwrite one character. After overwriting the single character, go back to command mode.
The ESC key Exit insert/overwrite mode and go back to command mode.
Deleting text
Start highlighting characters. Use the normal movement keys and commands to select text for
v
highlighting.
The ESC key Exit visual mode and return to command mode.
Note: the Vim commands marked with (V) work in visual mode, when you’ve selected some text. The other commands
work in the command mode, when you haven’t selected any text.
Change the case of characters. This works both in visual and command mode. In visual mode, change
~
the case of highlighted characters. In command mode, change the case of the character uder cursor.
2/4
< (V) Shift left (de-indent).
y (V) Yank the highlighted text. In Windows terms, “copy the selected text to clipboard.”
d (V) Delete the highlighted text. In Windows terms, “cut the selected text to clipboard.”
dd or :d Delete the current line. Again, you don’t need to highlight it first.
Put the text you yanked or deleted. In Windows terms, “paste the contents of the clipboard”. Put
p
characters after the cursor. Put lines below the current line.
P Put characters before the cursor. Put lines above the current line.
U Undo all the latest changes that were made to the current line.
Ctrl + r Redo.
Search
Replace
g Replace all occurrences in the line. Without this, Vim replaces only the first occurrences in each line.
Confirm each substitution. You can type y to substitute this match, n to skip this match, a to substitute
c
this and all the remaining matches (“Yes to all”), and q to quit substitution.
Examples
3/4
:452s/foo/bar/ Replace the first occurrence of the word foo with bar on line number 452.
The same as above, but ignore the case of the pattern you want to substitute. This
:%s/foo/bar/gi
replaces foo, FOO, Foo, and so on.
:%s/foo/bar/c For each line on the file, replace the first occurrence of foo with bar and confirm every substitution.
4/4