本文旨在幫助學習 vim 的新手快速掌握 vim 的基本操作。本文 整理自《 The Linux Command Line 》中《 A Gentle Introduction To vi 》一章 。該書籍通俗易懂,容易上手 , 十分實用。下面保留了一部分的英文,旨在幫助更好的理解命令,從而更快的掌握命令。
注明:文章中實驗在ubuntu 11下進行。Vi編輯器最初由Bill Joy于1976年編寫,大多數的Linux系統并不包含真正的vi,而是由vim(vi improved 的縮寫)替換了。在多數系統中vim將被鏈接為vi,假定我們使用的vi實質上是vim程序。
正文
1.Starting And Stopping vi 啟動和退出vi
我們需要學習的第一件事就是如何啟動和退出vi。
啟動vim只需要鍵入
qiaoqiao@ubuntu:~$ vi
即可,啟動后界面如下圖所示:

要退出vi,鍵入
:q
即可。
如果由于某些原因(通常是編輯了文件而沒有保存的情況下)無法退出時,我們可以使用
:q!
來強制退出。
2.Editing Modes 編輯模式
再次啟動vi,這次在命令后加上一個目前不存在的文件名,如下:
qiaoqiao@ubuntu:~$ vi foo.txt
啟動后界面如下:

其中波浪號"~"表示該行還沒有文字。這里顯示了一個空文件,沒有任何東西。
第二件我們需要學習的重要事情就是要知道vi是一個模式編輯器。當vi啟動的時候,它就出在命令模式下。在這種模式下,任何的鍵入都將被解釋成命令,此時我們開始鍵入的時候vi將會顯得有點混亂。
進入插入模式
要想添加文本到文件,首先我們需要進入插入模式。這可以通過在命令模式下,鍵入"i"鍵即可。我們將會看到vi下方的提示如下:
-- INSERT -- 0,1 All
現在插入下面一段文字:
The qucik brown fox jumped over the lazy dog.
要退出插入模式并返回到命令模式,我們可以按Esc鍵。
保存我們的工作
要保存我們對文件的更改,我們需要在命令模式下鍵入一個ex命令(ex command)。我們可以先鍵入一個冒號,然后緊接著一個"w",如下
:w
來保存我們的工作。當保存的時候vi下方的提示如下所示:
"foo.txt" [New] 1L, 46C written 1,45 All
提示:如果閱讀vim的幫助文檔的話,你將會看到,命令模式被稱為普通模式,而ex 命令將被稱為命令模式。我們注意這一點。
3 Moving The Cursor Around 光標定位
在命令模式下,我們可以通過鍵入一些移動光標的命令來移動光標。常見的操作如下表所示:
key
|
Moves The Cursor
|
l or Right Arrow
|
Right one character.
|
h or Left Arrow
|
Left one character
|
j or Down Arrow
|
Down one line.
|
k or Up Arrow
|
Up one line
|
0 (zero)
|
To the beginning of the current line.
|
^
|
To the first non-whitespace character on the current line.
|
$
|
To the end of the current line
|
w
|
To the beginning of the next word or punctuation character
|
W
|
To the beginning of the next word or punctuation character.
|
b
|
To the beginning of the previous word or punctuation character.
|
B
|
To the beginning of the previous word, ignoring punctuation characters.
|
Ctrl-f or Page Down
|
Down one page
|
Ctrl-b or Page Up
|
Up one page
|
numberG
|
To line number. For example, 1G moves to the first line of the file.
|
G
|
To the last line of the file
|
注意很多的命令都可以以數字開頭,通過添加數字我們可以讓命令執行多次,比如"5j"就可以將光標向下移動5行。
4.Basic Editing 基本編輯
通常的編輯包括了插入文本、刪除文本、剪切和復制文本這些操作。Vi包括了這些操作同時也包含了適當的撤銷操作。當在命令模式下按下"u"鍵的時候,我們就撤銷了最后一次的更改。這在編輯時十分方便。
4.1 Appending Text 追加文本
Vi有很多插入的命令。我們已經用過了i命令來進行插入。
這里介紹兩個新的命令,a和A命令如下表所示:
Command
|
Append text
|
a
|
Append text after the current cursor
|
A
|
Append text to the end of the current line
|
也就是a命令是在當前光標之后插入內容,A命令在當前行末尾插入內容。
下面實例練習一下。
回到上文中的foo.txt.
首先將光標移動到第一行末尾,可以使用"$"命令,然后鍵入a命令,插入內容后如下:
The qucik brown fox jumped over the lazy dog.It was cool.
這樣操作顯得不是很快捷,下面使用A命令來插入內容。不保存上述更改,通過"0"命令將光標定位到第一行行首,然后鍵入A命令,我們即可在行尾直接插入內容,我們插入如下內容:

