vi編輯器教學


有很多種Unix和編輯檔案對我來說最好的方法之一是使用面向螢幕的文字編輯器vi。這個編輯器讓您編輯在上下文中的其他檔案中的行線。

現在你會發現這就是所謂的VIM vi編輯器的改進版本。這裡VIM代表ViIM被證明。

被普遍認為是在VI編輯器,因為在Unix事實上的標準:

  • 它通常可在Unix系統中的使用。

  • 它的實現是非常的全面。

  • 它需要很少的資源。

  • 這是比任何其他的編輯器,如ed或前更加使用者友好。

可以使用vi編輯器來編輯現有的檔案或建立一個新的檔案從頭。您也可以使用這個編輯器唯讀取一個文字檔案。

啟動vi編輯器:

有以下方式,你就可以開始使用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將建立一個新的檔案,如果它已經不存在於當前的工作目錄下面的例子:

$vi testfile

因此,你會看到類似如下的畫面:

|
~
~
~
~
~
~
~
~
~
~
~
~
"testfile" [New File]    

游標之後你會發現在每行的符號(?)。一個波浪線表示未使用的線路。如果行不以波浪號開始,似乎是空白,有空格,製表符,換行符,或其他一些存在nonviewable字元。

所以,現在你已經開啟了一個檔案開始。在繼續之前,讓我們了解一些微小但重要的概念解釋如下。

操作模式:

用vi編輯器工作時,你會遇到以下兩種模式:

  1. 命令模式:此模式可讓您執行管理任務,如儲存檔案,執行命令,將游標移動,切割和貼上線或字(唬弄),並尋找和更換。在這種模式下,無論你是作為一個命令解釋。

  2. 插入模式:此模式使您可以插入到該檔案中的文字。一切的在此模式下輸入被解釋為輸入,最後,它被放置在該檔案。

vi的總是開始在命令模式。要輸入文字,您必須在插入模式。來插入模式,你只需鍵入i。要插入模式,按Esc鍵,就會把你背到命令模式。

提示:如果你不知道你是在哪個模式,按Esc鍵兩次,那麼你就可以在命令模式。您使用vi編輯器開啟一個檔案,並開始鍵入一些字元,然後在命令模式來理解上的差異。

退出vi:

退出vi的命令是 :q。一旦在命令模式下,鍵入冒號,和“q”,依次是返回。如果您的檔案已經以任何方式修改,編輯器會提醒你這一點,而不是讓你戒煙。要忽略此訊息,不儲存退出vi的命令是:q! 這可以讓你退出VI不儲存任何的變化。

儲存編輯器內容的命令是 :w。您可以結合上面的命令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是大小寫敏感的,所以你需要使用命令時,要特別注意大小寫。

  • 大多數命令在vi中可以行動發生的時候,你想由數量開頭。例如,2j移動游標兩條線下來的游標位置。

