Vim command summary

From Wattanit's Wiki
Jump to: navigation, search

This is a list of the most useful commands for Vim in my (Wattanit) own opinion. This list is by no means conclusive and there are much more commands to learn in Vim. Nevertheless, this list is enough to work with Vim smoothly and efficiently.

Some command in this list is denoted as an (x), which is my own customised commands defined in my vimrc file.

Open and Save[edit]

:e -- open a blank buffer
:e <filename> or :edit <filename> -- open new file 
:w -- save current file
:w <filename> -- save as a new file
:q -- exit Vim
:q! -- exit without saving
:x -- save then exit


h -- move cursor left one character
j -- move cursor down one line
k -- move cursor up one line
l -- move cursor right one character
0 or (H) -- move cursor to the beginning of the line
$ or (L) -- move cursor to the end of the line
gg -- move cursor to the top of the file
G -- move cursor to the bottom of the file
w -- move cursor to the beginning of the next word
e -- move cursor to the end of the current word
b -- move cursor to the beginning of the current word
:<line number> -- go to <line number>


i -- enter Insert Mode where the cursor is (insert)
I -- enter Insert Mode at the beginning of the line
a -- enter Insert Mode after the cursor (append)
A -- enter Insert Mode at the end of the line
o -- create a new line after the current line then enter Insert Mode
O -- create a new line before the current line then enter Insert Mode
r -- replace a character under cursor
d<navigation> -- delete (use with navigation commands
dd -- delete from cursor to the end of line
u -- undo (Very important!)
y<navigation> -- yank (copy) (use with navigation commands
yy -- yank a line
p -- paste a yanked or deleted text

Remember that edit commands and navigation commands can be combined with numbers. For examples, d5j means delete the next 5 lines down, 30l means move the cursor right 30 characters, and 10i means insert something ten times. (You need to try this out to see for yourself.)

Split views and Buffer[edit]

:b <buffer name/number>-- switch buffer
:b or :lsbuffers -- shows the list of open buffers
:bn or :bnext -- switch to the next buffer
:bp or :bprevious -- switch to the previous buffer
:bd <buffer name/number> or :bdelete <buffer name/number> -- delete (close) a buffer
:sp or :split -- split view horizontally 
:vsp or :vsplit -- split view vertically


/<pattern> -- search using regular expression pattern
n -- next occurance
N -- previous occurance


q<macro id> -- start recording a macro. Macro id can be from a to z. Press q again to stop recording.
@<macro id> -- execute a macro

Custom functions[edit]

(<ladder>n) -- toggle between absolute line number and relative line number