Vim
1
The cursor is moved using either the arrow keys or the hjkl keys.
h (left) j (down) k (up) l (right)To delete the character at the cursor type: x
To insert or append text type:
i type inserted textinsert before the cursor
A type appended textappend after the line
2
To delete from the cursor up to the next word type: dw
To delete from the cursor up to the end of the word type: de
To delete from the cursor to the end of a line type: d$
To delete a whole line type: dd
To repeat a motion prepend it with a number: 2w
The format for a change command is:
operator [number] motion
where:
operator - is what to do, such as d for delete
[number] - is an optional count to repeat the motion
motion - moves over the text to operate on, such as w (word),
e (end of word), $ (end of the line), etc.To move to the start of the line use a zero: 0
To undo previous actions, type: u (lowercase u)
To undo the undo’s, type: CTRL-R
3
To put back text that has just been deleted, type p . This puts the
deleted text AFTER the cursor (if a line was deleted it will go on the
line below the cursor).To replace the character under the cursor, type r and then the
character you want to have there.The change operator allows you to change from the cursor to where the
motion takes you. eg. Type ce to change from the cursor to the end of
the word, c$ to change to the end of a line.The format for change is:
c [number] motion
4
G moves to the end of the file.
number G moves to that line number.
gg moves to the first line.Typing / followed by a phrase searches FORWARD for the phrase.
Typing ? followed by a phrase searches BACKWARD for the phrase.
After a search type n to find the next occurrence in the same direction
or N to search in the opposite direction.Typing % while the cursor is on a (,),[,],{, or } goes to its match.
To substitute new for the first old in a line type :s/old/new
To substitute new for all ‘old’s on a line type :s/old/new/g
To substitute phrases between two line #’s type :#,#s/old/new/g
To substitute all occurrences in the file type :%s/old/new/g
To ask for confirmation each time add ‘c’ :%s/old/new/gc
5
Type o to open a line BELOW the cursor and start Insert mode.
Type O to open a line ABOVE the cursor.Type a to insert text AFTER the cursor.
Type A to insert text after the end of the line.The e command moves to the end of a word.
The y operator yanks (copies) text, p puts (pastes) it.
Typing a capital R enters Replace mode until
is pressed. Typing “:set xxx” sets the option “xxx”. Some options are:
‘ic’ ‘ignorecase’ ignore upper/lower case when searching
‘is’ ‘incsearch’ show partial matches for a search phrase
‘hls’ ‘hlsearch’ highlight all matching phrases
You can either use the long or the short option name.Prepend “no” to switch an option off: :set noic
![Advanced Vim](/img/Advanced vim.png)
tips
Basic
1.命令模式
2.输入模式
3.底线命令模式
:wq save and quit
:w filename ave as other file
:set nu line number
:set nonu
i/I 输入字符
a/A 输入字符
o/O 新起一行
r/R 替换字符
移动光标操作
移动光标 hjkl/⬅️⬇️⬆️➡️
G 移动到最后一行
gg 移动到第一行
0 移动到行首
$ 移动到行尾
nG n为数字,移动到第n行(配合:set nu显示行号)
n
ctr+f page down
ctr+b page up
ctr+d half page down
ctr+u half page up
文本操作
dd 删除当前行
ndd n为数字,删除n行
x delete
X Backspace
nx/nX 连续删除n个字符
yy 复制当前行
nyy n为数字,复制n行
p/P 粘贴复制的内容
v/V/Ctr+v 可视化模式
/word 搜索关键词,n下N上,从当前光标往下搜索(:set hlsearch 高亮显示)
?word 搜索关键词,从当前光标往上搜索
u,Ctr+r u撤销,ctr+r重做
:n1,n2s/wd1/wd2/g 搜索替换关键词 【eg. :1,$s/wd1/wd2/g :1,$s/wd1/wd2/gc】