>

Insert/Change

Keys Function
a append after cursor
A append at the end of line
i insert before the cursor
I insert before the first non-blank character of line
o/O open a new line below/above the current.
C, c$ change to the end of line
c^ change to the first non-blank character of line
s, cl change one character
S, cc change a whole line
ct change to char, not include char
r replace current char, e.g ra will replace char under cursor to character ‘a’
R Enter insert mode, replacing characters(starting from cursor) instead of inserting
vu/vU uppercase/lowercase the current char
viu/viU uppercase/lowercase the current word
~ Switch case of character under cursor
g, gg~ Switch case of whole line
gUU/guu Change whole line to uppercase/lowercase
gUw/guw Change word to uppercase/lowercase
J Join Lines without space
gJ Join Lines with space

Delete

Keys Function
x, DEL delete character under cursor
X delete character before cursor
nx/nX delete forward/backword n characters
dl/dh delete character under/after cursor, l means forward, h means backward
dk/dj delete sentence above/below
dnl/dnh delete forward/backword n characters
df, dF delete forward/backward to char, but include char
dt, dt delete forward/backward to char, but exclude char
D delete to the end of line
dw delete to the end of current word (dw, 3dw, d3w, 3d2w,i d$, d^, df)
dW delete to the end of current space separated word
diw delete current word (excluding whitespace)
daw delete current word (including whitespace)
dG/dgg delete until the end/begining of file
dd delete current line
dnj delete below n lines
dnk delete above n lines

Note
In keys, n represents count.
d{motion} : delete text that {motion} moves over. e.g: dl delete current char, dh delete before char

Copy/Yank

Keys Function
ynw yank n words
yy, Y yank whole line
ynl yank n characters
y$ yank to the end of the line
nyy yank n lines

Undo/Redo

Keys Function
u undo, in Normal mode
U undo all changes in current line
Ctrl-r redo
:earlier 4m – step back 4 minutes
:later 55s – step forward 55 seconds

Fold, Indent

Keys Function
zf fold
zo unfold
zc close
== Fix line indent
VG= Indent the whole file
=% Indent the code between parenthesis
ctrl-t, ctrl-d Indent/un-indent in insert mode

Note
:set autoindent Turn on auto-indent
:set smartindent Turn on intelligent auto-indent
:set shiftwidth=4 Defines 4 spaces as indent size

File operations

commands Function
:e [file] edit file or current file if file is not specified
:e! [file] edit file or current file if file is not specified, discard all changes
:r [file] insert file into current cursor position
:w Save file
:q Exit Vim
:q! Quit without saving
:x Save and quit
:sav filename Saves file as filename
. Repeats the last change made in normal mode
5. Repeats 5 times the last change made in normal mode
ZZ Save and quit
:1,10 w outfile Saves lines 1 to 10 in outfile
:1,10 w >> outfile Appends lines 1 to 10 to outfile
:r infile Insert the content of infile
:23r infile Insert the content of infile under line 23

File Explorer

Commands Function
:e . Open integrated file explorer
:Sex Split window and open integrated file explorer
:Sex! Same as :Sex but split window vertically
:browse e Graphical file explorer
:ls List buffers
:cd .. Move to parent directory
:args List files
:args *.txt Open file list
:grep expression *.cpp Returns a list of .cpp files contening expression
gf Open file name under cursor

Interaction with Unix/Linux commands

Commands Function
:!pwd Execute the pwd unix command, then returns to Vi
!!pwd Execute the pwd unix command and insert output in file
:sh Temporary returns to Unix
$exit Retourns to Vi

Auto completion

Keys Function
Ctrl+n, Ctrl+p (in insert mode) Complete word
Ctrl+x, Ctrl+l Complete line
:set dictionary=dict Define dict as a dictionnary
Ctrl+x Ctrl+k Complete with dictionnary

1) File encription

Commands Function
vim -x file start to edit a encripted file
:X – Set a password for current file
:set key= – remove password of current file

2) File encoding

Commands Function
:e ++enc=utf8 filename open file using utf-8 encoding
:w ++enc=gbk convert to gbk encoding regardless of the encoding of the file
:set fenc checking encoding of current file
:set fileencoding same as above

Note
If we set fileencoding=ucs-bom,utf-8,cp936 in .vimrc, vim will open file according to the appropriate encoding, note that there are no spaces between each encodings.
cp936 is for gbk, ucs-bom is the encoding format in windows.

3) File format
Noteable there are mainly three type of files: dos, unix, mac.
Their major differences are the representations of line ending

format symbols desc
dos \r\n carriage return (CR) + line feed (LF)
unix \n line feed (LF)
mac \r carriage return (CR)
  • :e ++ff=dos filename open file in dos format
  • :w ++ff=mac filename open file in mac format
  • :set ff display format of current file

Add set fileformats=unix,dos,mac in .vimrc so that vim can detect file format automatically.

Reference
http://linux.chinaunix.net/techdoc/desktop/2009/07/06/1122020.shtml
http://blog.csdn.net/scaleqiao/article/details/45153379