Vi文件編輯器/vi指令表
Vi文件編輯器: 認識vi - 基本入門 - 小技巧 - 進階技巧 - 高階技巧 - Vi變體 (Vim文件編輯器, Vile文件編輯器, BB vi文件編輯器) - vi指令表 |
常見的指令如下
c | 單個字符,例如"a"或"1". |
m | 單個小寫字母, used to mark text. |
string | 字符串, 例如"abc bed". |
pattern | 一串用於查詢的字符,其中可以包含正則表達式,例如"abc"或"^ab[123]". |
myfile | 將要編輯的文件名. |
<CR> | 回車鍵. |
<ESC> | 「Esc」鍵(通常在鍵盤的左上角,並且上面寫著"ESC"或"Esc"). |
^G | Control G,即按下Ctrl鍵的同時,按下G鍵(Ctrl鍵通常有兩個,都在鍵盤的最下面一行). |
vi中的所有命令都是區分大小寫。
Invocation
[編輯]vi myfile | 開啟檔名為myfile的檔案並編輯。如果這個檔名的檔案並不存在,以myfile為名的檔案將會被建立。 |
vi +line myfile | 開啟myfile這個檔案,且游標位於所指定的位置(列)。
|
vi +/string/ myfile | 開啟檔案 myfile with the cursor positioned at the first line containing the string. If the string has spaces it should be enclosed in quotes.
|
vi -r | 列出還原的檔案,當編輯過程被中斷時,可利用這個方法找還原編輯中的文件。 |
vi -r myfile | 開啟檔名為myfile的還原檔案。 |
view myfile | view是 vi的一個唯讀版. 所有 vi 的指令, include those to change the file are allowed and act as in vi. The difference is that normal attempts to save, ZZ or :wq do not work. Instead :x! or :w need to be used. |
vi指令
[編輯]移動游標
[編輯]在絕大多數的系統中,皆可以使用鍵盤上的按鍵來移動vi的游標,進行左移、上一頁、刪除等動作。
G | 移至某行,可以在前面加數字指明行號,例如3G移至第三行,什麼都不加則移至最後一行。 |
h | 游標向左移一個字元。可在h前加上左移字元數,例如5h表示向左移5格。 |
j | 游標向下移一個行數。可在j前加上下移列數,例如5j表示向下移5列。當一段文本較長,換行顯示時,j命令的結果是下移一個物理行,與顯示無關,可在前面加g,下移一個顯示行。 |
k | 游標向上移一個行數。可在k前加上上移列數,例如5k表示向上移5列。當一段文本較長,換行顯示時,j命令的結果是上移一個物理行,與顯示無關,可在前面加g,上移一個顯示行。 |
l | 游標向右移一個字元。可在l前加上右移字元數,例如5l表示向右移5格 |
H | 移到視窗的第一列 |
M | 移到視窗的中間一列 |
L | 移到視窗最底列 |
- | 移到上一列第一個(非空白)字元處。可加數字來指示移動列數。
|
+ | 移到下一列第一個(非空白)字元處。可加數字來指示移動列數。
|
<CR> | Same as +. |
| | 移至第三列,什麼都不加則移至第一列(行首)。 |
w | 移至下一個單詞的開頭(允許換行)。 |
W | 和"w"類似,但把連字符(-)、逗號、句號等視為單詞的一部分。 |
e | 移動到光標所在詞的詞尾,如果光標已經在詞尾或者在兩詞之間,則移動到下一詞的詞尾。 |
E | 和"e"類似,但把連字符(-)、逗號、句號等視為單詞的一部分。 |
b | 游標移動到所在位置前一個字串的第一個字元 |
B | 和"b"類似,但把連字符(-)、逗號、句號等視為單詞的一部分。 |
0 | 移到該行最開始 |
^ | 移至該行的(非空白)第一個字元 |
$ | 移到該行最末端 |
^F | 視窗向下卷動一頁
|
^B | 視窗向上卷動一頁
|
^D | 視窗向下卷動半頁 |
^U | 視窗向上卷動半頁 |
^E | 屏幕向上滾動一行(即把屏幕最下方的下一行顯示出來,把屏幕最上方的一行隱去。如果操作前光標不在屏幕最上面一行,則隨屏幕向上滾動,如果操作前光標已經在屏幕最上面一行,操作後光標並不消失,仍留在新的最上面一行)。 |
^Y | 屏幕向下滾動一行(即把屏幕最上方的上一行顯示出來,把屏幕最下方的一行隱去。如果操作前光標不在屏幕最下面一行,則隨屏幕向下滾動,如果操作前光標已經在屏幕最下面一行,操作後光標並不消失,仍留在新的最下面一行)。 |
插入字元
[編輯]以下這些插入指令會使vi進入插入模式. 插入模式 可以按 ESC 結束.
i | 於游標所在位置,進入插入模式. |
I | 於游標所在該行的開頭,進入插入模式. |
a | 於游標所在位置之後,進入插入模式. |
A | 於游標所在該行的尾端,進入插入模式. |
o | 於游標所在位置之下方,插入新的一行,並進入插入模式. |
O | 於游標所在位置之上方,插入新的一行,並進入插入模式. |
Changing
[編輯]The change commands all select text to be removed, the end of which is indicated by a $. Insert mode is entered and new text overwrites or extends the text. When the <ESC> key is pressed to terminate the insert, any remaining original text is deleted.
Text deleted during a change is placed into the buffer, replacing any text already there. Buffered text can be retrieved by p or P.
C | Change from the cursor position to the end of the line. Can be preceded by a number.
|
cc | Change the current line. Can be preceded by a number.
|
cw | Change the current word. Can be preceded by a number.
|
ce | Exactly the same as cw. |
ctc | Changes from the cursor position to the first instance of the character.
|
cG | Changes from the start of the current line to the end of the file. |
s | Change the character underneath the cursor. Can be preceded by a number.
|
置換
[編輯]r | 用接下來輸入的那個字符替換當前光標下的字符,前面可帶一個數字,例如5ra將從當前光標算起的5個連續字符替換為aaaaa |
R | 字符串替換. 從當前光標處起,R後的每個字符都依次替換掉一個原來的字符,直到按下ESC鍵為止. 前面可帶一個數字,例如5Rab後跟ESC鍵會將從光標處算起的10個字符換為ababababab。與此同時原來的行會存入buffer,並取代其中原來的內容。 |
刪去
[編輯]Each time a delete command is used, the deleted text is placed into the buffer, replacing any text already in the buffer. Buffered text can be retrieved by p or P.
dd | 刪去當前行,前面可帶數字.
|
de | Deletes from the character underneath the cursor to the end of the word. Can be preceded by a number.
|
dE | As de but takes into account punctuation. |
dw | 從光標處的字符刪至下一單詞之前,前面可帶數字.
|
dW | As dw but takes into account punctuation. |
db | Deletes from the left of the cursor to the start of the previous word. Can be preceded by a number.
|
dB | As db but takes into account punctuation. |
dtc | Deletes from the cursor position to before the first instance of the character.
|
dfc | Deletes from the cursor position to the first instance of the character.
|
dG | Deletes from the current line to the end of the file. |
d/string | Deletes from the cursor to the string, either forwards or backwards. |
D | 從光標處刪至行尾. |
d$ | 同D. |
d^ | Deletes from the left of the cursor to the start of the line. |
x | Delete the character underneath the cursor. Can be preceded by a number.
|
X | Delete the character to the left of the cursor, but will not delete the end of line marker or any characters on the next line. Can be preceded by a number.
|
剪下/貼上
[編輯]The yank commands copy text into the vi buffer. Text is also copied into the buffer by delete and change commands. The put or place commands retrieve text from the buffer.
yy | Yanks the current line into the buffer. Can be preceded by a number.
|
Y | Same as yy. |
yw | Yanks from the cursor to the start of the next word into the buffer. Can be preceded by a number.
|
p | If the buffer consists of whole lines, they are inserted after the current line. If it consists of characters only, they are inserted after the cursor. |
P | If the buffer consists of whole lines, they are inserted before the current line. If it consists of characters only, they are inserted before the cursor. |
尋找
[編輯]Searching uses regular expressions.
/pattern | Searches for the string, which could be a regular expression. Searching is from the cursor position downwards, stopping at the first match. If not found, it will continue from the start of the file to the cursor position.
|
/pattern/+ | Goes to the line after the one containing the search string.
|
?pattern | As / but searches upwards. |
?pattern?- | Goes to the line above the one containing the search string.
|
n | Repeat last search. |
N | Repeat last search but in the opposite direction. |
Mark Text
[編輯]Marked lines can be used when changing or deleting text.
mm | Mark the current line with the letter.
|
''''m | Move to the line marked by the letter.
|
Screen Refresh
[編輯]^L | Refresh the screen. |
z<CR>' | 刷新屏幕,是當前行作為屏幕上的第一行出現. 前面可以指定一個行號nz,相當於nG與z<CR>組合.
|
/pattern/z | Finds the line with the first occurrence of string and then refreshes the screen so that it is at the top. |
z. | Refreshes the screen so that the current line is in the middle of the screen. Can be preceded by a line number, in which case the line is at the middle.
|
/string/z. | Finds the line with the first occurrence of string and then refreshes the screen so that it is in the middle. |
z- | Refreshes the screen so that the current line is at the bottom. Can be preceded by a line number, in which case the line is at the bottom.
|
/string/z- | Finds the line with the first occurrence of string and then refreshes the screen so that it is at the bottom. |
其他
[編輯]~ | Changes the case of the character underneath the cursor and moves to the next character. Can be preceded by a number, so that 5~ changes the case of 5 characters. |
. | Repeats the last insert or delete. Can be preceded by a number, dd followed by 5. deletes a line and then deletes another 5 lines. |
% | Moves the cursor to the matching bracket, any of (), [] or {}. |
^G | Temporarily displays a status line at the bottom of the screen. |
:f | Same as ^G. |
J | Joins the next line to the end of the current line. Can be preceded by a number. Both 1J and 2J do the same as J.
<ul |
u | Undoes the last change. A second u puts the change back. |
U | Undoes all changes to the current line. |
^Z | Puts vi into the background, that is control is returned to the operating system. In UNIX, the vi session can be returned to the foreground with fg. |
儲存/離開
[編輯]ZZ | 保存並退出。(用表示」睡覺「的符號代表工作結束了^_^). |
:q | 退出,僅用於文件沒有被修改時. |
:q! | 不保存退出. |
:w | 保存當前文件(不退出).
|
:w! filename | 強制保存為 filename,如果該文件已經存在就覆蓋掉原來的文件。 |
:wq | 保存並退出. |
:x | 保存並退出. |
:x! | Used to save and quit in view. |
檔案
[編輯]:e filename | 退出當前文檔並打開名為 filename 的文檔, 當前文檔不能有未保存的編輯操作。 |
:e + filename | 相當於":e filename"與"G"的組合。退出當前文檔並打開名為 filename 的文檔, 光標出現在某一行行首,當前文檔不能有未保存的編輯操作。"+"後可用數字指定光標所在的行號,什麼都不寫代表最後一行。
|
:e! | 放棄未保存的內容,直接退出當前文件,並重新打開此文件。 |
:e# | 從文件A通過":e"系列的命令打開文件B(並做若干操作)後,在文件B中執行此命令可退出文件B並打開文件A. |
:n | 命令行中有多個文件作為vi的參數被打開時,此命令退出當前文件並打開下一待編輯的文件. |
:n files | Resets the list of files for :n. The current file will be closed and the first file in the list will be opened for editing. |
:r filename | 將文件filename的內容插入到當前文件,r前可以指定插入在某一行的下方,如果沒有指定行號,就從光標的下一行起插入。
|
vi Options
[編輯]All options are ex options, and so require an initial colon.
Default options may be placed into a file in the user's home directory called .exrc. Options in this file do not have the initial colon, e.g.
- set ic
:set all | Displays all the current settings. |
Set on | Set off | Meaning |
:set ignorecase :set ic | :set noignorecase :set noic |
Ignore case. Makes searching case insensitive. |
:set list | :set nolist | Shows control characters. ^I is tab, $ is linefeed. |
:set number :set nu | :set nonumber :set nonu |
Turns on line numbering. |
:set term | Displays the terminal type. |
ex 指令
[編輯]ex commands start with :, which puts vi into last line mode, entered on the last line of the screen. Spaces within the command are ignored.
:! command | Executes the named operating system command and then returns to vi.
|
:sh | Starts up a shell. exit returns to the vi session. |
:vi | Exit last line mode and return to normal command mode. |
ex line commands
[編輯]These commands edit lines and have the following syntax:
- No line number, meaning work on the current line.
- With %, meaning work on all lines.
- A pair of line numbers, such as '3,5' meaning work on lines 3 to 5 inclusive. Either number can be replaced with ., standing for the current line or $ standing for the last line. So .,$ means from the current line to the end of the file and 1,$ means the same as %. Additionally simple arithmetic may be used, so .+1 means the line after the current line, or $-5 means 5 lines before the last line.
co | Copy, followed by the line position to copy to.
|
d | Delete.
|
m | Move, followed by the line position to move to.
|
外部連結k
[編輯]
Vi文件編輯器: 認識vi - 基本入門 - 小技巧 - 進階技巧 - 高階技巧 - Vi變體 (Vim文件編輯器, Vile文件編輯器, BB vi文件編輯器) - vi指令表 |