×

Loading...

给大家贴一点实质性的东西,以下是我以前总结出来在公司内部贴的经验文档,介绍VI操作的,基本足够了

本文发表在 rolia.net 枫下论坛Vi命令

1、Vi有3个状态:操作状态、编辑状态和命令行状态
2、^f 表示CTRL-f

1 操作状态下命令:
1.1 光标移动类命令
0 Move to beginning of current line
^ Move to the first character of current line
$ Move to end of current line
h Move left a cursor
H Move to beginning of current page
l Move right a cursor
L Move to end of current page
j Move down a cursor
k Move up a cursor
G Move to end of file
]] Ditto
[[ Move to beginning of file
w move to the beginning of next word
W move to the beginning of next word,ignore interpunction
b move to the beginning of Last word
B move to the beginning of Last word,ignore interpunction
^f PageDown
^b PageUp
^d 1/2 PageDown
^u 1/2 PageUp
{ Move to the last space line
} Move to the next space line
( Move to the last different line (space line <-> line with words)
) Move to the next different line (space line <-> line with words)
- Move to the beginning of last line
+ Move to the beginning of next line
M Move to center line of current page
% Goto the matched bracket ('(',')','[',']','{','}')
fx Move forword to 'x' in current line
Fx Move back to 'x' in current line

1.2 编辑命令:
x Delete current char
nx Delete n characters
dd Delete current line
ndd Delete n lines
dG Delete to end of file
D Delete to end of current line
d$ Ditto
d0 From beginning delete to current cursor
d^ From first character delete to current cursor
dw Delete a word
dW Delete a word,ignore interpunction
(All delete command will save the what deleted into default buffer)
yy Copy current line
xyy Copy x lines
y$ Copy to end of line
y0 From beginning of current line copy to cursor
y^ From first charcter of current line copy to cursor
yw Copy a word
yW Copy a word,ignore interpunction
(All copy command copy to default buffer)
p paste after current cursor or current line
P paste before current cursor or current line
J Link following line to current line
rx use 'x' to replace current char
>> Move current line right a table key
<< Move current line left a table key
>% Move the lines between current bracket and the matched one right a table key
<% Move the lines between current bracket and the matched one left a table key
u undo last modify
U undo all modify of current line
~ Reverse the case of current character

1.3 状态迁移类命令
The following key will transfer vi state to edit mode
i Insert before cursor
I Insert before current line
a Insert after cursor
A Insert after current line
o Make a new line after current line
O Make a new line Before current line
s Into insert mode to replace current character
S Delete current line and Into insert mode
cc Ditto
cw Into insert mode to replace current word
C Into Insert mode to replace current line
c$ Ditto
c0 Into insert mode to replace beginning of line to current character
c^ Into insert mode to replace first character of line to current character
R Into modify mode

: Into command state,begin to get some command
/ Into command state,get search command
? Into command state,get search command

1.4 其他命令:
n Continue last search
N Continue last search in the reverse way
. re-do last command
^g Show brief of file in command line
^l refresh screen
"a Register a named buffer with name 'a'; The following command will execute with certain buffer

2 编辑状态下命令:
x Insert character 'x' to current file
ESC Back to control state
^c Back to control state
^h backspace
^j enter
^w Delete the fore-words


3 命令行状态命令:
r file Readin file and insert after current line
r !command Readin the output of shell command "command" and insert after current line
nr file Readin file and insert after the No.n line
!command Execute the shell command "command" and return
sh Transfer into shell command state, can use "exit" to return
w Save current file
wq Save and quit vi
x Ditto
q Quit
q! Quit ignore the modified of current file
e file Edit file
e! Ignore the modified of current file and reload this for edit
n Begin to edit next file
map n command Declare 'n' as a macro of command
unmap n Cancel the declare of macro 'n'
set env Set environ
set all Get brief help of 'set'
/ Search forward
? Search backward
g Global
s Replace
l1,l2command Excute command from line l1 to line l2


4 一些命令的详细介绍和应用举例

4.1 命名缓冲区的应用:
"q10dd Delete 10 lines and save them into buffer 'q'
"qp Paste from buffer 'q'
You can use named buffer to save something you usually used.

4.2 搜索的应用
/abcd Search "abcd" and skip to the next "abcd"
/[aA]bcd Search "abcd" or "Abcd" and skip to the next matched
/[aA][0-9].cd Search "anxcd" or "Axcd" and skip to the next matched , Here, n is a digital and x is any character

4.3 搜索替换的组合应用
base formate : /templet1/s/templet2/templet3
Search the matched line with templet1 and replace the templet2 with templet3
/abcd/s//dcba Search "abcd" and replace the first matched with "dcba", without give the templet2, default is templet1
g/abcd/s//dcba Search "abcd" and replace the first matched in every line with "dcba"
g/abcd/s//dcba/g Search "abcd" and replace all matched in file with "dcba"
g/abcd/s/123/321/g Replace all "123" with "321" in those line with "abcd"
10,30/abcd/s//dcba/g Replace all "123" with "321" between lines 10 to 30
g/\(abcd\)\(efg\)/s//\2---\1/g Replace all "abcdefg" wirh "efg---abcd"
说明:'/'为转义符,模板一中用括号将"abcdefg"分为两个子串"abcd"和"efg",缺省就是串1和串2,在替换模板中,用'/'分别引用串2和串1,中间加上了"---",达到以上目的

4.4 宏的应用
map ` /[{}]^Mi^M^[a^M^[ Search '{' or '}' and let it to occupy a single line. the command is :
/[{}]^M Search '{' or '}', follow an Enter (In command line use ctrl-v + Enter to input Enter key, ctrl-v is a Transfer key)
i^M^[ Insert Enter before '{' or '}', follow an Escape. (ctrl-v + Esc)
a^M^[ Insert Enter after '{' or '}', follow an Escape. (ctrl-v + Esc)

最常用set命令
set nu Show line number
set nonu Unshow line number
set tab=n Set table width n spaces
set ai Set autoindent
set noai Cancel autoindent
set all Get brief help of set更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report