【Unix】vi command summary

vi command summary

The following tables contain all the basic vi commands.(下表包含了所有基本的vi命令)

Starting vi(启动vi)

Command Description
vi file start at line 1 of file              光标在第一行
vi +n file start at line n of file                光标在第n行
vi file start at last line of file            光标在最后一行
vi +/pattern file start at pattern in file             光标在pattern行
vi -r file recover file after a system crash       系统down机后恢复文件

 

Saving files and quitting vi(保存和退出vi) 

Command Description
:e file edit file (save current file with :w first)        编辑file
:w save (write out) the file being edited              保存当前文件
:w file save as file                                保存为file
:w! file save as an existing file                       强制保存
:q quit vi                               退出
:wq save the file and quit vi                      保存并退出
:x save the file if it has changed and quit vi 如果文件被改动,则保存并退出。
:q! quit vi without saving changes                 放弃修改并退出

 

Moving the cursor(光标移动)

Keys pressed Effect
h left one character                        左移一个字符
l or <Space> right one character                        右移一个字符
k up one line                                上移一行
j or <Enter> down one line                               下移一行
b left one word                               左移一个字
w right one word                              右移一个字
( start of sentence                           到达一句的开头
) end of sentence                            到达一句的结尾
{ start of paragraph                          到达一段的开头
} end of paragraph                           到达一段的结尾
1G top of file                                 到达文件的开头
nG line n                                 到达第n行
G end of file                                 到达文件末尾
<Ctrl>W first character of insertion
<Ctrl>U up ½ screen                                 上滚半屏
<Ctrl>D down ½ screen                                下滚半屏
<Ctrl>B up one screen                               上滚一屏
<Ctrl>F down one screen                             下滚一屏

 

Inserting text(文字插入)

Keys pressed Text inserted
a after the cursor                            光标后插入
A after last character on the line            行尾插入
i before the cursor                          光标前插入
I before first character on the line          行首插入
o open line below current line                行下插入一行
O open line above current line                行上插入一行

 

Changing and replacing text

Keys pressed Text changed or replaced
cw word
3cw three words
cc current line
5cc five lines
r current character only
R current character and those to its right
s current character
S current line
~ switch between lowercase and uppercase

 

Deleting text(文本删除)

Keys pressed Text deleted
x character under cursor                       删除光标右侧文字
12x 12 characters                                删除12个字符
X character to left of cursor                  删除光标左侧字符
dw word                                         删除一个单词
3dw three words                                  删除三个单词
d0 to beginning of line                         删除至行首
d$ to end of line                               删除至行尾
dd current line                                 删除当前行
5dd five lines                                   删除五行
d{ to beginning of paragraph                    删除至段首
d} to end of paragraph                          删除至段尾
:1,. d to beginning of file                         删除至文件首
:.,$ d to end of file                              删除至文件尾
:1,$ d whole file                                   删除整个文件

 

Using markers and buffers

Command Description
mf set marker named ``f''
`f go to marker ``f''
´f go to start of line containing marker ``f''
"s12yy copy 12 lines into buffer ``s''
"ty} copy text from cursor to end of paragraph into buffer ``t''
"ly1G copy text from cursor to top of file into buffer ``l''
"kd`f cut text from cursor up to marker ``f'' into buffer ``k''
"kp paste buffer ``k'' into text

 

Searching for text(文本搜索)

Search Finds
/and next occurrence of ``and'', for example, ``and'', ``stand'', ``grand'' 
                                                      向下搜索文字and
?and

previous occurrence of ``and''  

                                                      向上搜索文字and

/^The

next line that starts with ``The'', for example, ``The'', ``Then'', ``There''

                                                      搜索下一个以the开头的行

/^The\>

next line that starts with the word ``The'' 

                                                      搜索下一个以整字The开头的行

/end$

next line that ends with ``end'' 

                                                      搜索下一个以end结尾的行

/[bB]ox

next occurrence of ``box'' or ``Box'' 

                                                      搜索box或者Box

n

repeat the most recent search, in the same direction 

                                                      在相同方向上继续搜索

N

repeat the most recent search, in the opposite direction 

                                                      在相反方向上继续搜索

 

Searching for and replacing text(搜索并替换文本)

Command Description
:s/pear/peach/g

replace all occurrences of ``pear'' with ``peach'' on current line 

                            在当前行搜索pear,并替换为peach

:/orange/s//lemon/g

change all occurrences of ``orange'' into ``lemon'' on next line containing ``orange'' 

                            在下一行中搜索orange,并替换为lemon

:.,$/\<file/directory/g replace all words starting with ``file'' by ``directory'' on every line from current line onward, for example, ``filename'' becomes ``directoryname'' 
:g/one/s//1/g

replace every occurrence of ``one'' with 1, for example, ``oneself'' becomes ``1self'', ``someone'' becomes ``some1''

                           用“1”替换所有的"one"

 

Matching patterns of text

expression_r_r_r_r_r_r Matches
. any single character
* zero or more of the previous expression_r_r_r_r_r_r
.* zero or more arbitrary characters
\< beginning of a word
\> end of a word
\ quote a special character
\* the character ``*''
^ beginning of a line
$ end of a line
[set] one character from a set of characters
[XYZ] one of the characters ``X'', ``Y'', or ``Z''
[[:upper:]][[:lower:]]* one uppercase character followed by any number of lowercase characters
[^set] one character not from a set of characters
[^XYZ[:digit:]] any character except ``X'', ``Y'', ``Z'', or a numeric digit

 

Options to the :set command

Option Effect
all list settings of all options
ignorecase ignore case in searches
list display <Tab> and end-of-line characters
mesg display messages sent to your terminal
nowrapscan prevent searches from wrapping round the end or beginning of a file
number display line numbers
report=5 warn if five or more lines are changed by command
term=ansi set terminal type to ``ansi''
terse shorten error messages
warn display ``[No write since last change]'' on shell escape if file has not been saved

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值