我們看到A命令直接將光標置到行尾進行插入,十分方便。
4.2 Opening A Line 新增一行
我們通過使用o和 O命令來新增一行。命令如下表所示:
Command
|
Open a line
|
o
|
Open a blank line below the current line.
|
O
|
Open a blank line above the current line
|
也就是使用o命令在當前行的下方插入一行,使用O命令在當前行的上方插入一行。
下面實例演示.
補充:
為了便于觀察到效果,這里介紹下讓vi顯示行號。
在用戶主目錄,即你登錄時的目錄 /home/用戶名下,使用命令:
qiaoqiao@ubuntu:~$ sudo vim .vimrc
打開vi配置文件,然后寫入內容如下:

來讓vi顯示行號。
打開foo.txt,將光標定位在第三行,然后鍵入o命令,我們看到在第三行之下插入一行,如下所示:

撤銷剛才的操作,光標依然定位在第三行,使用O命令,我們看到在第三行之上插入一行如下所示:

4.3 Deleting Text 刪除文本
Vi提供了很方便的刪除文本的命令,x命令刪除當前光標處的字符,也可以在x命令錢加上數字來指定刪除的字符數目。d命令也用來刪除文本,另外,d命令總是跟著一個移動的命令來控制刪除的區域。
下表是一些實例:
Command
|
Deletes text(cuts text)
|
x
|
Delete the current character.
|
3x
|
Delete the current character and the next two characters
|
dd
|
Delete the current line.
|
5dd
|
Delete the current line and the next four lines.
|
dw
|
Delete from the current cursor position to the beginning of the next word.
|
d$
|
Delete from the current cursor location to the end of the current line.
|
d0
|
Delete from the current cursor location to the beginning of the line.
|
d^
|
Delete from the current cursor location to the first non-whitespace character in the line.
|
dG
|
Delete from the current line to the end of the file.
|
d20G
|
Delete from the current line to the twentieth line of the file.
|
下面做一些練習。
打開foo.txt,將光標定位到第一行的"It"單詞上,然后重復使用x命令刪除這個句子。
按下"u"鍵撤銷操作,這次同樣將光標定位在"It"上,我們使用dw命令來刪除單詞"It",刪除后結果如下圖所示:

撤銷操作,再次將光標定位到"It"上,這次使用d$命令將光標到行尾的部分全部刪除,刪除后結果如下圖所示:

鍵入dG命令刪除第一行到最后一行的內容,刪除結果如下圖所示:

4.4 Cutting, Copying And Pasting Text 剪切、復制和粘貼
在vi中實際上d命令在刪除文本的同時會把刪除的內容放到類似于windows下剪切板的緩沖區中,刪除的內容可以在以后的操作中用p或者P命令進行粘貼,這類似于剪切操作。
y命令則用來復制內容(稱為yank),這與d命令用來剪切內容很相同。下表是一些復制操作的例子:
Command
|
Copies text
|
yy
|
Copy the current line
|
5yy
|
Copy the current line and the next four lines.
|
yW
|
Copy from the current cursor position to the beginning of the next word
|
y$
|
Copy from the current cursor location to the end of the current line.
|
y0
|
Copy from the current cursor location to the beginning of the line
|
y^
|
Copy from the current cursor location to the first non-whitespace character in the line.
|
yG
|
Copy from the current line to the end of the file.
|
y20G
|
Copy from the current line to the twentieth line of the file.
|
下面做一些練習。
打開foo.txt,我們將光標定位在第一行,然后使用yy命令復制第一行內容,再使用G命令將光標置于最后一行,我們使用p命令將內容粘貼在最后一行的下面。
結果如下圖所示:

撤銷剛才的操作,將光標再次定位到最后一行,這次使用P命令,將內容粘貼在最后一行的上面,結果如下圖所示:

操作完畢后請將文件恢復到原狀態。
4.5 Joining Lines 行的連接
Vi中對行的概念是很嚴格的。通常情況下你無法將光標移動到行的末尾來刪除行結束符而把下一行連接起來。因此vi中提供了J命令來進行行連接。
將光標置于第三行,我們使用J命令,結果如下圖所示:

