有很多種Unix和編輯文件對(duì)我來(lái)說(shuō)最好的方法之一是使用面向屏幕的文本編輯器vi。這個(gè)編輯器讓您編輯在上下文中的其他文件中的行線(xiàn)。
現(xiàn)在你會(huì)發(fā)現(xiàn)這就是所謂的VIM vi編輯器的改進(jìn)版本。這里VIM代表ViIM被證明。
被普遍認(rèn)為是在VI編輯器,因?yàn)樵赨nix事實(shí)上的標(biāo)準(zhǔn):
它通??稍赨nix系統(tǒng)中的使用。
它的實(shí)現(xiàn)是非常的全面。
它需要很少的資源。
這是比任何其他的編輯器,如ed或前更加用戶(hù)友好。
可以使用vi編輯器來(lái)編輯現(xiàn)有的文件或創(chuàng)建一個(gè)新的文件從頭。您也可以使用這個(gè)編輯器只讀取一個(gè)文本文件。
有以下方式,你就可以開(kāi)始使用vi編輯器:
命令 | 描述 |
---|---|
vi filename | Creates a new file if it already does not exist, otherwise opens existing file. |
vi -R filename | Opens an existing file in read only mode. |
view filename | Opens an existing file in read only mode. |
testfile將創(chuàng)建一個(gè)新的文件,如果它已經(jīng)不存在于當(dāng)前的工作目錄下面的例子:
$vi testfile
因此,你會(huì)看到類(lèi)似如下的畫(huà)面:
| ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ "testfile" [New File]
光標(biāo)之后你會(huì)發(fā)現(xiàn)在每行的符號(hào)(?)。一個(gè)波浪線(xiàn)表示未使用的線(xiàn)路。如果行不以波浪號(hào)開(kāi)始,似乎是空白,有空格,制表符,換行符,或其他一些存在nonviewable字符。
所以,現(xiàn)在你已經(jīng)打開(kāi)了一個(gè)文件開(kāi)始。在繼續(xù)之前,讓我們了解一些微小但重要的概念解釋如下。
用vi編輯器工作時(shí),你會(huì)遇到以下兩種模式:
命令模式:此模式可讓您執(zhí)行管理任務(wù),如保存文件,執(zhí)行命令,將光標(biāo)移動(dòng),切割和粘貼線(xiàn)或字(唬弄),并尋找和更換。在這種模式下,無(wú)論你是作為一個(gè)命令解釋。
插入模式:此模式使您可以插入到該文件中的文本。一切的在此模式下輸入被解釋為輸入,最后,它被放置在該文件。
vi的總是開(kāi)始在命令模式。要輸入文字,您必須在插入模式。來(lái)插入模式,你只需鍵入i。要插入模式,按Esc鍵,就會(huì)把你背到命令模式。
提示:如果你不知道你是在哪個(gè)模式,按Esc鍵兩次,那么你就可以在命令模式。您使用vi編輯器打開(kāi)一個(gè)文件,并開(kāi)始鍵入一些字符,然后在命令模式來(lái)理解上的差異。
退出vi的命令是 :q。一旦在命令模式下,鍵入冒號(hào),和“q”,依次是返回。如果您的文件已經(jīng)以任何方式修改,編輯器會(huì)提醒你這一點(diǎn),而不是讓你戒煙。要忽略此消息,不保存退出vi的命令是:q! 這可以讓你退出VI不保存任何的變化。
保存編輯器內(nèi)容的命令是 :w。您可以結(jié)合上面的命令quit命令,或者 :wq和返回。
保存更改并退出vi的最簡(jiǎn)單的方法是ZZ命令。當(dāng)你在命令模式下:wq ,它會(huì)做相當(dāng)于輸入ZZ。
您可以指定一個(gè)不同的文件名保存到指定的名稱(chēng):W之后。例如,如果你希望你工作,另一名為文件名的文件名保存該文件,您可以鍵入:w filename2中和返回。嘗試一次。
要左右移動(dòng)在一個(gè)文件中,而不會(huì)影響你的文字,您必須在命令模式下(按Esc鍵兩次)。這里有一些你可以用它來(lái)走動(dòng)一次一個(gè)字符的命令:
命令 | 描述 |
---|---|
k | Moves the cursor up one line. |
j | Moves the cursor down one line. |
h | Moves the cursor to the left one character position. |
l | Moves the cursor to the right one character position. |
有以下兩個(gè)重要點(diǎn)要注意:
vi是大小寫(xiě)敏感的,所以你需要使用命令時(shí),要特別注意大小寫(xiě)。
大多數(shù)命令在vi中可以行動(dòng)發(fā)生的時(shí)候,你想由數(shù)量開(kāi)頭。例如,2j移動(dòng)光標(biāo)兩條線(xiàn)下來(lái)的光標(biāo)位置。
還有很多其他的方法來(lái)移動(dòng)在vi一個(gè)文件內(nèi)。請(qǐng)記住,你必須在命令模式(按Esc鍵兩次)。這里更多一些命令,你可以用它來(lái)走動(dòng)文件:
命令 | 描述 |
---|---|
0 or | | Positions cursor at beginning of line. |
$ | Positions cursor at end of line. |
w | Positions cursor to the next word. |
b | Positions cursor to previous word. |
( | Positions cursor to beginning of current sentence. |
) | Positions cursor to beginning of next sentence. |
E | Move to the end of Blank delimited word |
{ | Move a paragraph back |
} | Move a paragraph forward |
[[ | Move a section back |
]] | Move a section forward |
n| | Moves to the column n in the current line |
1G | Move to the first line of the file |
G | Move to the last line of the file |
nG | Move to nth line of the file |
:n | Move to nth line of the file |
fc | Move forward to c |
Fc | Move back to c |
H | Move to top of screen |
nH | Moves to nth line from the top of the screen |
M | Move to middle of screen |
L | Move to botton of screen |
nL | Moves to nth line from the bottom of the screen |
:x | Colon followed by a number would position the cursor on line number represented by x |
使用命令后,您可以使用控制鍵:
命令 | 描述 |
---|---|
CTRL+d | Move forward 1/2 screen |
CTRL+d | Move forward 1/2 screen |
CTRL+f | Move forward one full screen |
CTRL+u | Move backward 1/2 screen |
CTRL+b | Move backward one full screen |
CTRL+e | Moves screen up one line |
CTRL+y | Moves screen down one line |
CTRL+u | Moves screen up 1/2 page |
CTRL+d | Moves screen down 1/2 page |
CTRL+b | Moves screen up one page |
CTRL+f | Moves screen down one page |
CTRL+I | Redraws screen |
要編輯的文件,你需要在插入模式。有很多方法進(jìn)入插入模式,命令模式:
Command | 描述 |
---|---|
i | Inserts text before current cursor location. |
I | Inserts text at beginning of current line. |
a | Inserts text after current cursor location. |
A | Inserts text at end of current line. |
o | Creates a new line for text entry below cursor location. |
O | Creates a new line for text entry above cursor location. |
這里是清單可用于在一個(gè)打開(kāi)的文件中刪除字符和線(xiàn)條的重要的命令:
Command | 描述 |
---|---|
x | Deletes the character under the cursor location. |
X | Deletes the character before the cursor location. |
dw | Deletes from the current cursor location to the next word. |
d^ | Deletes from current cursor position to the beginning of the line. |
d$ | Deletes from current cursor position to the end of the line. |
D | Deletes from the cursor position to the end of the current line. |
dd | Deletes the line the cursor is on. |
正如上面所提到的,大多數(shù)命令可以在vi行動(dòng)發(fā)生的時(shí)候,你想由數(shù)量開(kāi)頭。例如,2個(gè)刪除光標(biāo)下的位置和2dd刪除光標(biāo)在兩行兩個(gè)字符。
我會(huì)強(qiáng)烈建議所有上面的命令,然后再繼續(xù)正常練習(xí)。
你也可以改變?cè)趘i中的字符,單詞或行而不刪除他們。以下是相關(guān)的命令:
Command | 描述 |
---|---|
cc | Removes contents of the line, leaving you in insert mode. |
cw | Changes the word the cursor is on from the cursor to the lowercase w end of the word. |
r | Replaces the character under the cursor. vi returns to command mode after the replacement is entered. |
R | 上一篇:UNIX 網(wǎng)絡(luò)實(shí)用工具下一篇:UNIX 文件權(quán)限/訪(fǎng)問(wèn)模式 |