国产欧美日韩第一页|日本一二三不卡视频|在线精品小视频,亚洲第一免费播放区,metcn人体亚洲一区,亚洲精品午夜视频

vi編輯器命令集合

2016-07-18 14:41:40 11786

基礎

: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

在文件中移動(dòng)

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

大小寫(xiě)

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

讀寫(xiě)文件

: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

自動(dòng)完成

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

縮寫(xiě)

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

文本縮進(jìn)

: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

語(yǔ)法高亮

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

三.命令行模式操作

1.進(jìn)入插入模式

按“i”:從光標當前位置開(kāi)始輸入文件。
按“I”:在光標所在行的行首插入。
按“a”:從目前光標所在位置的下一個(gè)位置開(kāi)始輸入文字。
按“A”:在光標所在行的行末插入。
按“o”:在下面插入一行
按“O”:在上面插入一行。
按“s”:刪除光標后的一個(gè)字符,然后進(jìn)入插入模式。
按“S”:刪除光標所在的行,然后進(jìn)入插入模式。

3.移動(dòng)光標

“h”、“j”、“k”、“l(fā)”分別控制光標左、下、上、右移一格。


“w”:光標跳到下個(gè)字的開(kāi)頭。
“e”:光標跳到下個(gè)字的字尾。
“b”:光標回到上個(gè)字的開(kāi)頭。
“nl”:光標移動(dòng)該行的第n個(gè)位置,例如:“5l”表示移動(dòng)到該行的第5個(gè)字符。

“ctrl+b”:屏幕往后移動(dòng)一頁(yè)。
“ctrl+f”:屏幕往前移動(dòng)一頁(yè)。
“ctrl+u”:屏幕往后移動(dòng)半頁(yè)。
“ctrl+d”:屏幕往前移動(dòng)半頁(yè)。
—數字“0”:移動(dòng)到文本的開(kāi)頭。
           “G”:移動(dòng)到文件的最后。
—“$”:移動(dòng)到光標所在行的行尾。
    “^”:移動(dòng)到光標所在行的行首。

4.刪除文字

“x”:每按一次,刪除光?所在位置的后面一個(gè)字符。
“nx”:例如:“6x”表示刪除光標所在位置后面6個(gè)字符。
“X”:每按一次,刪除光標所在位置的前面一個(gè)字符。
“dd”:刪除光標所在行。
“ndd”:從光標所在行開(kāi)始刪除n行。例如:“4dd”表示刪除從光標所在行開(kāi)始的4行字符。

5.復制

“yw”:將光標所在之處到字尾的字符復制到緩沖區中。
“nyw”:復制n個(gè)字到緩沖區。
“yy”:復制光標所在行到緩沖區。
“nyy”:例如:“6yy”表示復制從光標所在行開(kāi)始6行字符。
“p”:將緩沖區內的字符寫(xiě)到光標所在位置。

8.更改

“cw”:更改光標所在處的字到字尾處。
“cnw”:例如:“c3w”表示更改3個(gè)字。

9.跳至指定的行

“ctrl+g”:列出光標所在行的行號。
“nG”:例如:“15G”,表示移動(dòng)光標到該文件的第15行行首。

其他:

“ZZ”:存盤(pán)退出
“ZQ”:不存盤(pán)退出
“r”:替換光標所在處的字符。
“R”:替換光標所到處的字符,直到按下“ESC”鍵為止。
“u”:撤銷(xiāo)上一次操作

四.末行模式操作

在使用末行模式之前,請記住先按“ESC”鍵確定已經(jīng)處于命令行模式后,再按冒號“:”即可進(jìn)入末行模式。

“set nu”:列出行號
“set nonu”:取消列出行號
“set ic”:搜索時(shí)忽略大小寫(xiě)
“set noic”:取消在搜索時(shí)忽略大小寫(xiě)。
“n”:跳到文件中的某一行,“n”表示一個(gè)數字,如輸入數字15,再回車(chē)就會(huì )跳到文本的第15行。
“!cmd”:運行shell命令cmd。