4.6 Search And Replace 查找和替換
單行查找
單行查找使用f命令,f命令在一行中查找單個字符,并將光標置于下一個字符出現的位置。
例如,我們使用fa命令,在當前行中查找"a"字符。
全文查找
Vi中使用/命令來進行單詞或者句子的查找。當我們鍵入/符號,屏幕下方就會出現/符號。接下來鍵入單詞或者句子,并以回車鍵結束就開始了全文查找。我們可以使用n命令來重復查找命令,查看下一個單詞或者句子出現的地方。下面做一個練習。
使用/Line命令在foo.txt中查找Line單詞,鍵入回車后光標第一次出現在第二行處,接下來鍵入n時,光標移動到第三行。重復鍵入n,將會把光標移動到下一個出現Line的位置。
當然除了查找單詞和句子外,還可以用正則表達式進行搜素,在這里不做討論,可以后續學習。
全局查找和替換
Vi使用ex命令來在一定范圍內或者整個文件中進行查找和替換。這里我們以將foo.txt中的Line替換為line為例,執行的命令是:
:%s/Line/line/g
執行結果如下圖所示:

這個命令中各個字段的含義如下表所示:
Item
|
Meaning
|
:
|
The colon character starts an ex command.
|
%
|
Specifies the range of lines for the operation. % is a shortcut
meaning from the first line to the last line. Alternately, the
range could have been specified 1,5 (since our file is five
lines long), or 1,$ which means “from line 1 to the last line in
the file.” If the range of lines is omitted, the operation is onlyperformed on the current line.
|
s
|
Specifies the operation. In this case, substitution (search and
replace).
|
/Line/line/
|
The search pattern and the replacement text
|
g
|
This means “global” in the sense that the search and replace is
performed on every instance of the search string in the line. If
omitted, only the first instance of the search string on each line
is replaced.
|
我們可以再命令的末尾加上c參數,來提供用戶確認的功能。我們執行命令:
:%s/line/Line/gc
來講line還原為Line。
下圖為出現的提示:

屏幕下方出現的替換確認提示符各個字段的含義如下表所示:
Key
|
Action
|
y
|
Perform the substitution.
|
n
|
Skip this instance of the pattern
|
a
|
Perform the substitution on this and all subsequent instances of the pattern
|
q or Esc
|
Quit substituting.
|
l
|
Perform this substitution and then quit. Short for “last.”
|
Ctrl-e, Ctrl-y
|
Scroll down and scroll up, respectively. Useful for viewing
the context of the proposed substitution.
|
當我們鍵入y將進行本次的替換,鍵入n將忽略本次替換并將光標置于下一個待替換的位置,而鍵入a將執行全部的替換并不再提醒。
4.7 Editing Multiple Files 多文件編輯
一次能編輯多個文件通常是方便的。比如你要從一個文件中拷貝內容到另一個文件。
在vi通過在命令行中指定多個文件名,可以一次打開多個文件。
格式如下:
vi file1 file2 file3 ...
為了便于演示,我們首先再建立另外一個文件ls-output.txt,方法如下:
qiaoqiao@ubuntu:~$ ls -l /usr/bin >ls-output.txt
下面同時打開兩個文件如下:
qiaoqiao@ubuntu:~$ vi foo.txt ls-output.txt
我們將首先看到foo.txt中的內容。
多文件之間的切換
當打開多個文件后,我們使用:n和:N命令來在文件之間切換,:n表示切換到下一個文件,而:N則表示切換回上一個的文件。需要注意的是vi將采取一種策略來防止我們在沒有保存當前文件的情況下進行文件切換。要強制進行文件切換我們可以再命令的后面加上感嘆號(!)。
另外vi還提供了:buffers命令來查看當前編輯文件的列表,我們運行該命令將在屏幕下方看到:

列出了打開的文件信息。要在文件之間切換可以使用:buffer加上你想編輯的文件的號碼。例如,你要編輯ls-output.txt,就可以執行:
:buffer 2
命令。
打開額外的文件
添加文件到當前編輯會話也是可以的。ex命令:e緊接文件名就可以打開額外的文件。我們先關閉當前會話,重新單獨打開foo.txt文件。此時要打開另外一個文件,需要鍵入:
:e ls-output.txt
我們可以使用:buffers命令來驗證打開了幾個文件,驗證結果如下圖所示:

需要注意的是,利用e命令打開的文件,不能使用:n或者:N命令來切換,而要用:buffer加上數字來切換文件。
從一個文件中拷貝內容到另外一個文件
當編輯多個文件時,我們希望能從一個文件中拷貝一些內容到我們正在編輯的文件中。在vi中可以通過復制命令即可輕松實現。
首先,我們打開上述兩個文件,使用命令
:buffer 1
切換到第一個文件,顯示如下圖所示:

