有很多種Unix和編輯文件對我來說最好的方法之一是使用面向屏幕的文本編輯器vi。這個編輯器讓您編輯在上下文中的其他文件中的行線。
現(xiàn)在你會發(fā)現(xiàn)這就是所謂的VIM vi編輯器的改進版本。這里VIM代表ViIM被證明。
被普遍認為是在VI編輯器,因為在Unix事實上的標準:
它通常可在Unix系統(tǒng)中的使用。
它的實現(xiàn)是非常的全面。
它需要很少的資源。
這是比任何其他的編輯器,如ed或前更加用戶友好。
可以使用vi編輯器來編輯現(xiàn)有的文件或創(chuàng)建一個新的文件從頭。您也可以使用這個編輯器只讀取一個文本文件。
有以下方式,你就可以開始使用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)建一個新的文件,如果它已經(jīng)不存在于當前的工作目錄下面的例子:
$vi testfile
因此,你會看到類似如下的畫面:
| ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ "testfile" [New File]
光標之后你會發(fā)現(xiàn)在每行的符號(?)。一個波浪線表示未使用的線路。如果行不以波浪號開始,似乎是空白,有空格,制表符,換行符,或其他一些存在nonviewable字符。
所以,現(xiàn)在你已經(jīng)打開了一個文件開始。在繼續(xù)之前,讓我們了解一些微小但重要的概念解釋如下。
用vi編輯器工作時,你會遇到以下兩種模式:
命令模式:此模式可讓您執(zhí)行管理任務(wù),如保存文件,執(zhí)行命令,將光標移動,切割和粘貼線或字(唬弄),并尋找和更換。在這種模式下,無論你是作為一個命令解釋。
插入模式:此模式使您可以插入到該文件中的文本。一切的在此模式下輸入被解釋為輸入,最后,它被放置在該文件。
vi的總是開始在命令模式。要輸入文字,您必須在插入模式。來插入模式,你只需鍵入i。要插入模式,按Esc鍵,就會把你背到命令模式。
提示:如果你不知道你是在哪個模式,按Esc鍵兩次,那么你就可以在命令模式。您使用vi編輯器打開一個文件,并開始鍵入一些字符,然后在命令模式來理解上的差異。
退出vi的命令是 :q。一旦在命令模式下,鍵入冒號,和“q”,依次是返回。如果您的文件已經(jīng)以任何方式修改,編輯器會提醒你這一點,而不是讓你戒煙。要忽略此消息,不保存退出vi的命令是:q! 這可以讓你退出VI不保存任何的變化。
保存編輯器內(nèi)容的命令是 :w。您可以結(jié)合上面的命令quit命令,或者 :wq和返回。
保存更改并退出vi的最簡單的方法是ZZ命令。當你在命令模式下:wq ,它會做相當于輸入ZZ。
您可以指定一個不同的文件名保存到指定的名稱:W之后。例如,如果你希望你工作,另一名為文件名的文件名保存該文件,您可以鍵入:w filename2中和返回。嘗試一次。
要左右移動在一個文件中,而不會影響你的文字,您必須在命令模式下(按Esc鍵兩次)。這里有一些你可以用它來走動一次一個字符的命令:
| 命令 | 描述 |
|---|---|
| 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. |
有以下兩個重要點要注意:
vi是大小寫敏感的,所以你需要使用命令時,要特別注意大小寫。
大多數(shù)命令在vi中可以行動發(fā)生的時候,你想由數(shù)量開頭。例如,2j移動光標兩條線下來的光標位置。
還有很多其他的方法來移動在vi一個文件內(nèi)。請記住,你必須在命令模式(按Esc鍵兩次)。這里更多一些命令,你可以用它來走動文件:
| 命令 | 描述 |
|---|---|
| 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 |
要編輯的文件,你需要在插入模式。有很多方法進入插入模式,命令模式:
| 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. |
這里是清單可用于在一個打開的文件中刪除字符和線條的重要的命令:
| 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行動發(fā)生的時候,你想由數(shù)量開頭。例如,2個刪除光標下的位置和2dd刪除光標在兩行兩個字符。
我會強烈建議所有上面的命令,然后再繼續(xù)正常練習。
你也可以改變在vi中的字符,單詞或行而不刪除他們。以下是相關(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ò)實用工具下一篇:UNIX 文件權(quán)限/訪問模式 |