Vim Cheat Sheet
Working with files
Open a new file. You can use the Tab key for automatic file name completion, just like at the shell 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 saving the file under a different name, specify the file name.
Quit Vim. If you have unsaved changes, Vim refuses to exit.
Exit Vim without saving changes.
Write the file and exit.
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.
Moving in the file
These Vim commands and keys work both in command mode and visual mode.
j or Up Arrow
Move the cursor up one line.
k or Down Arrow
Down one line.
l or Right Arrow
Right one character.
h or Left Arrow
Left one character.
To the end of a word.
To the end of a whitespace-delimited word.
To the beginning of a word.
To the beginning of a whitespace-delimited word.
To the beginning of a line.
To the first non-whitespace character of a line.
To the end of a line.
To the first line of the screen.
To the middle line of the screen.
To the the last line of the screen.
Jump to line number n. For example, to jump to line 42, you'd type :42
Inserting and overwriting text
Insert before cursor.
Insert to the start of the current line.
Append after cursor.
Append to the end of the current line.
Open a new line below and insert.
Open a new line above and insert.
Change the rest of the current line.
Overwrite one character. After overwriting the single character, go back to command mode.
Enter insert mode but replace characters rather than inserting.
The ESC key
Exit insert/overwrite mode and go back to command mode.
Delete characters under the cursor.
Delete characters before the cursor.
dd or :d
Delete the current line.
Entering visual mode
Start highlighting characters. Use the normal [vimcheat.html#nav movement keys and commands] to select text for highlighting.
Start highlighting lines.
The ESC key
Exit visual mode and return to command mode.
Editing blocks of text
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.
Change the highlighted text.
Yank the highlighted text. In Winblows terms, "copy the selected text to clipboard."
Delete the highlighted text. In Winblows terms, "cut the selected text to clipboard."
yy or :y or Y
Yank the current line. You don't need to highlight it first.
dd or :d
Delete the current line. Again, you don't need to highlight it first.
In Winblows terms, "paste" the contents of the "clipboard". In Vim terms, you "put" the text you yanked or deleted. Put characters after the cursor. Put lines below the current line.
Put characters before the cursor. Put lines above the current line.
Undo and redo
Undo the last action.
Undo all the latest changes that were made to the current line.
Ctrl + r
Search the file for pattern.
Scan for next search match in the same direction.
Scan for next search match but opposite direction.
Substitute foo with bar. r determines the range and a determines the arguments.
The range (r) can be
Work on current line only.
Work on the line whose number you give.
The whole file.
Arguments (a) can be
Replace all occurrences in the line. Without this, Vim replaces only the first occurrences in each line.
Ignore case for the search pattern.
Don't ignore case.
Confirm each substitution. You can type y to substitute this match, n to skip this match, a to substitute this and all the remaining matches ("Yes to all"), and q to quit substitution.
Replace the first occurrence of the word foo with bar on line number 452.
Replace every occurrence of the word foo with bar on current line.
Replace every occurrence of the word foo with bar in the whole file.
The same as above, but ignore the case of the pattern you want to substitute. This replaces foo, FOO, Foo, and so on.
Confirm every substitution.
For each line on the file, replace the first occurrence of foo with bar and confirm every substitution.