This is a cheatsheet for vim commands and operations.
Configuration
Display line numbers
- in the current file
:set number # to enable line numbers
:set nonumber # to disable line numbers
- to change config
add set number in ~/.vimrc
set mouse mode on
add set mouse=a to ~/.vimrc
set tab size
add set tabstop=4
Navigating
Basic movement
-
hmove to the left by one position -
lmove to the right by one position -
jmove to the downward direction by one line -
kmove to the upward direction by one line
Inline movement
-
bmove to the start of the current word -
emove to the end of the current word -
wmove to the start of the next word
Info: combine movement with a number, e.g. 3w, 9k is the same as pressing w three times, pressing k nine times, respectively.
-
^move to the first non-blank character -
g_move to the last non-blank character -
n + Spacemove to the nth character of the current line -
0move to the start column of the current line -
$move to the end column of the current line
Text movement
-
+move to the first non-blank character of the next line -
-move to the first non-blank character of the previous line -
:njump to the nth line -
:+norn + Enterjump down n lines -
:-njump up n lines -
ggor:0move to the first line of the file -
Gor:$move to the end line of the file
Info: combine movement with a number, e.g. 3G move to the third line of the file
screen movement
-
ctrl + escroll down a line -
ctrl + yscroll up a line -
ctrl + dscroll down the half of page -
ctrl + uscroll up the half page -
ctrl + forPage downscroll down the entire page -
ctrl + borPage upscroll up the entire page -
ctrl + ojump to the previous position -
ctrl + ijump to the next position
Editing
Insert
-
iinsert the text before the cursor -
Iinsert the text at the beginning of the line
Open a new line
-
oopen new line below the cursor -
Oopen new line above the cursor
Append
-
aappend the text after the cursor -
Aappend the text at the end of line
Delete
-
xdelete the character on the cursor -
Xdelete the character before the cursor -
dwdelete a word beginning at the cursor -
d0delete from the beginning of current line to the cursor position (including current character) -
d$delete from current position to the end of the line (including current character) -
Ddelete entire line beginning at the cursor -
dddelete entire line, -
d1Gdelete from the first line to the current line (including current line) -
dGdelete from the current line to the end (including current line)
Info: combine movement with a number, e.g. 3dw delete 3 words at a time, 4D delete 4 lines at a time
Mention: all of these commands is will ‘cut’ the text to the clipboard, so the following p will works!
Delete and insert
-
sdelete the character under the cursor and switch to insert mode -
Sdelete the whole line and switch to insert mode -
Cdelete the following text after the cursor and switch to insert mode
Replace
-
rreplace the character on the cursor but not switch to insert mode -
Rreplace the following text
Copy & Paste
-
ycopy a single character on the cursor -
yycopy entire line -
ppaste text after the cursor -
Ppaste text before the cursor
Info: combine movement with a number, e.g. 3yy copy the following three lines including the current line
Join lines
Jremove theline breakstowhitespace
Info: combine inserting with a number, e.g. 5igo + Esc wil insert ‘go’ five times
Undo & Redo
uundo single action
Info: combine inserting with a number, e.g. 3u will undo action five times
ctrl + rredo single action
Searching
Inline searching
-
f<char>jump to the next occurrence of<char> -
t<char>jump before the next occurrence of<char>
e.g. delete until specified char: dt" delete text until "
Global searching
-
*search the next occurrence of the word cursor on -
#search the previous occurrence of the word cursor on -
/<expression>search the expression in forward direction -
?<expression>search the expression in backward direction -
nfind the next/previous occurence in/<expression>/?<expression> -
Nfind the previous/next occurence in/<expression>/?<expression> -
//repeat the previous searching
Advance
Replace
-
:<line1>,<line2>s/<word1>/<word2>/greplaceword1withword2betweenline1andline2, flaggrefers to global -
:%s/<word1>/<word2>/gcreplaceword1withword2to the whole text,%means for every line, flaggcrefers to global and need confirmation -
ddpswap the current line to the next one
Visual mode
vswitch to visual mode
delete the whole word: v for visual mode, e jump to the end of the current word, d to delete
Visual block mode
ctrl + vswitch to visual block mode
Comment quickly: ctrl + v for block mode, select some lines, I to insect at the beginning of each line, input the annotation symbol, Esc twice finally comment several lines quickly
-
:10,20s#^#//#gcomment//from line 10 to line 20 -
:10,20s#^//##guncomment//from line 10 to line 20 -
:10,20s/^/#/gcomment#from line 10 to line 20 -
:10,20s/^#//guncomment#from line 10 to line 20 -
:%s/$/\r/gadd a newline to each line
Others
:! <command>run the command and show the output outside of vim environment