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

vi編輯器命令集合

2016-07-18 14:41:40 16961

基礎(chǔ)

: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

剪切、復(fù)制和粘貼

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 系統(tǒng)交互

:!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

對(duì)齊

:%!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

縮寫

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

3.移動(dòng)光標(biāo)

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


“w”:光標(biāo)跳到下個(gè)字的開(kāi)頭。
“e”:光標(biāo)跳到下個(gè)字的字尾。
“b”:光標(biāo)回到上個(gè)字的開(kāi)頭。
“nl”:光標(biāo)移動(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è)。
—數(shù)字“0”:移動(dòng)到文本的開(kāi)頭。
           “G”:移動(dòng)到文件的最后。
—“$”:移動(dòng)到光標(biāo)所在行的行尾。
    “^”:移動(dòng)到光標(biāo)所在行的行首。

4.刪除文字

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

5.復(fù)制

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

8.更改

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

9.跳至指定的行

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

其他:

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

四.末行模式操作

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

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

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

替換字符:
“s /SPARCH/replace/g”:把當(dāng)前光標(biāo)所處的行中的SEARCH單詞替換成REPLACE,并把所有SEARCH高亮顯示。
“%s /SPARCH/REPLACE”:把文檔中所有SEARCH替換成REPLACE。
“n1,n2 s /SPARCH/replace/g”:n1、n2表示數(shù)字,表示從n1行到n2行,把SEARCH替換成REPLACE。

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

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

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

掌握這些命令就很不錯(cuò)了,因?yàn)槌S玫降拿钜部赡苤挥幸话搿Mǔi的命令除了上面筆者注明的常用的幾個(gè)外,其他不用背,可以做一張簡(jiǎn)單的命令表,當(dāng)有問(wèn)題時(shí)就可以馬上查詢。


blob.png


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

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

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

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