vi 的使用方法 (Linux/ Unix)

vi is a screen-oriented text editor originally created for the Unix operating system. The portable subset of the behavior of vi and programs based on it, and the ex editor language supported within these programs, is described by (and thus standardized by) the Single Unix Specification and POSIX.

vi是「Visual」的不正規的縮寫,來源於另外一個文本編輯器ex的命令visual。

vi是一種模式編輯器。不同的按鈕和鍵擊可以更改不同的「模式」;比如說:在「插入模式」下,輸入的文本會直接被插入到文檔;當按下「退出鍵」,「插入模式」就會更改為「命令模式」,並且游標的移動和功能的編輯都由字母來響應,例如:「j」用來移動游標到下一行;「k」用來移動游標到上一行,「x」可以刪除當前游標處的字元,「i」可以返回到「插入模式」(也可以使用方向鍵)。在「命令模式」下,敲入的鍵(字母)並不會插入到文檔,這給新接觸vi的用戶帶來混亂。

在「命令模式」下,多重文本編輯操作是由一組鍵(字母)來執行,而不是同時按下、和其他特殊鍵來完成。更多複雜的編輯操作可以使用多重功能基元的組合,比如說「dw」用來刪除一個單詞,「c2fa」可以更改當前的游標處中「a」之前的文本。這就是說:對於熟練的vi用戶可以更快的操作,因為雙手就可以不必離開鍵盤。


使用方法與指令

You need to change to the “insert” mode by moving the cursor with i. There are other ways to delete characters directly. You can download a powerpoint that I use when teaching a class on VI here.

It sounds like you are in the “Command” mode. To move your cursor:
H = Left, J = Up, K = Down, L = Right

Once you have your cursor positioned you can delete text as follows:
x Deletes the character under the cursor
X Deletes the character before the cursor
dw Deletes from the cursor to the next word
dd Deletes the line the cursor is on.

To enter text, you can use one of the text entry modes.
a Adds text to the right of the cursor
A Adds text to the end of the current line
i Adds text to the left of the cursor
I Adds test to the beginin of the current line
o Opens a new line below the current line and places you in text entry mode
O Opens a new line Above the current line and places you in text entry mode

To exit Text entry mode, and return to the Command mode, use Esc.

To Undo changes: (A student favorite)
u Undo the last command entered
U Undo all changes to the ** current line**

To Save/Quit: :w Writes (Saves) the file and remains open
:wq Writes (Saves) the file and exits VI
:q Quits (Exits) if you’ve made no changes
:q! Quits (Exits) without saving changes
ZZ Writes (Saves) the file and exits VI (same as :wq)