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指令表 |