然后我們在第一行使用yy 命令復制第一行的內容,通過鍵入
:buffer 2
切換到第二個文件,第二個文件顯示如下:

我們將光標定位在第一行,使用p命令,粘貼內容后如下圖所示:

插入一整個文件
Vi中也可以通過命令
:r 加上文件名,向當前編輯的文件中插入文件名指定的文件。
下面來演示。
首先打開一個文件使用命令如下:
qiaoqiao@ubuntu:~$ vi ls-output.txt
打開文件如下圖所示:

在打開的文件中將光標定位在第三行,然后使用命令:
:r foo.txt
結果如下圖所示:

4.8 Saving Our Work 保存工作
就像vi中的其他操作一樣,保存文件的操作也有多種方式。已經講過了:w命令,還有一些其他命令,也很有用。在命令模式下,鍵入ZZ可以保存當前編輯的文件并退出vi。同樣地使用:wq命令將組合了:w和:q命令,既能保存文件又能退出系統。
另外:w可以指定文件名,這樣可以實現另存為功能。例如將foo.txt另存為foo1.txt需要鍵入
:w foo1.txt
需要注意的是,在當前編輯文件另存為另一個文件后,你編輯的文件文件名不變。也就是你繼續編輯的是foo.txt,而不是foo1.txt文件。
總結
Vi的入門命令學習起來也很簡單,下面總結上面的各個命令列在下面供參考學習。
key
|
Moves The Cursor
|
l or Right Arrow
|
Right one character.
|
h or Left Arrow
|
Left one character
|
j or Down Arrow
|
Down one line.
|
k or Up Arrow
|
Up one line
|
0 (zero)
|
To the beginning of the current line.
|
^
|
To the first non-whitespace character on the current line.
|
$
|
To the end of the current line
|
w
|
To the beginning of the next word or punctuation character
|
W
|
To the beginning of the next word or punctuation character.
|
b
|
To the beginning of the previous word or punctuation character.
|
B
|
To the beginning of the previous word, ignoring punctuation characters.
|
Ctrl-f or Page Down
|
Down one page
|
Ctrl-b or Page Up
|
Up one page
|
numberG
|
To line number. For example, 1G moves to the first line of the file.
|
G
|
To the last line of the file
|
Command
|
Append text
|
a
|
Append text after the current cursor
|
A
|
Append text to the end of the current line
|
|
|
Command
|
Open a line
|
o
|
Open a blank line below the current line.
|
O
|
Open a blank line above the current line
|
Command
|
Deletes text(cuts text)
|
x
|
Delete the current character.
|
3x
|
Delete the current character and the next two characters
|
dd
|
Delete the current line.
|
5dd
|
Delete the current line and the next four lines.
|
dw
|
Delete from the current cursor position to the beginning of the next word.
|
d$
|
Delete from the current cursor location to the end of the current line.
|
d0
|
Delete from the current cursor location to the beginning of the line.
|
d^
|
Delete from the current cursor location to the first non-whitespace character in the line.
|
dG
|
Delete from the current line to the end of the file.
|
d20G
|
Delete from the current line to the twentieth line of the file.
|
Command
|
Copies text
|
yy
|
Copy the current line
|
5yy
|
Copy the current line and the next four lines.
|
yW
|
Copy from the current cursor position to the beginning of the next word
|
y$
|
Copy from the current cursor location to the end of the current line.
|
y0
|
Copy from the current cursor location to the beginning of the line
|
y^
|
Copy from the current cursor location to the first non-whitespace character in the line.
|
yG
|
Copy from the current line to the end of the file.
|
y20G
|
Copy from the current line to the twentieth line of the file.
|
Command
|
Paste text
|
p
|
Paste the contents of the buffer after the cursor
|
P
|
Paste the contents before the cursor
|
Command
|
Join lines
|
J
|
Join one line with the one below it
|
Key
|
Editing Multiple Files
|
:n
|
Switch from one file to the next
|
:N
|
Move back to the previous file
|
:buffers
|
View a list of files being edited
|
:buffer number
|
Switch to the specified file the number point to
|
:e filename
|
Add files to our current editing session
|
:r filename
|
Inserts the specified file to the current file
|
更新:
調整vim到適合python格式,在~/.vimrc文件總加入:
set smartindent
set tabstop=4
set shiftwidth=4
set expandtab
即可.
要更快的掌握這些命令需要多加練習,要完全掌握vi的使用,需要學習更多的內容,從事更多的實踐。