Vim Hot Keys

The most powerful editor

Posted by CodingMrWang on July 15, 2018

This post is first created by CodingMrWang, 作者 @Zexian Wang ,please keep the original link if you want to repost it.

Vim

Vim is a really powerful text editor, when I had an internship in ByteDance, my leader kept encouraging me to use vim and stop using pycharm. So I decided to conclude those hot keys in vim and kept practicing it. This do speed up the workflow. All commands are searched from many blogs and offical website.

Move Inside the whole program

k              move up one line
j              move down one line
h              move left one line
l              move right one line

move in a larger range

crtl+f         page down
crtl+b         page up
(              move to the start of current sentense
)              move to the start of next sentence
{              move to the start of current paragraph
}              move to the start of next paragraph
gg             move to the start of first line of the program
G              move to the start of last line of the program
NG or Ngg      move to the start of Nth line of the program
H              move to the first line of the screen
M              move to the middle of the screen
L              move to the end of the screen
*              search current word

Move inside in one line

w              move to the start of next word
e              move to the end of current word
b              move to the start of the word before
0              move to the start of current line
^              move to the first none empty word
$              move to the end of current line
g_             move to the last non empty word
fx             move to the first x after current position
Nfx            move the the Nth x after current position
tx             move to the word begfore x
F and T        the opposite direction to f and t

Search and match

/word          seach for the word in the program
n              go to next match word
N              go to last match word

Replace and delete

rx             replace current letter with x
nrx            replace n letters after with x
x              remove current letter
nx             delete n letters after
dw             delete word on the right
ndw            delete n words on the right
db             delete word on the left
ndb            delete n words on the left
dd             delete current line and remove empty
ndd            delete and copy n lines
:s/old/new     replce first old with new in current line
:s/old/new/g   replace all old with new in current line
:%s/old/new/g  replace all old with new
:n1,n2s/old/new/g replace all old with new for n1 to n2 lines
:%s/^/xxx/g    add xxx to all lines head
:%s/$/xxx/g    add xxx to all lines tails
:%s/old/new/gc replace all old with new but need verification

Edit

c is similar to d, the only difference is that c will become insrt mode after deletion.

s             delete current letter and start insert
ns            delete n letter to the right and start insert
S             delete current line and start insert
nS            delete n lines and start insert
cw            delete the word and start insert
cW or c$      delete the line to the right and start insert
ncw           delete n words to the right and start insert
cb            delete word to the left and start insert
ncb           delete n words to the left and start insert
cd            delete current line and start insert
ncd           delete n lines and start insert
c$            delete to the end of the line and start insert
c0            delete to the begin and start insert

Copy and Paste

when you delete some words, they are actually saved to buffer

p            paste the content in buffer after the cursor
P            paste the content in the buffer before the cursor
yy           copy current line
nyy          copy n lines
"+yy         copy current line to system paste board
"+nyy        copy n lines to system paste board 

Cancel and Repeat

u            cancel last command
.            repeat last command
:undo n      go back to n changes ago
:undolist    get undo list
U            cancel all changes
:earlier 4m  go back to 4 minutes ago

Insert

i            insert from left side
a            insert from right side
o            insert from next line
O            insert from last line
I            insert from start of the line
A            insert from end of the line

Command mode

:e path_to_file/filename    open another file
:w            save modified file
:w temp_file  save to another file
:q            quit
:q            force quit
:wq           save and quit
:nw file      save nth line to a file
:n1,n2w file  save n1 to n2 line to a file
:n1,.w file   save n1 to current line to a file
:.,$w file    save current line to last line to a file
:.,.+n file   save from current line to next n lines to a file
:1,$w file.   save all lines to a file
:r file       read file and write the file after current line
:f file       rename current file

Split

:sp           horizontal split
:vsp          vertical split
ctrl+w+h/j/k/l move to left, down, up and right split
ctrl+w +      increase height
ctrl+w -      decrease height

Some other useful hot keys

g;:          jump to the last modified place
g,;          jump to next modified place
%            match {[(
~            change capital

Use Ctags in Vim

ctags -R -f xx_tagFileName ./
:set tags=....../tags_glibc

vim command

Ctrl + T          jump to last tag
Ctrl + O	        jump to last position
Ctrl + ]          jump to the definition of function

Enjoy Vim!!!