查找字符:
“/關(guān)鍵字”:先按“/”,再輸入想查找的字符,如果第一次查找的關(guān)鍵字不是想要的,可?一直按“n”,往后查找一個(gè)關(guān)鍵字。
“? 關(guān)鍵字”:先按“?”鍵,再輸入想查找的字符,如果第一次查找的關(guān)鍵字不是想要的,可以一直按“?”,往后查找一個(gè)關(guā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。

五.命令行內容說(shuō)明

命令行模式:移動(dòng)光標的方法
h或向左方向鍵(←)光標向左移動(dòng)一個(gè)字符
j或向下方向鍵(↓)光標向下移動(dòng)一個(gè)字符
k或向上方向鍵(↑)光標向上移動(dòng)一個(gè)字符
l或向右方向鍵(→)光標向右移動(dòng)一個(gè)字符
如果想要進(jìn)行多次移動(dòng)的話(huà),例如;向下移動(dòng)30行,可以使用“30j”或“30↓”的組合鍵,即加上想要進(jìn)行的次數(數字)后,操作即可。
[Ctrl]+[f]屏幕“向下”移動(dòng)一頁(yè),相當于[Page Down]按鍵
[Ctrl]+[b]屏幕“向上”移動(dòng)一頁(yè),相當于[Page Up]按鍵
[Ctrl]+[d]屏幕“向下”移動(dòng)半頁(yè)
[Ctrl]+[u]屏幕“向上”移動(dòng)半頁(yè)
命令行模式:移動(dòng)光標的方法
+光標移動(dòng)到非空格符的下一行
-光標移動(dòng)到非空格符的上一行
n<space>n表示“數字”,例如20.按下數字后再按空格鍵,光標會(huì )向右移動(dòng)這一行n個(gè)字符。例如20<space>則光標會(huì )向后移動(dòng)20個(gè)字符距離
0這是數字“0”:移動(dòng)到這一行的最前面字符處(常用)
$移動(dòng)到這一行的最后面字符處(常用)
H光標移動(dòng)到這個(gè)屏幕的最上方哪一行
M光標移動(dòng)到這個(gè)屏幕的中央哪一行
L光標移動(dòng)到這個(gè)屏幕的最下方哪一行
G移動(dòng)到這個(gè)文件的最后一行(常用)
nGn為?字。移動(dòng)到這個(gè)文件的第n行。例如20G則會(huì )移動(dòng)到這個(gè)文件的第20行(可配合:set nu)
gg移動(dòng)到這個(gè)文件的第一行,相當于1G(常用)
n<Enter>n為數字。光標向下移動(dòng)n行(常用)
命令行模式:搜索與替換
/word從光標位置開(kāi)始,向下尋找一個(gè)名為word的字符串。例如要在文件內搜索vbird這個(gè)字符串,就輸入/vbird即可(常用)
?word從光標位置開(kāi)始,向上尋找一個(gè)名為word的字符串
nn是英文按鍵。表示“重復前一個(gè)搜索的動(dòng)作”。舉例來(lái)說(shuō),如果剛剛執行/vbird去向下搜索vbird字符串,則按下n后,會(huì )向下繼續搜索下?個(gè)名稱(chēng)為vbird的字符串。如果是執行?vbird的話(huà),那么按下n,則會(huì )向上繼續搜索名稱(chēng)為vbird的字符串
N這個(gè)N是英文按鍵。與n剛好相反,為“反向”進(jìn)行前一個(gè)搜索操作。例如/vbird后,按下N則表示“向上”搜索vbird
命令行模式:搜索與替換
:n1、n2s/word1/word2/gn1與n2為數字。在第n1與n2行之間尋找word1這個(gè)字符串,并將該字符串替換為word2。舉例來(lái)說(shuō),在100到200行之間搜索vbird并替換為VBIRD則:“:100、200s/vbird/VBIRD/g”(常用)
:1、$s/word1/word2/g從第一行到最后一行尋找word1字符串,并將該字符串替換為word2(常用)
:1、$s/word1/word2/gc從第一行到最后一行尋找word1字符串,并將該字符串替換為word2。且在替換前顯示提示符給用戶(hù)確認(conform)是否需要替換(常用)
命令行模式:刪除、復制與粘貼
p,Pp為將已復制的數據粘貼到光標的下一行,P則為貼在光標上一行。舉例來(lái)說(shuō),當前光標在第20行,且已經(jīng)復制了10行數據。則按下p后,那10行數據會(huì )粘在原來(lái)的20行之后,?由21行開(kāi)始貼。但如果是按下P,那么原來(lái)的第20行會(huì )被變成30行(常用)
J將光標所在行與下一列的數據結合成同一行
c重復刪除多個(gè)數據,例如向下刪除10行,[10cj]
u復原前一個(gè)操作(常用)
[Ctrl]+r重做上一個(gè)操作(常用)
U與[Ctrl]+r是很常用的命令。一個(gè)是復原,另一個(gè)則是重做一次。利用這兩個(gè)功能按鍵,編輯起來(lái)就得心應手。
命令行模式:刪除、復制與粘貼
.這就是不數點(diǎn)。意思是重復前一個(gè)動(dòng)作。如果想重復刪除、重復粘貼,按下小數點(diǎn)“.”就可以(常用)
插入模式
i、I插入:在當前光標所在處插入輸入文字,已存在的文字會(huì )向后退;其中,i為“從當前光標所在處插入”,I為“在當前所在行的第一個(gè)非空格符處開(kāi)始插入”(常用)
a、Aa為“從當前光標所在的下一個(gè)字符處開(kāi)始插入”,A為“從光標所在行的最?一個(gè)字符處開(kāi)始插入”(常用)
o、O這是英文字母o的大小寫(xiě)。o為“在當前光標所在的下一行處插入新的一行”,O為“在當前光標所在處的上一行插入新的一行”(常用)
r、R替換:r會(huì )替換光標所在的那一個(gè)字符;R會(huì )一直替換光標所在的文字,直到按下Esc鍵為止(常用)
使用上面這些按鍵時(shí),在vi畫(huà)面的左下角處會(huì )出現“—INSERT--”或“—replace--”的字樣。通過(guò)名稱(chēng)就知道是什么操作。特別注意,上面也提過(guò)了,想在文件中輸入字符時(shí),一定要在左下角處看到INSERT/ REPLACE才能輸入。
Esc退出插入模式,回到命令行模式中(常用)
末行命令模式
:w將編輯的數據寫(xiě)入硬盤(pán)文件中(常用)
:w!若文件屬性為“只讀”時(shí),強制寫(xiě)入該文件。不過(guò),到底能不能寫(xiě)入,與文件權限有關(guān)
:q離開(kāi)vi(常用)
:q!若曾修改過(guò)文件,又不想存儲,使用!為強制離開(kāi)不存儲文件
注意一下,那個(gè)感嘆號(!)在vi當中,常常具有“強制”的意思。
:wq存儲后離開(kāi),若為:wq!則為強制存儲后離開(kāi)(常用)
:e!將文件還原到最原始的狀態(tài)
ZZ若文件沒(méi)有更改,則不存儲離開(kāi),若文件已經(jīng)更改,則存儲后離開(kāi)
:w[filename]將編輯的數據存儲成另一個(gè)文件(類(lèi)似另存新文件)
:r[filename]在編輯的數據中,讀入另一個(gè)文件的數據。即將“filename”這個(gè)文件內容加到光標所在行的后面
:n1、n2 w[filename]將n1到n2的內容存儲成filename文件
:!command暫時(shí)離開(kāi)vi到命令模式下執行command的顯示結果。例如,“:! ls  /home”,即可在vi中查看/home中以ls輸出的文件信息
:set nu顯示行號,設置之后,會(huì )在每一行的前綴顯示該行的行號
:set nonu與set nu相反,為取消行號

特別注意,在vi中,“數字”是很有意義的。數字通常表示重復做幾次的意思。也有可能表示要去哪里的意思。舉例來(lái)說(shuō),要刪除50行,則是用“50dd”。數字加在動(dòng)作之前。要向下移動(dòng)20行,使用“20j”或“20↓”即可。

掌握這些命令就很不錯了,因為常用到的命令也可能只有一半。通常vi的命令除了上面筆者注明的常用的幾個(gè)外,其他不用背,可以做一張簡(jiǎn)單的命令表,當有問(wèn)題時(shí)就可以馬上查詢(xún)。


blob.png


提交成功!非常感謝您的反饋,我們會(huì )繼續努力做到更好!

這條文檔是否有幫助解決問(wèn)題?

非常抱歉未能幫助到您。為了給您提供更好的服務(wù),我們很需要您進(jìn)一步的反饋信息:

在文檔使用中是否遇到以下問(wèn)題:
-->