Vi Skills
This article contains some useful skills in Vi/Vim.
Basic Commands
Move
Basic
| key | function |
|---|---|
h, left |
go left one character |
j, down |
go down one line |
k, up |
go up one line |
l, right |
go right one character |
e, E |
go to end of word, WORD |
w, W |
go to begin of next word, WORD |
b, B |
go to previous word, WORD |
Go to line
:123type the number of line you want to goEnterto go
Search
Search words
/wordtype the “word” you want to findEnterto start searchingn/Nto search next/prev
Search current word
- move cursor to the target word
#/*to find prev/next
Modification
| command | function |
|---|---|
yy |
copy current line |
yw |
copy current word from cursor |
p |
paste after cursor/line |
u |
undo |
dd |
delete current line |
x |
delete current character |
Enter insert mode
| key | function |
|---|---|
i |
enter insert mode before the cursor |
I |
enter insert mode at the start of line |
a |
enter insert mode after the cursor |
A |
enter insert mode at the end of line |
o |
enter insert mode at a new line up |
O |
enter insert mode at a new line down |
File operation
:commandtype the commandEnterto go
| command | function |
|---|---|
w |
save |
q |
quit |
wq |
save & quit |
q! |
don’t save & quit |
e |
refresh file |
e! |
discard change and refresh file |
Some Skills
Comment / Uncomment
Comment
Ctrl+venter block visual modej/kto select linesIto insert at the beginning//to add charactersEscto finish
Uncomment
Ctrl+venter block visual modej/k/h/lto select charactersxto delete
Autofill
Ctrl + p
Replace
Replace all xx to yy in this file. Confirmation required.
:%s/xx/yy/gc
Replace xx to yy between line 10 to 100. Confirmation not required.
:10:100s/xx/yy/g
Reference
