vi(vim是其增强款)是一款功能强大的文本编辑器,它朴实无华,所有的文本编辑任务都可以用键盘完成,根本不依赖鼠标。刚开始用vim时你可能有些不习惯,但是一旦你熟悉了它的众多快捷键,你就会爱上它们,再也离不开了!
在开始正文之前,先分享一个在线玩游戏学vim的网站Vim Adventures,你可以边玩边学。
基本技巧
:q
离开vi编辑器
:w
存档
-
vi三种模式
command mode
insert mode
ex mode(extended mode)
command mode
按 A键进入insert mode
insert mode
按 <Esc>键返回command mode
ex mode
-
光标移动
h-left
j-down
k-up
l-right
arrow keys also work
w-word ahead
b-word back
(-sentence back
)-sentence forward
{-paragraph above
}-paragraph below
-
进入insert mode
a append after the cursor
i insert before the cursor
A append to the end of line
I insert at beginning of line
o open a line below
O open a line above
-
change/delete/yank(copy) : command code
change delete yank(copy)
line cc dd yy
letter cl dl yl
word cw dw yw
- put(paste) : command mode
use p or P to put changed, deleted or copied data
for line oriented data
p puts the data below the current line
P puts the data above the current line
for character oriented data
p puts the data after the cursor
P puts the data before the cursor
- 回复undo
u
undo most recent change
<ctrl-r>
redo last "undone" change
U
undo all changes to the current line
- searching for text : command mode
/text
search downwards for "text"
?text
search upwards for "text"
n
continue search in the same direction
N
continue search in the opposite direction
- command-mode tricks
dtc
delete from cursor to the letter c
rc
replace a character with c
x
delete a character
5dd
delete 5 lines
5yy
copied 5 lines
5x
delete 5 characters
R
replace character-for-character until <Esc>
replace mode
- saving and quitting : ex mode
:w
saving(writing)
:q
quitting
:wq
saving and quitting
!
forcing
:w!
forcing save
:q!
forcing quit
:wq!
forcing save and quit
高阶技巧
- file repositioning
G
go to last line in file
1G
go to first line in file
<ctrl-d>
go down half a screen
<ctrl-u>
go up half a screen
- screen repositioning
H
M
L
z+<Enter>
make current line the first line on screen
z+-
make current line the last line on screen
- filering
!!
the output of a command can be placed in the file
!}
the data in the file can be used as input
- advanced reading and saving
:r file1
read the file1 into current file
:1,20w file2
write lines 1 through 20 to the file2
:1,$w file3
write entire file to the file3
:1,20w >> file4
append lines 1 through 20 to the file4
:n
move to the next file
:rew
rewind to the first file
:n#
jump to the previous file, thus toggling between two files
- 设置vi
set
set all
set ignorecase
最后
当然,还要提一下我新近学的一个标记功能,可以快速切换到文章中的不同位置:
m+<a>。即按下m和a键
mark one line with label a
'+<a>
goto the marked line labeled by a
(公众号:生信了)
image
网友评论