還有很多其他的方法來移動在vi一個檔案內。請記住,你必須在命令模式(按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.

正如上面所提到的,大多數命令可以在vi行動發生的時候,你想由數量開頭。例如,2個刪除游標下的位置和2dd刪除游標在兩行兩個字元。

我會強烈建議所有上面的命令,然後再繼續正常練習。

更改命令:

你也可以改變在vi中的字元,單詞或行而不刪除他們。以下是相關的命令:

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 Overwrites multiple characters beginning with the character currently under the cursor. You must use Esc to stop the overwriting.
s Replaces the current character with the character you type. Afterward, you are left in insert mode.
S Deletes the line the cursor is on and replaces with new text. After the new text is entered, vi remains in insert mode.

“複製”和“貼上”命令:

您可以複製線或從一個地方的話,那麼你就可以過去,他們在另一個地方使用下面的命令:

Command 描述
yy Copies the current line.
yw Copies the current word from the character the lowercase w cursor is on until the end of the word.
p Puts the copied text after the cursor.
P Puts the yanked text before the cursor.

高階的命令:

有一些先進的簡化現在的編輯器,以便更有效地利用vi的命令:

Command 描述
J Join the current line with the next one. A count joins that many lines.
<< Shifts the current line to the left by one shift width.
>> Shifts the current line to the right by one shift width.
~ Switch the case of the character under the cursor.
^G Press CNTRL and G keys at the same time to show the current filename and the status.
U Restore the current line to the state it was in before the cursor entered the line.
u Undo the last change to the file. Typing 'u' again will re-do the change.
J Join the current line with the next one. A count joins that many lines.
:f Displays current position in the file in % and file name, total number of file.
:f filename Renames current file to filename.
:w filename Write to file filename.
:e filename Opens another file with filename.
:cd dirname Changes current working directory to dirname.
:e # Use to toggle between two opened files.
:n In case you open multiple files using vi, use :n to go to next file in the series.
:p In case you open multiple files using vi, use :p to go to previous file in the series.
:N In case you open multiple files using vi, use :N to go to previous file in the series.
:r file Reads file and inserts it after current line
:nr file Reads file and inserts it after line n.

Word和字元搜尋:

vi編輯器有兩種型別的搜尋字串和字元。對於一個字串搜尋,/和?命令的使用。當您啟動這些命令,剛剛鍵入的命令將顯示在底線上,在那裡你輸入特定的字串來尋找。

這兩個命令的不同在搜尋方向替換:

  • /命令向前搜尋(向下)在檔案中。

  • ?命令向後搜尋(向上)在檔案中。

n和N命令重複以前的搜尋命令,分別在相同或相反的方向。有些字元具有特殊的含義,同時使用搜尋命令和前面的搜尋表示式的一部分被列為一個反斜槓()。

Character 描述
^ Search at the beginning of the line. (Use at the beginning of a search expression.)
. Matches a single character.
* Matches zero or more of the previous character.
$ End of the line (Use at the end of the search expression.)
[ Starts a set of matching, or non-matching expressions.
< Put in an expression escaped with the backslash to find the ending or beginning of a word.
> See the '<' character description above.

同一行內的字元搜尋搜尋命令後,尋找輸入的字元。f和F命令僅在當前行上的字元搜尋。 f 向前搜尋和F搜尋向後游標移動到的位置找到的字元。

t和T命令搜尋僅在當前行上的字元,但對於T,游標移動到該字元前的位置,和T行的字元之後的位置向後搜尋。

set命令:

你可以改變它的外表和感覺使用以下屏VI:設定命令。要使用這些命令,你必須在命令模式下,然後鍵入 :set其次任何下列選項: 

命令 描述
:set ic Ignores case when searching
:set ai Sets autoindent
:set noai To unset autoindent.
:set nu Displays lines with line numbers on the left side.
:set sw Sets the width of a software tabstop. For example you would set a shift width of 4 with this command: :set sw=4
:set ws If wrapscan is set, if the word is not found at the bottom of the file, it will try to search for it at the beginning.
:set wm If this option has a value greater than zero, the editor will automatically "word wrap". For example, to set the wrap margin to two characters, you would type this: :set wm=2
:set ro Changes file type to "read only"
:set term Prints terminal type
:set bf Discards control characters from input

執行命令:

vi有能力在編輯器中執行命令。要執行一個命令,你只需要進入命令模式輸入 :! 命令。

例如,如果你要檢查檔案是否存在,然後再嘗試儲存您的檔案,檔案名,你可以鍵入:! ls在螢幕上,你會看到ls和ls的輸出。

當你按任意鍵(或命令的跳脫序列),您將返回到您的vi對談。

替換文字:

替換命令(:s/) ,使您能夠快速替換單詞或組的話,在你的檔案。下面是一個簡單的語法:

:s/search/replace/g

g代表全域性。這個命令的結果是,所有出現游標的上線改變。

重要提示:

這裡用vi你成功的關鍵點是:

  • 您必須在命令模式下使用命令。 (按Esc鍵兩次在任何時候,以確保您在命令模式中。)

  • 你必須小心正確使用所有命令。

  • 您必須在插入模式下輸入文字。