Personal Vim cheatsheet

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.txtopen file newfile.txt for editing
:e .load current directory
:w!force write (if permission allows)
shift + zzequivalent to :wq!
shift + zqequivalent to :q!

Operator

.repeat last operation
fxfind 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:

yypcopy current line and insert after
diwdelete 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 + Vselect 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
ditdelete everything between tags. e.g. <xml>contenttodelete</xml>
dipdelete 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 .
iinsert at cursor location
shift + Imove cursor to first non-blank character of line and start in insert mode
ainsert at the location next to cursor
shift + Amove cursor to last non-blank character of line and start in insert mode
0move 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/lettuceFor every line of the file, replace the first occurrence of bacon in each line to lettuce
:%s/bacon/lettuce/gFor every line of the file, replace all occurrences of bacon to lettuce
:s/bacon/lettuceFor current line, replace the first occurrence of bacon to lettuce
:s/bacon/lettuce/giFor current line, replace all occurrences of bacon to lettuce, case insensitive
:g/bacon/ddelete all lines that contain pattern ‘bacon’
:g!/lettuce/ddelete all lines that do not contain pattern ‘lettice’; or use :v/lettuce/d instead
:g/^\s*$/ddelete all blank lines. \s* represents zero or more white spaces

Bookmarking

mamark cursor line as bookmark a
`ajump to cursor position at line a
‘ajump to beginning of line a
`.jump to last line where change occurred
jump back

Insert Mode

Ctrl + NAuto 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.

:termOpen up a terminal above vim. You can also spell :ter or :terminal
Ctrl+W; Ctrl+WPress Ctrl+W twice can help you toggle between the terminal and vim buffer
Ctrl+DClose 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.