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 +5 myfile 开启档案myfile,且游标位于第五列。
  • vi + 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 +/"search string"/ myfile 开启档案myfile at the first line containing search string.
vi -r 列出还原的档案,当编辑过程被中断时,可利用这个方法找还原编辑中的文件。
vi -r myfile 开启档名为myfile的还原档案。
view myfile viewvi的一个唯读版. 所有 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 移到视窗最底列

- 移到上一列第一个(非空白)字元处。可加数字来指示移动列数。
  • 10-:上移10列
+ 移到下一列第一个(非空白)字元处。可加数字来指示移动列数。
  • 10+下移10列
<CR> Same as +.
| 移至第三列,什么都不加则移至第一列(行首)。

w 移至下一个单词的开头(允许换行)。
W 和"w"类似,但把连字符(-)、逗号、句号等视为单词的一部分。
e 移动到光标所在词的词尾,如果光标已经在词尾或者在两词之间,则移动到下一词的词尾。
E 和"e"类似,但把连字符(-)、逗号、句号等视为单词的一部分。
b 游标移动到所在位置前一个字串的第一个字元
B 和"b"类似,但把连字符(-)、逗号、句号等视为单词的一部分。

0 移到该行最开始
^ 移至该行的(非空白)第一个字元
$ 移到该行最末端

^F 视窗向下卷动一页
  • 5^F 视窗向下卷动五页
^B 视窗向上卷动一页
  • 5^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.
  • 5C changes 5 lines, the current line and the next 4 lines.

cc Change the current line. Can be preceded by a number.
  • 5cc changes 5 lines, the current line and the next 4 lines.
cw Change the current word. Can be preceded by a number.
  • 5cw changes five words. c5w is the same as 5cw.
ce Exactly the same as cw.
ctc Changes from the cursor position to the first instance of the character.
  • cta changes text up and to, but not including, the first letter 'a'.
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.
  • 5s changes 5 characters, the one under the cursor and the next 4.

置换[编辑]

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 删去当前行,前面可带数字.
  • 5dd 删去5行, d5d5dd相同.
de Deletes from the character underneath the cursor to the end of the word. Can be preceded by a number.
  • 5de deletes five words. d5e is the same as 5de.
dE As de but takes into account punctuation.
dw 从光标处的字符删至下一单词之前,前面可带数字.
  • 5dw 删去5个单词. d5w5dw相同.
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.
  • 5db deletes five words to the left of the cursor.
dB As db but takes into account punctuation.
dtc Deletes from the cursor position to before the first instance of the character.
  • dta deletes text up and to, but not including, the first letter 'a'.
dfc Deletes from the cursor position to the first instance of the character.
  • dfa deletes text up and to, and including, the first letter 'a'.
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.
  • 5x deletes the character underneath the cursor and the next 4 characters.
  • xp swaps the character underneath the cursor with the one to the right of it.
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.
  • 5X deletes 5 characters to the left of the cursor.

剪下/贴上[编辑]

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.
  • 5yy yanks five lines.
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.
  • 5yw yanks five words.

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.
  • /abc seaches for the first occurrence of abc.
/pattern/+ Goes to the line after the one containing the search string.
  • /abc/+3 goes to the third line after the one containing abc.
?pattern As / but searches upwards.
?pattern?- Goes to the line above the one containing the search string.
  • ?abc?-3 goes to the third line above the one containing abc.
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.
  • ma marks the current line with the letter a.
''''m Move to the line marked by the letter.
  • 'a moves to the line marked by a.

Screen Refresh[编辑]

^L Refresh the screen.
z<CR>' 刷新屏幕,是当前行作为屏幕上的第一行出现. 前面可以指定一个行号nz,相当于nG与z<CR>组合.
  • 35z 光标跳至第35行,并滚动屏幕使之显示在屏幕第一行.
/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.
  • 35z. refreshes the screen so that line 35 is in 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.
  • 35z- refreshes the screen so that line 35 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
  • 3J joins three lines together, the current line and the next two lines.
  • 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 myfile可将文件另存为myfile.
    :w! filename 强制保存为 filename,如果该文件已经存在就覆盖掉原来的文件。
    :wq 保存并退出.
    :x 保存并退出.
    :x! Used to save and quit in view.

    档案[编辑]

    :e filename 退出当前文档并打开名为 filename 的文档, 当前文档不能有未保存的编辑操作。
    :e + filename 相当于":e filename"与"G"的组合。退出当前文档并打开名为 filename 的文档, 光标出现在某一行行首,当前文档不能有未保存的编辑操作。"+"后可用数字指定光标所在的行号,什么都不写代表最后一行。
    • :e +5 myfile 退出当前文件并开始编辑 myfile, 光标出现在第5行行首.
    :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前可以指定插入在某一行的下方,如果没有指定行号,就从光标的下一行起插入。
    • :r myfile 将文件myfile插入到光标所在行之下.
    • :5r myfile将文件myfile插入到第5行下方.

    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.
    • :! ls runs the UNIX ls command.
    :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:

    1. No line number, meaning work on the current line.
    2. With %, meaning work on all lines.
    3. 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.
    • :co 5 copies the current line and places it after line 5.
    • :1,3 co 4 copies lines 1 to 3 and places after line 4.
    d Delete.
    • :d deletes the current line.
    • :.,.+5d delete the current line and the next 5 lines.
    • :%d deletes all lines.
    m Move, followed by the line position to move to.
    • :m 10 moves the current line and places it after line 10.
    • :1,3 m 4 moves lines 1 to 3 and places after line 4.

    外部链接k[编辑]

    vim Official Reference Manual

    Vi文件编辑器: 认识vi - 基本入门 - 小技巧 - 进阶技巧 - 高阶技巧 - Vi变体 (Vim文件编辑器, Vile文件编辑器, BB vi文件编辑器) - vi指令表