This is a cheatsheet of the commands I most frequently use for Vim. For a beginner’s guide to Vim, refer to my post Starting Vim.
Necessary commands
- Going into the Insert mode:
i
starting before the current cursor positiona
starting after the current cursor positiono
adds a new blank line after the current cursor position and goes into the insert mode
u
undo last editctrl
+r
redo- Exiting vim:
:w
write file:q
quit, assuming you haven’t made any changes in the file:x
or:wq
write file, then quit
Finding things
f
+pattern
goes to the next instance ofpattern
in that line/pattern
searches forward from the current cursor position for the text matchingpattern
. Press/
, type the search term (pattern), and pressEnter
.- To search for the word “example”: first, ensure you are in Normal mode by pressing
esc
. Then type/example
and pressEnter
. - To navigate search results,
n
moves to the next occurrence of the search pattern. N
moves to the previous occurrence of the search pattern.
- To search for the word “example”: first, ensure you are in Normal mode by pressing
?pattern
searches backward from the current cursor position for the text matchingpattern
. Press?
, type the search term (pattern), and pressEnter
.- To search backward for the word “example”, you would type
?example
in Normal mode and pressEnter
.
- To search backward for the word “example”, you would type
Moving around the line
^
or0
goes to the beginning of the line$
goes to the end of the linegg
goes to the beginning of the line
Moving around the file
:0
goes to line zero:135
goes to line 135:+5
goes 5 lines forward:-5
goes 5 lines backwardG
goes to the last line of the file
Text editing
dd
delete current lined$
orD
deletes from current cursor position to the end of the line