This is my personal cheatsheet as intermediate Vim user so I skipped the ones that I consider basic. All the commands listed are used in command mode for fast editing.
Command execution
In command mode, use colon to start ex command. Here are some examples of ex commands:
- e: edit file
- g: global command
- q: quit
- w: write
- s: substitute
The commands (g)lobal and (s)ubstitute are heavily used in string manipulation. The rest are summarized here:
:e! | reload current file discarding all unsaved changes |
:e newfile.txt | open file newfile.txt for editing |
:e . | load current directory |
:w! | force write (if permission allows) |
shift + zz | equivalent to :wq! |
shift + zq | equivalent to :q! |
Operator
. | repeat last operation |
fx | find next character x on the same line |
Text Editing
y for yank(copy), i for inside, a for around, d for delete, w for word, p for paragraph or paste. Examples:
yyp | copy current line and insert after |
diw | delete the entire word where the cursor sits in (dw deletes from cursor to end of word; db deletes from cursor to beginning of word) |
shift + V | select entire line in visual mode (v selects character in visual mode) |
2> | visual mode: indent twice on all selected lines edit mode: indent once for 2 lines |
3< | visual mode: outdent three times on all selected lines edit mode: outdent once for 3 lines |
di” | delete everything between the double quotes surrounding the cursor (exclusive); use c instead of d to finish the same effect with insert mode |
di> | delete everything between < and > surrounding the cursor (exclusive); use (c)hange instead of (d)elete to finish the same effect with insert mode |
dit | delete everything between tags. e.g. <xml>contenttodelete</xml> |
dip | delete the entire paragraph |
da’ | delete everything between the single quote surrounding the cursor (inclusive); use c instead of d to finish the same effect with insert mode |
da} | delete everything between { and } surrounding the cursor (inclusive); use c instead of d to finish the same effect with insert mode |
dt. | delete all characters until the next . |
i | insert at cursor location |
shift + I | move cursor to first non-blank character of line and start in insert mode |
a | insert at the location next to cursor |
shift + A | move cursor to last non-blank character of line and start in insert mode |
0 | move to beginning of line. ^ moves to first non-blank character in the line. $ moves to the end of line |
Note: wherever d is used in this table, c can be used instead for the same effect but switch to editing mode at the end.
String Manipulation
The general patterns are:
- [range]g/pattern/cmd
- [range]s/match/replacement/option
If range is not specified, it applies to current line only! To specify the whole file, use % range. You may also specify line range such as “10,20”. Here are some examples:
:%s/bacon/lettuce | For every line of the file, replace the first occurrence of bacon in each line to lettuce |
:%s/bacon/lettuce/g | For every line of the file, replace all occurrences of bacon to lettuce |
:s/bacon/lettuce | For current line, replace the first occurrence of bacon to lettuce |
:s/bacon/lettuce/gi | For current line, replace all occurrences of bacon to lettuce, case insensitive |
:g/bacon/d | delete all lines that contain pattern ‘bacon’ |
:g!/lettuce/d | delete all lines that do not contain pattern ‘lettice’; or use :v/lettuce/d instead |
:g/^\s*$/d | delete all blank lines. \s* represents zero or more white spaces |
Bookmarking
ma | mark cursor line as bookmark a |
`a | jump to cursor position at line a |
‘a | jump to beginning of line a |
`. | jump to last line where change occurred |
“ | jump back |
Insert Mode
Ctrl + N | Auto complete |
Edit and run
While tmux and screen can help split screen in Bash, we sometimes need to split a bash screen to run a quick command when we’re already in vim. This can be done with some simple commands.
:term | Open up a terminal above vim. You can also spell :ter or :terminal |
Ctrl+W; Ctrl+W | Press Ctrl+W twice can help you toggle between the terminal and vim buffer |
Ctrl+D | Close the terminal |
This can be very helpful when you are debugging code and need it run repeatedly. You don’t need to exit vim just to run a command and come back. Note that while you’re in terminal, you can’t use Ctrl+W as a shortcut key to backspace a word. Use Alt + Delete instead.