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
:
123
type the number of line you want to goEnter
to go
Search
Search words
/
word
type the “word” you want to findEnter
to start searchingn
/N
to 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
:
command
type the commandEnter
to 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
+v
enter block visual modej/k
to select linesI
to insert at the beginning//
to add charactersEsc
to finish
Uncomment
Ctrl
+v
enter block visual modej
/k
/h
/l
to select charactersx
to 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