vi编辑器命令集合

2016-07-18 14:41:40 7672

基础

:e filenameOpen filename for edition
:wSave file
:qExit Vim
:q!Quit without saving
:xWrite file (if changes has been made) and exit
:sav filenameSaves file as filename
.Repeats the last change made in normal mode
5.Repeats 5 times the last change made in normal mode

在文件中移动

k or Up Arrowmove the cursor up one line
j or Down Arrowmove the cursor down one line
emove the cursor to the end of the word
bmove the cursor to the begining of the word
0move the cursor to the begining of the line
Gmove the cursor to the end of the line
ggmove the cursor to the begining of the file
Lmove the cursor to the end of the file
:59move cursor to line 59. Replace 59 by the desired line number.
20|move cursor to column 20.
%Move cursor to matching parenthesis
[[Jump to function start
[{Jump to block start

剪切、复制和粘贴

yCopy the selected text to clipboard
pPaste clipboard contents
ddCut current line
yyCopy current line
y$Copy to end of line
DCut to end of line

搜索

/wordSearch word from top to bottom
?wordSearch word from bottom to top
*Search the word under cursor
/cstringSearch STRING or string, case insensitive
/jo[ha]nSearch john or joan
/< theSearch the, theatre or then
/the>Search the or breathe
/< the>Search the
/< ?.>Search all words of 4 letters
//Search fred but not alfred or frederick
/fred|joeSearch fred or joe
/<dddd>Search exactly 4 digits
/^ {3}Find 3 empty lines
:bufdo /searchstr/Search in all open files
bufdo %s/something/somethingelse/gSearch something in all the open buffers and replace it with somethingelse

替换

:%s/old/new/greplace all occurences of old by new in file
:%s/onward/forward/gireplace onward by forward, case unsensitive
:%s/old/new/gcreplace all occurences with confirmation
:2,35s/old/new/greplace all occurences between lines 2 and 35
:5,$s/old/new/greplace all occurences from line 5 to EOF
:%s/^/hello/greplace the begining of each line by hello
:%s/$/Harry/greplace the end of each line by Harry
:%s/onward/forward/giReplace onward by forward, case unsensitive
:%s/ *$//gdelete all white spaces
:g/string/ddelete all lines containing string
:v/string/ddelete all lines containing which didn’t contain string
:s/Bill/Steve/replace the first occurence of Bill by Steve in current line
:s/Bill/Steve/gReplace Bill by Steve in current line
:%s/Bill/Steve/gReplace Bill by Steve in all the file
:%s/^M//gdelete DOS carriage returns (^M)
:%s/ / /gTransform DOS carriage returns in returns
:%s#<[^>]+>##gdelete HTML tags but keeps text
:%s/^(.*) 1$/1/delete lines which appears twice
Ctrl+aIncrement number under the cursor
Ctrl+xDecrement number under cursor
ggVGg?Change text to Rot13

大小写

VuLowercase line
VUUppercase line
g~~Invert case
vEUSwitch word to uppercase
vE~Modify word case
ggguGSet all text to lowercase
gggUGSet all text to uppercase
:set ignorecaseIgnore case in searches
:set smartcaseIgnore case in searches excepted if an uppercase letter is used
:%s/<./u&/gSets first letter of each word to uppercase
:%s/<./l&/gSets first letter of each word to lowercase
:%s/.*/u&Sets first letter of each line to uppercase
:%s/.*/l&Sets first letter of each line to lowercase

读写文件

:1,10 w outfileSaves lines 1 to 10 in outfile
:1,10 w >> outfileAppends lines 1 to 10 to outfile
:r infileInsert the content of infile
:23r infileInsert the content of infile under line 23

文件浏览器

:e .Open integrated file explorer
:SexSplit window and open integrated file explorer
:Sex!Same as :Sex but split window vertically
:browse eGraphical file explorer
:lsList buffers
:cd ..Move to parent directory
:argsList files
:args *.phpOpen file list
:grep expression *.phpReturns a list of .php files contening expression
gfOpen file name under cursor

和 Unix 系统交互

:!pwdExecute the pwd unix command, then returns to Vi
!!pwdExecute the pwd unix command and insert output in file
:shTemporary returns to Unix
$exitRetourns to Vi

对齐

:%!fmtAlign all lines
!}fmtAlign all lines at the current position
5!!fmtAlign the next 5 lines

Tabs/Windows

:tabnewCreates a new tab
gtShow next tab
:tabfirstShow first tab
:tablastShow last tab
:tabm n(position)Rearrange tabs
:tabdo %s/foo/bar/gExecute a command in all tabs
:tab ballPuts all open files in tabs
:new abc.txtEdit abc.txt in new window

分屏显示

:e filenameEdit filename in current window
:split filenameSplit the window and open filename
ctrl-w up arrowPuts cursor in top window
ctrl-w ctrl-wPuts cursor in next window
ctrl-w_Maximize current window vertically
ctrl-w|Maximize current window horizontally
ctrl-w=Gives the same size to all windows
10 ctrl-w+Add 10 lines to current window
:vsplit fileSplit window vertically
:sview fileSame as :split in readonly mode
:hideClose current window
:?nlyClose all windows, excepted current
:b 2Open #2 in this window

自动完成

Ctrl+n Ctrl+p (in insert mode)Complete word
Ctrl+x Ctrl+lComplete line
:set dictionary=dictDefine dict as a dictionnary
Ctrl+x Ctrl+kComplete with dictionnary

Marks

m {a-z}Marks current position as {a-z}
' {a-z}Move to position {a-z}
''Move to previous position

缩写

:ab mail mail@provider.orgDefine mail as abbreviation of mail@provider.org

文本缩进

:set autoindentTurn on auto-indent
:set smartindentTurn on intelligent auto-indent
:set shiftwidth=4Defines 4 spaces as indent size
ctrl-t, ctrl-dIndent/un-indent in insert mode
>>Indent
<<Un-indent
=%Indent the code between parenthesis
1GVG=Indent the whole file

语法高亮

:syntax onTurn on syntax highlighting
:syntax offTurn off syntax highlighting
:set syntax=perlForce syntax highlighting

三.命令行模式操作

1.进入插入模式

按“i”:从光标当前位置开始输入文件。
按“I”:在光标所在行的行首插入。
按“a”:从目前光标所在位置的下一个位置开始输入文字。
按“A”:在光标所在行的行末插入。
按“o”:在下面插入一行
按“O”:在上面插入一行。
按“s”:删除光标后的一个字符,然后进入插入模式。
按“S”:删除光标所在的行,然后进入插入模式。

3.移动光标

“h”、“j”、“k”、“l”分别控制光标左、下、上、右移一格。


“w”:光标跳到下个字的开头。
“e”:光标跳到下个字的字尾。
“b”:光标回到上个字的开头。
“nl”:光标移动该行的第n个位置,例如:“5l”表示移动到该行的第5个字符。

“ctrl+b”:屏幕往后移动一页。
“ctrl+f”:屏幕往前移动一页。
“ctrl+u”:屏幕往后移动半页。
“ctrl+d”:屏幕往前移动半页。
—数字“0”:移动到文本的开头。
           “G”:移动到文件的最后。
—“$”:移动到光标所在行的行尾
    “^”:移动到光标所在行的行首

4.删除文字

“x”:每按一次,删除光?所在位置的后面一个字符。
“nx”:例如:“6x”表示删除光标所在位置后面6个字符。
“X”:每按一次,删除光标所在位置的前面一个字符。
“dd”:删除光标所在行。
“ndd”:从光标所在行开始删除n行。例如:“4dd”表示删除从光标所在行开始的4行字符。

5.复制

“yw”:将光标所在之处到字尾的字符复制到缓冲区中。
“nyw”:复制n个字到缓冲区。
“yy”:复制光标所在行到缓冲区。
“nyy”:例如:“6yy”表示复制从光标所在行开始6行字符。
“p”:将缓冲区内的字符写到光标所在位置。

8.更改

“cw”:更改光标所在处的字到字尾处。
“cnw”:例如:“c3w”表示更改3个字。

9.跳至指定的行

“ctrl+g”:列出光标所在行的行号。
“nG”:例如:“15G”,表示移动光标到该文件的第15行行首。

其他:

“ZZ”:存盘退出
“ZQ”:不存盘退出
“r”:替换光标所在处的字符。
“R”:替换光标所到处的字符,直到按下“ESC”键为止。
“u”:撤销上一次操作

四.末行模式操作

在使用末行模式之前,请记住先按“ESC”键确定已经处于命令行模式后,再按冒号“:”即可进入末行模式。

“set nu”:列出行号
“set nonu”:取消列出行号
“set ic”:搜索时忽略大小写
“set noic”:取消在搜索时忽略大小写。
“n”:跳到文件中的某一行,“n”表示一个数字,如输入数字15,再回车就会跳到文本的第15行。
“!cmd”:运行shell命令cmd。

查找字符:
“/关键字”:先按“/”,再输入想查找的字符,如果第一次查找的关键字不是想要的,可?一直按“n”,往后查找一个关键字。
“? 关键字”:先按“?”键,再输入想查找的字符,如果第一次查找的关键字不是想要的,可以一直按“?”,往后查找一个关键字。

替换字符:
“s /SPARCH/replace/g”:把当前光标所处的行中的SEARCH单词替换成REPLACE,并把所有SEARCH高亮显示。
“%s /SPARCH/REPLACE”:把文档中所有SEARCH替换成REPLACE。
“n1,n2 s /SPARCH/replace/g”:n1、n2表示数字,表示从n1行到n2行,把SEARCH替换成REPLACE。

五.命令行内容说明

命令行模式:移动光标的方法
h或向左方向键(←)光标向左移动一个字符
j或向下方向键(↓)光标向下移动一个字符
k或向上方向键(↑)光标向上移动一个字符
l或向右方向键(→)光标向右移动一个字符
如果想要进行多次移动的话,例如;向下移动30行,可以使用“30j”或“30↓”的组合键,即加上想要进行的次数(数字)后,操作即可。
[Ctrl]+[f]屏幕“向下”移动一页,相当于[Page Down]按键
[Ctrl]+[b]屏幕“向上”移动一页,相当于[Page Up]按键
[Ctrl]+[d]屏幕“向下”移动半页
[Ctrl]+[u]屏幕“向上”移动半页
命令行模式:移动光标的方法
+光标移动到非空格符的下一行
-光标移动到非空格符的上一行
n<space>n表示“数字”,例如20.按下数字后再按空格键,光标会向右移动这一行n个字符。例如20<space>则光标会向后移动20个字符距离
0这是数字“0”:移动到这一行的最前面字符处(常用)
$移动到这一行的最后面字符处(常用)
H光标移动到这个屏幕的最上方哪一行
M光标移动到这个屏幕的中央哪一行
L光标移动到这个屏幕的最下方哪一行
G移动到这个文件的最后一行(常用)
nGn为?字。移动到这个文件的第n行。例如20G则会移动到这个文件的第20行(可配合:set nu)
gg移动到这个文件的第一行,相当于1G(常用)
n<Enter>n为数字。光标向下移动n行(常用)
命令行模式:搜索与替换
/word从光标位置开始,向下寻找一个名为word的字符串。例如要在文件内搜索vbird这个字符串,就输入/vbird即可(常用)
?word从光标位置开始,向上寻找一个名为word的字符串
nn是英文按键。表示“重复前一个搜索的动作”。举例来说,如果刚刚执行/vbird去向下搜索vbird字符串,则按下n后,会向下继续搜索下?个名称为vbird的字符串。如果是执行?vbird的话,那么按下n,则会向上继续搜索名称为vbird的字符串
N这个N是英文按键。与n刚好相反,为“反向”进行前一个搜索操作。例如/vbird后,按下N则表示“向上”搜索vbird
命令行模式:搜索与替换
:n1、n2s/word1/word2/gn1与n2为数字。在第n1与n2行之间寻找word1这个字符串,并将该字符串替换为word2。举例来说,在100到200行之间搜索vbird并替换为VBIRD则:“:100、200s/vbird/VBIRD/g”(常用)
:1、$s/word1/word2/g从第一行到最后一行寻找word1字符串,并将该字符串替换为word2(常用)
:1、$s/word1/word2/gc从第一行到最后一行寻找word1字符串,并将该字符串替换为word2。且在替换前显示提示符给用户确认(conform)是否需要替换(常用)
命令行模式:删除、复制与粘贴
p,Pp为将已复制的数据粘贴到光标的下一行,P则为贴在光标上一行。举例来说,当前光标在第20行,且已经复制了10行数据。则按下p后,那10行数据会粘在原来的20行之后,?由21行开始贴。但如果是按下P,那么原来的第20行会被变成30行(常用)
J将光标所在行与下一列的数据结合成同一行
c重复删除多个数据,例如向下删除10行,[10cj]
u复原前一个操作(常用)
[Ctrl]+r重做上一个操作(常用)
U与[Ctrl]+r是很常用的命令。一个是复原,另一个则是重做一次。利用这两个功能按键,编辑起来就得心应手。
命令行模式:删除、复制与粘贴
.这就是不数点。意思是重复前一个动作。如果想重复删除、重复粘贴,按下小数点“.”就可以(常用)
插入模式
i、I插入:在当前光标所在处插入输入文字,已存在的文字会向后退;其中,i为“从当前光标所在处插入”,I为“在当前所在行的第一个非空格符处开始插入”(常用)
a、Aa为“从当前光标所在的下一个字符处开始插入”,A为“从光标所在行的最?一个字符处开始插入”(常用)
o、O这是英文字母o的大小写。o为“在当前光标所在的下一行处插入新的一行”,O为“在当前光标所在处的上一行插入新的一行”(常用)
r、R替换:r会替换光标所在的那一个字符;R会一直替换光标所在的文字,直到按下Esc键为止(常用)
使用上面这些按键时,在vi画面的左下角处会出现“—INSERT--”或“—replace--”的字样。通过名称就知道是什么操作。特别注意,上面也提过了,想在文件中输入字符时,一定要在左下角处看到INSERT/ REPLACE才能输入。
Esc退出插入模式,回到命令行模式中(常用)
末行命令模式
:w将编辑的数据写入硬盘文件中(常用)
:w!若文件属性为“只读”时,强制写入该文件。不过,到底能不能写入,与文件权限有关
:q离开vi(常用)
:q!若曾修改过文件,又不想存储,使用!为强制离开不存储文件
注意一下,那个感叹号(!)在vi当中,常常具有“强制”的意思。
:wq存储后离开,若为:wq!则为强制存储后离开(常用)
:e!将文件还原到最原始的状态
ZZ若文件没有更改,则不存储离开,若文件已经更改,则存储后离开
:w[filename]将编辑的数据存储成另一个文件(类似另存新文件)
:r[filename]在编辑的数据中,读入另一个文件的数据。即将“filename”这个文件内容加到光标所在行的后面
:n1、n2 w[filename]将n1到n2的内容存储成filename文件
:!command暂时离开vi到命令模式下执行command的显示结果。例如,“:! ls  /home”,即可在vi中查看/home中以ls输出的文件信息
:set nu显示行号,设置之后,会在每一行的前缀显示该行的行号
:set nonu与set nu相反,为取消行号

特别注意,在vi中,“数字”是很有意义的。数字通常表示重复做几次的意思。也有可能表示要去哪里的意思。举例来说,要删除50行,则是用“50dd”。数字加在动作之前。要向下移动20行,使用“20j”或“20↓”即可。

掌握这些命令就很不错了,因为常用到的命令也可能只有一半。通常vi的命令除了上面笔者注明的常用的几个外,其他不用背,可以做一张简单的命令表,当有问题时就可以马上查询。


blob.png


提交成功!非常感谢您的反馈,我们会继续努力做到更好!

这条文档是否有帮助解决问题?

非常抱歉未能帮助到您。为了给您提供更好的服务,我们很需要您进一步的反馈信息:

在文档使用中是否遇到以下问题: