命令行大全

vi

舞夕之 发表于 2020-08-21 16:28浏览次数:

vi是一个面向显示的交互式文本编辑器:终端的屏幕充当您正在编辑的文件的窗口。您对该文件所做的更改将反映在您看到的内容中。

查看英文版

目录:

1 vi 运行系统环境

2 vi 语法

3 使用 Vi

4 在vi中移动

5 简单的更改

6 重新排列和复制文本

7 高级命令

8 专题

9 技术细节

10 快速参考

vi 运行系统环境

unix

Unix&Linux

vi 语法

vi [ -| -s ] [-l] [-L] [-R] [ -r [ filename ] ] [-S] [-t tag] [-v] [-V]
   [-x] [-w] [-n ] [-C] [+command | -c command ] filename
选项
-|-s 禁止所有交互式用户反馈。在处理编辑器脚本时,这很有用。
-l 设置用于编辑LISP程序。
-r 列出由于编辑器或系统崩溃而保存的所有文件的名称。
-rfilename 编辑器或系统崩溃后编辑文件名。恢复崩溃发生时缓冲区中文件名的版本。
-L 与-r相同
-R 只读模式;设置了只读标志,以防止意外覆盖文件。
-S 此选项结合使用与-t标签选项告诉VI的标签文件可能不进行排序,并且,如果二进制搜索(这依赖于一个排序的标签文件)标签没有找到它,要慢得多线性搜索也应该完成。由于线性搜索的速度很慢,因此大标签文件的用户应确保对标签文件进行排序,而不要使用此标志。创建标记文件通常会产生排序的标记文件。有关标签文件的更多信息,请参见ctags。
-ttag 编辑包含标签tag的文件,并将编辑器置于其定义处。
-V 详细模式。当通过标准输入读取ex命令时,输入将回显到标准错误。在shell脚本中处理ex命令时,这可能很有用。
-X 加密选项;使用时,vi模拟ex的X命令并提示用户输入密钥。该密钥用于使用crypt函数的算法对文本进行加密和解密。的X命令使一个受过教育的猜测,以确定文本是否在被加密或不读出。临时缓冲区文件也使用-x选项键入的密钥的转换版本进行加密。如果输入了空的加密密钥(即,在提示后立即按下返回键),则文件不会被加密。这是解密用错误键入的加密密钥(例如,退格键或撤消密钥)错误加密的文件的好方法。
-wn 将默认窗口大小设置为n。在较慢的速度线上使用编辑器时,这很有用。
-C 加密选项;与-x选项相同,除了vi模拟ex的C命令。C命令类似于ex的X命令,除了假定读入的所有文本均已加密。
+command|-ccommand 通过执行指定的编辑器命令(通常是搜索或定位命令)来开始编辑。
filename 要编辑的文件。
vi [ -| -s ] [-l] [-L] [-R] [ -r [ filename ] ] [-S] [-t tag] [-v] [-V]
   [-x] [-w] [-n ] [-C] [+command | -c command ] filename
options
-|-s Suppress all interactive user feedback. This is useful when processing editorscripts.
-l Set up for editingLISPprograms.
-r List the name of all files saved as the result of an editor or system crash.
-rfilename Editfilenameafter an editor or system crash. Recovers the version offilenamethat was in the buffer when the crash occurred.
-L Same as-r
-R Readonlymode; the readonly flag is set, preventing accidental overwriting of the file.
-S This option is used in conjunction with the-ttag option to tellvithat the tags file may not be sorted and that, if the binary search (which relies on a sorted tags file) fortagfails to find it, the much slower linear search should also be done. Since the linear search is slow, users of large tags files should ensure that the tags files are sorted rather than use this flag. Creation of tags files normally produces sorted tags files. Seectagsfor more information on tags files.
-ttag Edit the file containing the tagtag, and position the editor at its definition.
-V Verbosemode. Whenexcommands are read by means ofstandard input, the input will be echoed to standard error. This may be useful when processingexcommands within shell scripts.
-x Encryptionoption; when used,visimulates theXcommand ofexand prompts the user for a key. This key is used to encrypt anddecrypttext using thealgorithmof the crypt function. TheXcommand makes an educated guess to determine whether text read in is encrypted or not. The temporarybufferfile is encrypted also, using a transformed version of the key typed in for the-xoption. If an empty encryption key is entered (that is, if the return key is pressed right after theprompt), the file will not be encrypted. This is a good way to decrypt a file erroneously encrypted with a mistyped encryption key, such as a backspace or undo key.
-wn Set the default window size ton. This is useful when using the editor over a slow speed line.
-C Encryption option; same as the-xoption, except thatvisimulates the C command ofex. The C command is like theXcommand ofex, except that all text read in is assumed to have been encrypted.
+command|-ccommand Begin editing by executing the specified editor command (usually a search or positioning command).
filename The file to be edited.

查看英文版

查看中文版

使用 Vi

vi是一个面向显示的交互式文本编辑器:终端的屏幕充当您正在编辑的文件的窗口。您对该文件所做的更改将反映在您看到的内容中。

使用vi可以很容易地在文件中的任何位置插入文本。大多数vi命令在文件中四处移动光标。您可以以字符,单词,句子和段落为单位前后移动光标。某些运算符,例如d表示删除,c表示更改,可以与运动命令结合使用,以自然的方式对整个单词,段落等进行操作。

编辑文件

启动vi会话的最常见方法是告诉它要编辑哪个文件。要编辑名为filename的文件,请使用以下命令:

vi filename

屏幕将清除,并且文件文本将出现在屏幕上。如果文件名尚不存在,vi会在一个新文件中启动您,并告诉您保存您的工作时,它将使用您指定的文件名。

编辑的副本:“缓冲区”

编辑器不会直接修改您正在编辑的文件。而是在内存中将此文件的副本称为buffer。除非您将所做的更改写回到原始文件中,否则实际上不会影响文件的内容。

方向键

在大多数终端上,您可以使用箭头键来移动光标。向左和向右将光标向左或向右移动一个字符,然后向上或向下将光标向上或向下移动一行。移动光标的另一种方法是使用h,j,k和l键:

H 剩下
j
k 向上
l

有点历史性的琐事:之所以使用hjkl键,是因为Bill Joy开发的原始计算机系统没有单独的箭头键。而是,键盘使用hjkl键作为箭头键。

铁杆vi用户将告诉您,hjkl键是移动光标的唯一“真正”方法,因为您可以将手保持在键入位置,但是您应该使用最适合自己的方法。

特殊键:Esc,返回和删除

Escape键在vi中很重要:它用于取消您开始键入的任何命令,并在您插入或添加文本后返回到命令模式。如果您忘记了自己所处的模式或正在做什么,则可以使用Escape键。您可以多次单击它以返回命令模式并取消您一直在做的任何事情。

Return键(或“ Enter键”)用于完成某些命令并执行它们。

Delete键是另一个要按下的“安全”键。它会中断编辑器的所有操作,并使您返回到编辑器的默认状态,有时也称为“静态”状态。

退出vi

ZZ命令将把缓冲区的内容写入正在编辑的文件中,并退出vi。

如果没有要保存的更改,也可以使用命令:q退出编辑器。如果您所做的更改尚未保存,则编辑器将不允许您以这种方式退出。

如果要退出并放弃所做的更改,可以告诉编辑器,您可以通过使用:q!退出来确定您知道自己在做什么。。这将立即退出vi,所有未保存的更改都将丢失。

viis an interactive text editor that is display-oriented: the screen of yourterminalacts as a window into the file you are editing. Changes you make to the file are reflected in what you see.

Usingviyou can insert text anywhere in the file very easily. Most of thevicommands move thecursoraround in the file. You can move the cursor forward and backward in units of characters, words, sentences, and paragraphs. Some of the operators, likedfor delete andcfor change, can be combined with the motion commands to make them operate on entire words, paragraphs, etc., in a natural way.

Editing A File

The most common way to start avisession is to tell it which file to edit. To edit a file namedfilename, use the command:

vi filename

The screen will clear and the text of your file will appear on the screen. Iffilenamedoesn't exist yet,viwill start you in a new file, and when you tell it to save your work, it will use thefilenamethat you specified.

The Editor's Copy: The Buffer"

The editor does not directly modify the file you are editing. Instead, it makes a copy of this file in memory called the buffer. You do not actually affect the contents of the file until you write the changes you've made back into the original file.

Arrow Keys

On most terminals, you can use the arrow keys to move the cursor around. Left and right moves the cursor left or right one character, and up and down move the cursor up or down one line. The other way to move the cursor is with theh,j,k, andlkeys:

h left
j down
k up
l right

A bit of historical trivia: thehjklkeys are used because the original computer system Bill Joy developed with did not have separate arrow keys. Instead, the keyboard usedhjklkeys as the arrow keys.

Hardcoreviusers will tell you that thehjklkeys are the only true" way to move the cursor because you can keep your hands in the typing position, but you should use whatever works best for you.

Special Keys: Esc, Return, and Delete

The Escape key is important invi: it is used to cancel any command you've started typing, and to return to command-mode after you've been inserting or adding text. Escape is a safe key to hit if you've lost track of what mode you're in or what you're doing. You can hit it several times to return to command mode and cancel anything that you've been doing.

The Return (or Enter") key is used to complete certain commands and execute them.

The Delete key is another safe" key to press. It interrupts anything that the editor has been doing and brings you back to the default state of the editor, sometimes also called the quiescent" state.

Exiting vi

The commandZZwill write the contents of the buffer into the file you are editing, and quitvi.

If there are no changes to be saved, you can also quit the editor with the command:q. If you have made changes that haven't been saved yet, the editor won't let you exit this way.

If you want to quit and discard the changes you've made, you can tell the editor you're sure you know what you're doing by quitting with the command:q!. This will exitviimmediately, and any unsaved changes will be lost.

查看英文版

查看中文版

在vi中移动

滚动和分页

编辑器有几个命令可用于在文件内移动。其中最有用的一个是Control-D(也表示为^ D)。此命令在文件中“向下滚动”(D代表“向下”)。^ U(“向上”)类似:它在文件中向上滚动一页。这两个命令还将光标移动到新位置。

如果只想查看文件的更多内容,但将光标留在原处,则可以使用^ E在屏幕底部“显示”另外一行。^ Y(不代表任何东西,但在键盘上的U旁边)在屏幕顶部显示一行。

还有其他方法可以在文件中四处移动。键^ F和^ B向前和向后移动一页(称为“分页”),在屏幕之间保持几行连续性,因此可以使用这些键而不是^ D和^ U来读取文件。如果你希望。

注意“滚动”(^ U,^ D)和“分页”(^ B,^ F)之间的区别。滚动留下更多的上下文,但是分页仅留下几行。由您决定哪一个更容易四处移动和读取文件。

正在搜寻

将您自己放置在文件中的另一种方法是给编辑器一个要搜索的字符串。如果键入“ /”,后跟一个字符串,然后按Enter键,则编辑器将在文件中搜索该字符串的下一个出现位置,并将光标放在该位置。按n将转到此后的下一个事件。

如果要向后搜索字符串而不是正向,请使用?代替/。在一个?字符串搜索,按n键将带您进入相同(向后)方向的连续出现。

如果您给编辑器提供的搜索字符串不存在于文件中,则编辑器将打印一条消息,通知您找不到它,并且光标将不会移动。

如果只希望搜索字符串在行的开头匹配,则应以插入符号(“ ^ ”)开头。要仅在行末匹配,请在搜索字符串后加一个美元符号(“ $ ”)。所以使用命令

/^mystring

仅当“ mystring ”位于一行的开头时,才会找到匹配项。同样,命令

/mystring $

只匹配行末尾出现的“ mystring ”。

“转到”

命令G后面带有数字时,将“转到”一行:它将转到该行号并将光标定位在该行。如果使用不带数字的G,它将光标移动到文件的最后一行。

因此,使用命令

1G

将光标移动到文件的第一行;

30G

将光标移到第30行,然后

G

不管数字是多少,都将光标移动到最后一行。

找出文件中的位置

您可以按^ G在文件中找到您的位置。此底线将显示您正在编辑的文件的名称,当前行数,缓冲区中的行数以及光标当前位于缓冲区中的百分比。

回到你刚才的地方

每当您以上述任何一种方式移动光标时,都可以通过键入``(将两个反引号引起来)将光标移回原来的位置。所以,如果您输入

G

移至文件末尾,但随后您意识到您需要回到原来的位置(无论您是否记得在哪里),可以键入

``

回到您的原始位置。执行搜索后,此方法也有效。请记住此命令,因为它非常有用!

行超出文件末尾

如果您接近文件结尾,并且最后一行不在屏幕底部,则vi将在实际文件结尾之后的任何行上打印波浪号(“ 〜 ”)。这表示文件的最后一行在屏幕上。也就是说,任何显示“ 〜 ”的行都在文件末尾。例如,当您开始编辑新文件时,您会注意到这一点:每行都将以波浪号开头。

在屏幕上四处走动

正如我们已经提到的,您可以使用箭头键或h,j,k和l左右移动光标一个光标,或上下移动一行。

您也可以使用+移至下一行或-移至上一行。这类似于使用向上和向下(或Ĵ和ķ),所不同的是将光标将自动被放置在第一非空白上的行字符。

这些键中的任何一个,如果它们会将您带到屏幕的第一行之前或最后一行之后,都将沿正确的方向滚动显示一行。按返回键与按+相同。您还可以将光标定位在当前显示的文本屏幕的顶部,中间或底部:

H将带您到屏幕顶部(“主页”)行。在H之前加上数字将带您到主行加上该行数。例如,3H将光标移至屏幕上显示的第三行文本。

M是类似的命令:它将光标移动到屏幕中间。

L将光标移至屏幕的最后一行。如果您在数字前面加上数字,它将光标移至显示的最后一行之前的那一行。因此5L将光标移至当前显示的倒数第二行。

行内移动

w将光标移至该行的下一个单词,b将光标移至上一个单词。

e使您前进到当前单词的末尾,而不是下一个单词的末尾。

如果单词被标点,例如用撇号或逗号,则w和b将在标点处停止。如果改为使用W和B,它们将在忽略标点的同时移动光标。

这些命令全部换行到一行的末尾(或开头),如果它们对下一个或上一个单词的搜索走了那么远,则移至上一行或下一行。

摘要:运动钥匙

这是我们刚刚讨论过的移动键的概述,以及一些其他功能。

空格键 将光标前进一个位置
^ B 向后翻页
^ D 向下滚动
^ E 在屏幕底部露出一条线
^ F 向前翻页
^ G 位置状态检查
^ N 下一行,同一列
^ P 前一行,同一列
^ U 向上滚动
^ Y 在屏幕顶部显示一条线
+ 下一行,在第一个非空白字符处
- 前一行,第一个非空白字符
/string 向前扫描字符串。如果未指定字符串,则扫描最后一个扫描的字符串。
? 向后扫描string。如果未指定字符串,则扫描最后一个扫描的字符串。
B 向后倒一个单词,忽略标点符号
linenumberG 转到指定行号(一个整数)。默认为最后一行。
H 将光标移到屏幕的第一行
M 屏幕中线
L 屏幕的最后一行
W 转发一个单词,忽略标点符号
b 向后倒一个字
e 当前单词的结尾
n /或下一个实例?搜索模式
w 下一个字
Scrolling And Paging

The editor has several commands for moving around within your file. One of the most useful of these isControl-D(also represented as^D). This command "scrolls" down in the file (theDstands for "down").^U("up") is similar: it scrolls up one page in the file. Both of these commands will also move the cursor to the new location.

If you just want to see more of the file, but leave the cursor where it is, you can use^Eto "expose" one more line at the bottom of the screen.^Y(which doesn't stand for anything, but it's next to theUon the keyboard) exposes one line at the top of the screen.

There are other ways to move around in the file. The keys^Fand^Bmove forward and backward one page (this is called "paging"), keeping a couple of lines of continuity between screens so that it is possible to read through a file using these rather than^Dand^Uif you wish.

Notice the difference between "scrolling" (^U,^D) and "paging" (^B,^F). Scrolling leaves more context, but paging only leaves you a couple of lines. It's up to you which one is easier for moving around and reading your file.

Searching

Another way to position yourself in the file is by giving the editor astringto search for. If you type "/" followed by a string of characters and press Enter, the editor will search for the next occurrence of this string in your file, and place the cursor there. Pressingnwill go to the next occurrence after that.

If instead of forward, you want to search backward for a string, use?instead of/. In a?string search, pressingnwill take you to successive occurrences in the same (backward) direction.

If the search string you give the editor is not present in the file, the editor will print a message letting you know it couldn't find it, and your cursor will not be moved.

If you want your search string only to match if it's at the beginning of a line, you should begin the search string with acaret("^"). To match only at the end of a line, end the search string with a dollar sign ("$"). So using the command

/^mystring

will only find a match if "mystring" is at the very begining of a line. Similarly, the command

/mystring$

will only match any occurrences of "mystring" that are at the very end of a line.

"Goto"-ing

The commandG, when preceded by a number, will "goto" a line: it will go to that line number and position the cursor there. If you useGwith no number, it moves the cursor to the last line of the file.

So, using the command

1G

Moves your cursor to the first line in the file;

30G

Moves your cursor to line 30, and

G

Moves your cursor to the last line, whatever number that may be.

Finding Out Where You Are In The File

You can find out where you're at in the file by pressing^G. This sill show you the name of the file you are editing, the number of the current line, the number of lines in the buffer, and the percentage of the way through the buffer your cursor is currently at.

Getting Back To Where You Just Were

Any time you move the cursor in any of these ways, you can move the cursor back to where it just was by typing``(twoback quotes). So, if you type

G

to move to the end of the file, but then you realize you need to get back to where you were (whether you remember where that is or not), you can type

``

To get back to your original location. This also works after performing a search. Remember this command, because it's very useful!

Lines Past The End Of Your File

If you're near the end of your file, and the last line is not at the bottom of the screen,viwill print atilde("~") on any lines after the end of your actual file. This indicates that the last line of your file is on the screen; that is, any lines showing "~" are past the end of your file. You'll notice this, for instance, when you start editing a new file: every line will begin with a tilde.

Moving Around On The Screen

As we've already mentioned, you can use the arrow keys orh,j,k, andlto move the cursor left and right one cursor, or up and down one line.

You can also use+to move to the next line or-to move to the previous line. This is similar to usingupanddown(orjandk), except that your cursor will automatically be placed at the first non-whitespacecharacter on the line.

Any of these keys, if they would take you before the first line on the screen or after the last line, will scroll the display one line in the correct direction. Pressing theReturnkey is the same as pressing+. You can also position the cursor at the top, middle, or bottom of the currently-displayed screen of text:

Hwill take you to the top ("home") line on the screen. PrecedingHwith a number will take you to the home line plus that number of lines. For instance,3Hmoves your cursor to the third line of text displayed on the screen.

Mis a similar command: it moves your cursor to the middle of the screen.

Lmoves your cursor to the last line of the screen. If you precede it with a number, it moves your cursor to that number of lines before the last line displayed. So5Lmoves your cursor to the fifth-to-last line currently displayed.

Moving Within A Line

wwill advance the cursor to the next word on the line, andbwill back up the cursor to the previous word.

eadvances you to the end of the current word rather than the beginning of the next word.

If words are punctuated, for instance with an apostrophe or a comma,wandbwill stop at the punctuation. If you useWandBinstead, they move the cursor while ignoring punctuation.

These commands all wrap at the end (or beginning) of a line, moving to the previous or next line if their search for the next or previous word goes that far.

Summary: Movement Keys

Here's a recap of the movement keys we just discussed, and a few extras.

Spacebar advance the cursor one position
^B page backward
^D scroll down
^E expose a line at the bottom of the screen
^F page forward
^G Positional status check
^N next line, same column
^P previous line, same column
^U scroll up
^Y expose a line at the top of the screen
+ next line, at the first non-whitespace character
- previous line, at the first non-whitespace character
/string Scan forward forstring. If nostringis specified, scans for the laststringscanned for.
? Scan backwards forstring. If nostringis specified, scans for the laststringscanned for.
B backwards one word, ignoring punctuation
linenumberG Go to specifiedlinenumber(aninteger). Defaults to the last line.
H move cursor to first line of screen
M middle line of screen
L last line of screen
W forward a word, ignoring punctuation
b backwards a word
e end of current word
n next instance of/or?search pattern
w next word

查看英文版

查看中文版

简单的更改

更改文件的最基本命令是i(插入)命令。键入i后,您键入的所有内容(直到您按Esc键)都将光标位置插入到缓冲区中。

i命令的伴随命令是a(“追加”)命令。操作就像我,除了它开始插入文本后的光标位置,而不是在光标所在位置。

有时,您想在光标所在的行之后立即开始在新行上进行编辑。为此,请使用o命令。要在光标所在的行之前开始新行,请使用O命令。就像i和a一样,您键入的任何文本都会插入到缓冲区中,直到您按Esc为止。

使用以下任何命令插入文本时,可以使用^ W删除整个单词,并将光标留在前一个单词之后一个空格

需要注意的一件事是,当您退格时,您不会擦除任何未插入的字符。另外,您不能退格到上一行;当您到达当前行的开头时,backspacing将停止。

进行小更正

在vi中进行小的修正很容易。如果您需要删除不应该出现的字符,请将光标移到该字符上方,然后按x删除它。您还可以在按x之前提供一个数字,它将删除那么多字符。因此,如果您有“ 发现 ” 一词,并且将光标移到v上并键入

4x 

...您将剩下“ 迪斯科 ” 一词。

如果要用另一个字符替换单个字符,请将光标移到该字符上,然后按r,然后按应该替换它的字符。因此,如果您键入“ mistoke ”,然后将光标移到“ o ”上并键入

ra

... o将被替换为a,使您留下“ 错误 ” 一词。

在[R命令是相似的,但你可以用它来代替不止一个性格比较; 如果您按R键并开始输入,则您键入的每个字符都将替换那里已经存在的每个字符。因此,如果您有“ 错误” 一词,然后将光标移到字母t上,然后键入

Rsile

...您会看到“ 错误 ”一词变成了“ 导弹 ” 一词。您可能还会注意到,如果在此模式下退格,则将逐字母还原原始文本。完成后,按Esc键停止替换字符。

经营者

命令d用作删除操作符:按d,然后移动键将删除光标将移动到的任何位置。例如,键入dw将删除下一个单词,而键入db将向后删除到上一个单词。键入d SPACE将删除一个字符;这与输入x相同。

另一个有用的运算符是c,代表变化。命令cw将更改一个单词:它将删除单词的所有内容,并将您置于插入模式下以键入应替换它的文本。

重复更改

每次使用命令进行更改时,都可以键入“ 。 ”重复更改。因此,如果您使用dw删除单词,请输入。也将删除下一个单词。您可以输入来重复一遍。几次,如省略号(“ ... ”)。

在线操作

输入dd将删除整行。

键入cc将更改整行,将其删除并将其置于插入模式。

您可以一次在dd或cc前面加上一个数字来删除或更改多行。例如,5dd将删除5行。您还可以提供类似dL的命令,该命令将从光标中删除每一行,直到屏幕上的最后一行,包括d3L,从光标中删除每一行到倒数第二行。当您进行这样的大更改时,特别是如果所做的更改超出了在一个屏幕中可以看到的范围时,编辑器会向您显示一条消息,告诉您已完成的操作。

撤消

要撤消上一次更改,请键入u。要重做(撤消撤消),请再次键入u。

与vim不同,vi仅提供一级撤消。但是,您可以使用U(大写U)撤消对当前行所做的任何更改。按U可以将当前行恢复为开始更改前的状态。

摘要:进行更改
SPACE 将光标前进一个位置
^W 插入时删除单词
. 重复上一次更改命令
O 打开并在当前行上方输入新行
u 撤消您对当前行所做的更改
a 在光标后附加文本
c 将您指定的对象更改为之后输入的文本
d 删除您指定的对象
i 在光标之前插入文本
o 在当前行下方打开并输入新行
u 撤消上一次更改

The most basic command for making changes to your file is the i (insert) command. After you type i, everything you type until you hit Esc is inserted at your cursor position into the buffer.

The companion to the i command is the a ("append") command. This works exactly like i, except it starts inserting the text after the cursor location, not at the cursor location.

Sometimes, you want to start editing on a new line right after the line your cursor is at. To do this, use the o command. To start a new line before the line your cursor is at, use the O command. Just like with i and a, any text you type will be inserted into the buffer until you press Esc.

While you are inserting text with any of these commands, you can use ^W to erase a whole word and leave your cursor one space after the previous word

One thing to note is that when you backspace, you will not erase any characters that you haven't inserted. Also, you can't backspace to the previous line; backspacing will stop when you get to the beginning of the current line.

Making Small Corrections

It's easy to make small corrections in vi. If you need to remove a character that shouldn't be there, move the cursor over it and press x to delete it. You can also supply a number before pressing x, and it will delete that many characters. So if you have the word "discovery", and you move the cursor over the v and type

4x

...you will be left with the word "disco".

If you want to replace a single character with another character, move the cursor over it and press r and then the character that should replace it. So, if you type "mistoke", and move the cursor over the "o" and type

ra

...the o will be replaced with an a, leaving you with the word "mistake."

The R command is similar, but you can use it to replace more than one character; if you press R and start typing, every character you type will replace every character that was already there. So, if you have the word "mistake", and you move the cursor over the letter t and type

Rsile

...you will see that the word "mistake" has turned into the word "missile". You might also notice that if you backspace in this mode, your original text will be restored, letter by letter. When you're done, press Esc to stop replacing characters.

Operators

The command d acts as a delete operator: pressing d and then a movement key will delete wherever the cursor would have moved. For instance, typing dw will delete the next word, and typing db will delete backwards to the previous word. Typing dSPACE will delete one character; this is the same as typing x.

Another useful operator is c, which stands for change. The command cwwill change a single word: it will delete everything to the end of a word, and place you into insert mode to type the text that should replace it.

Repeating A Change

Any time you make a change with a command, you can type "." to repeat the change. So if you delete a word with dw, typing . will delete the next word as well. You can repeat this over and over by typing .several times, like an ellipsis ("...").

Operating On Lines

Typing dd will delete an entire line.

Typing cc will change an entire line, deleting it and placing you in insert mode.

You can delete or change more than one line at a time by preceding ddor cc with a number. For instance, 5dd will delete 5 lines. You can also give a command like dL which will delete every line from the cursor up to and including the last line on the screen, or d3L which will delete every line from the cursor to the third-to-last line. When you make big changes like this, especially if you make changes that go beyond what you can see in one screen, the editor gives you a message telling you what's been done.

Undoing

To undo the last change, type u. To re-do (to undo an undo), type uagain.

Unlike vim, vi only offers one level of undo. However, you can use U(capital U) to undo any changes that were made to the current line; pressing U restores the current line to the way it was before you started changing it.

Summary: Making Changes
SPACE advance the cursor one position
^W erase a word during insert
. repeats the last change command
O opens and inputs a new line, above the current line
U undoes the changes you made to the current line
a appends text after the cursor
c changes the object you specify to the text you type in after
d deletes the object that you specify
i inserts text before the cursor
o opens and inputs new lines, below the current line
u undoes the last change

查看英文版

查看中文版

重新排列和复制文本

低级角色动作

fx移动光标,找到当前行的下一个x字符。之后,如果按;您的光标将移动到下一个相同字符的位置。F是类似的命令,但在同一行中向后搜索,而不是向前搜索。

的˚F和˚F命令可以被组合,例如,与d命令。键入dfx会删除光标中的所有内容,直到该行中下次出现x为止。有时您想要删除(但不包括)下次出现的字符x。在这种情况下,请使用命令dtx。t代表“至”。

行的开头用零(“0”)表示,插入符号(“^”)表示该行的第一个非空白字符,行的结尾用美元符号(“$”)表示。如果按^,光标将放置在该行的第一个非空白字符处;如果按0,光标将位于行的开头;如果按$,光标将位于行的末尾。

要删除从光标到行尾的所有内容,请输入d $。要删除从光标到行首的所有内容,请键入d0...,依此类推。

高级文本对象

有时以整个句子为单位移动会很有帮助。在(和)命令分别移动到一个和下一个句子的开头。因此,命令d)删除当前句子末尾的所有内容;和d(删除当前句子开头的所有内容;如果您已经在句子开头,则删除整个先前的句子。

句子定义为以句点(“。”),感叹号(“!”)或问号(“?”)结尾,其后是行尾或空格。空格,行尾或。,?或!之后可能会出现任意数量的结尾),],“和'字符。

的{和}命令上移动段落,和操作[[和]在部分移动。可以通过特殊的宏或行首的换页符来定义“节”。

重新排列和复制文本

编辑器有一个特殊的未命名缓冲区,用于保存最后删除或更改的文本,还有一组命名缓冲区a到z,可用于保存文本副本并在文件中移动文本。

的y命令“猛拉”的对象的副本到匿名的缓冲器。如果以缓冲区名称开头,则会将该对象放入命名缓冲区。指定缓冲区的方式是双引号(“),然后是字母a-z,然后是y命令。因此” ay将对象拖入名为a的缓冲区中。

可以使用命令p或P将缓冲区中的文本(未命名的缓冲区或您命名的缓冲区之一)粘贴到文档中。p将缓冲区内容粘贴到光标位置之后(很像一个追加),P将缓冲区内容粘贴到光标位置(很像一个插入符)。

如果您拉动的文本形成一行的一部分,或者是诸如句子之类的对象,且部分跨越多条行,那么当您将文本放回原位时,它将被放置在光标之后(如果使用P)。如果选中的文本形成整行,则它们将作为整行放回,而不会更改当前行。在这种情况下,粘贴行为更像是o或O命令。因此,命令YP将复制当前行,并将您置于该副本的位置,该位置位于当前行之前。Y是yy的方便缩写。

命令Yp还将复制当前行,并将其放置在当前行之后。您可以给Y多行拉动,从而复制多行。例如,“3YP”将复制三行,并将它们放置在当前行之前。

要在缓冲区中移动文本,您需要在一个地方删除它,然后将其放回另一个地方。您可以在删除操作之前加上要在其中存储文本的缓冲区的名称,例如“ a5dd”,它将在命名缓冲区a中删除5行。然后,您可以将光标移动到这些行的最终放置位置并执行“ ap”或“ ap”以将其放回去。实际上,您可以通过以下命令来切换和编辑另一个文件:放回行:ename形式,其中name是您要编辑的另一个文件的名称。如果进行了更改,则必须编写当前编辑器缓冲区的内容(或丢弃它们),然后编辑器才允许您编辑新文件。

普通的delete命令会将文本保存在未命名的缓冲区中,以便普通的粘贴会将其放回到文件中。但是,更改文件时,未命名的缓冲区会丢失,因此要将文本从一个文件移动到另一个文件,您应该使用命名缓冲区。

移动和重新排列文本的摘要
^ 在线上的第一个非空白字符
0 线的起点
$ 队伍的尽头
) 转发到下一个句子休息
( 返回上一个句子中断
} 转发到下一段
{ 后退到上一段
]] 转到下一节
[[ 返回上一节
fx 在行中向前查找字符x
Fx 在行中向后查找字符x
p 在光标后粘贴文本
P 在光标之前粘贴文本
y yan
Y 拉整条线
t 转发到字符x,以进行操作
TX 返回字符x,以进行操作
Low-Level Character Motions

fx moves the cursor finds the next x character on the current line. After that, if you press ; your cursor moves to the next occurrence of the same character. F is a similar command, but searches backwards in the same line, rather than forwards.

The f and F commands can be combined, for example, with the dcommand. Typing dfx will delete everything from the cursor through the next occurrence of x in the line. Sometimes you want to delete up to, but not including, the next occurrence of character x. In this case, use the command dtx. t stands for "to".

The beginning of the line is represented by zero ("0"), the caret ("^") represents the first non-whitespace character on the line, and the end of the line is represented by the dollar sign ("$"). If you press ^, the cursor will be placed at the first non-whitespace character of the line; if you press 0, the cursor will be placed at the beginning of the line; and if you press $, the cursor will be placed at the end of the line.

To delete everything from the cursor to the end of the line, type d$. To delete everything from the cursor to the beginning of the line, type d0... and so on.

Higher-Level Text Objects

Sometimes it's helpful to move around in units of entire sentences. The ( and ) commands move to the beginning of the previous and next sentences, respectively. So the command d) deletes everything through the end of the current sentence; and d( deletes everything through the beginning of the current sentence, or the entire previous sentence if you're already at the beginning of a sentence.

A sentence is defined as ending at a period ("."), exclamation point ("!"), or question mark ("?") which is followed either by the end of the line or a space. Any number of closing ), ], ", and ' characters may appear after the ., ?, or ! before the space or the end of the line.

The { and } commands move over paragraphs, and the operations [[and ]] move over sections. A "section" can be defined by a special macro, or by a formfeed at the beginning of a line.

Rearranging And Duplicating Text

The editor has a special unnamed buffer where the last deleted or changed-away text is saved, and a set of named buffers a through zwhich you can use to save copies of text and to move text around in your file (or files).

The y command "yanks" a copy of an object into the unnamed buffer. If preceded by a buffer name, it places this object into the named buffer. The way to specify the buffer is double quotes ("), then a letter a-z, then the y command. So "ay will yank an object into the buffer named a.

Text in a buffer (either the unnamed buffer or one of your named buffers) can be pasted into your document with the commands p or P. pwill paste the buffer contents after the cursor location (much like an append), and P will paste the buffer contents at the cursor location (much like an insert).

If the text which you yank forms part of a line, or is an object such as a sentence which partially spans more than one line, then when you put the text back, it will be placed after the cursor (or before it if you used P). If the yanked text forms whole lines, they will be put back as whole lines, without changing the current line. In this case, the paste acts much more like an o or O command. So, the command YP will make a copy of the current line and place you at the location of the copy, which is placed before the current line. Y is a convenient abbreviation for yy.

The command Yp will also make a copy of the current line, and place it after the current line. You can give Y a number of lines to yank, and thus duplicate several lines; for instance, 3YP will duplicate three lines, and place them before the current line.

To move text within the buffer, you need to delete it in one place, and put it back in another. You can precede a delete operation by the name of a buffer in which the text is to be stored, as in "a5dd, which deletes 5 lines into the named buffer a. You can then move the cursor to the eventual resting place of these lines and do a "ap or "aP to put them back. In fact, you can switch and edit another file before you put the lines back, by giving a command of the form :e name, where name is the name of the other file you want to edit. You will have to write the contents of the current editor buffer (or discard them) if you have made changes, before the editor will allow you to edit a new file.

An ordinary delete command saves the text in the unnamed buffer, so that an ordinary paste will place it back in the file. However, the unnamed buffer is lost when you change files, so to move text from one file to another you should use a named buffer.

Summary of Moving And Rearranging Text
^ First non-whitespace character on line
0 Beginning of the line
$ End of the line
) Forward to next sentence break
( Backward to previous sentence break
} Forward to next paragraph break
{ Backward to previous paragraph break
]] Forward to next section break
[[ Backward to previous section break
fx Find character x forward in the line
Fx Find character x backward in the line
p Paste text after cursor
P Paste text before cursor
y yank
Y yank a whole line
t forward to character x, for operations
Tx back to character x, for operations

查看英文版

查看中文版

高级命令

编写,退出和编辑新文件

到目前为止,我们已经看到了如何将文件写入磁盘并退出(ZZ),或者简单地将文件写入并继续编辑(:w)。

如果您更改了文件,但想要不保存而退出,请使用命令:q!。:q命令退出编辑器,但只有在自上次写入(上次保存)以来未进行任何更改的情况下,它才允许您执行此操作。但是,感叹号告诉编辑者“是的,我真的很想这样做。”

同样,您可以使用:ename命令编辑另一个文件,其中name是您要编辑的文件的名称。但是,如果您尚未保存更改,则编辑器将不允许您执行此操作。但是,如果再次使用感叹号(:e!name),编辑器将按照您的要求进行操作:它将放弃所做的更改并打开文件名进行编辑。

如果您没有指定文件名,只需使用命令:e!,编辑器将重新加载您正在处理的文件的保存版本。就像其他文件编辑器中的“还原”命令一样。它将放弃您的更改,并恢复到文件的最后保存版本。

逃脱到壳

如果要运行shell命令而不退出编辑器,则可以使用命令:!命令。系统将运行命令command,命令完成后,编辑器将要求您按Enter键,然后返回到编辑屏幕。当要求您按Enter时,您还可以输入另一个:命令。这将运行下一个命令而不重绘屏幕。

如果要完全放在外壳的命令提示符下,请使用命令:sh。这将使您进入外壳,可以像往常一样在其中使用它。完成后,键入Control-D(^ D),您将返回到vi。

在许多系统上,您也可以随时在编辑器内简单地键入^ Z放入您的shell中。从技术上讲,这会将vi置于后台。要返回vi,请在shell命令提示符下键入fg(“ foreground”),vi将再次成为您的前台进程。

标记和返回

如前所述,命令``(两个反引号)使您在通过/,/或G之类的命令移动光标后返回到上一个位置。您还可以使用单字母标签标记您在文件中的位置,并在以后返回这些标记。使用命令mx进行标记,其中x是要分配给标记的字母。要返回标记的位置,可以使用命令`x

例如,如果键入ma,则将字母a标记为当前光标位置。然后,如果将光标移动到文件中的其他位置,则始终可以使用命令`a返回到该行。标记会一直保留,直到您开始编辑另一个文件,或者直到您通过将标记设置在其他位置来覆盖标记为止。

当使用诸如d之类的运算符并引用标记的行时,有时您会希望引用所标记的行,而不是该行中的实际光标位置。在任何时候,您都可以使用'x(而不是`x)来引用行本身。因此,命令d`a将删除从当前光标位置到标记a的确切位置的所有内容,而命令d'a将删除直到标记a所在的整行(包括该行)的所有内容。

在没有运算符的情况下使用,'x会将您带到标记x所在行的第一个非空白字符。

调整屏幕

如果屏幕图像由于任何原因弄乱了,您可以使用Control-L(^ L)刷新屏幕。

如果您想随时将屏幕居中放置在光标上,请输入z-。要调整屏幕,使光标所在的行位于屏幕顶部,请输入z并按Enter。要调整屏幕,使光标所在的行位于屏幕底部,请键入z。。

zz与z-相同,它将使屏幕居中,使光标所在的线位于屏幕的中心。

Writing, Quitting, and Editing New Files

So far we have seen how to write our file to disk and quit (ZZ), or simply write our file and continue editing(:w).

If you have changed your file but you want to quit without saving, use the command:q!. The command:qquits the editor, but it will only let you do this if you haven't made any changes since your last write (your last save). The exclamation point, however, tells the editor "yes, I really want to do this."

Similarly, you can edit another file with the:enamecommand, wherenameis the name of the file you want to edit. But the editor won't let you do this if you have unsaved changes. However, if you use the exclamation mark again (:e!name), the editor will do as you ask: it will discard your changes and open filenamefor editing.

If you don't specify a file name, and just use the command:e!, the editor will re-load your saved version of the file you're working on. This is like the "revert" command in other file editors. It will discard your changes, and revert to the last-saved version of the file.

Escaping To The Shell

If you want run a shell command without exiting the editor, you can use the command:!command. The system will run the commandcommand, and when the command finishes, the editor will ask you to press Enter before returning you to the editing screen. You can also give another:command when it asks you to press Enter; this will run the next command without redrawing the screen.

If you want to drop entirely to your shell's command prompt, use the command:sh. This will drop you into your shell, where you can use it as you normally would; when you're done, type Control-D (^D), and you will be returned tovi.

On many systems, you can also simply type^Zfrom within the editor at any time to drop into your shell. This technically putsviin the background. To return tovi, typefg("foreground") at your shell command prompt, andviwill become your foreground process again.

Marking And Returning

As already mentioned, the command``(two back quotes) returns you to the previous place after a motion of the cursor by a command such as/,/, orG. You can also mark your position in the file with single-letter tags and return to these marks later. The marking is done with the commandmx, wherexis the letter you want to assign to the mark. To return to the place that you marked, you can use the command`x

For example, if you typema, you will mark the current cursor position with the lettera. Then, if you move the cursor anywhere else in the file, you can always return to that line with the command`a. Marks last until you start editing another file, or until you overwrite the mark by setting it somewhere else.

When using operators such asdand referring to marked lines, sometimes you'll want to refer to the line that you marked, and not the actual cursor position within the line. At any time, you can use'x(as opposed to`x) to refer to the line itself. So, the commandd`awill delete everything from the current cursor position up to the exact position at marka, whereas the commandd'awill delete everything up to (and including) the entire line where markaresides.

Used without an operator,'xmoves you to the first non-whitespace character in the line where markxresides.

Adjusting The Screen

If the screen image is messed up for any reason, you can refresh the screen with Control-L (^L).

If you want to center the screen on your cursor at any time, typez-. To adjust the screen so that the line with your cursor is at the top of the screen, typezand press Enter. To adjust the screen so that the line with your cursor is at the bottom of the screen, typez..

zzis the same asz-, and will center the screen so that the line with your cursor is at the center of the screen.

查看英文版

查看中文版

专题

设置选项和使用编辑器启动文件

编辑器有一组选项,您可以使用:set命令设置。要将选项opt设置为val,请使用命令:set opt = val。

切换选项都可以用下面的命令来设置设置 选择,和取消用命令设置没有选择。

您可以设置以下一些最有用的选项:

选项名称 默认值 描述
autoindent noai 自动缩进
autowrite noaw 编辑时自动写入文件
ignorecase noic 搜索时忽略大小写
lisp nolisp (,{,)和}命令处理S表达式
list nolist 标签打印为^ I ; 标有$的行尾
magic nomagic 人物。,[和*在扫描中是特殊的(“魔术”)
number nonu 行以行号开头显示
paragraphs para=IPLPPPQPbpP LI宏名称以段落开头
redraw nore 在哑终端上模拟智能终端
sections sect = NHSHH HU宏名称开始新的部分
shiftwidth sw = 8 <,>和其他“移位”命令的移位距离
showmatch nosm 显示匹配(或{位置如)或}被输入,例如
showmode nosmd 显示输入模式说明
slowopen slow 插入期间推迟显示更新
term dumb 您使用的终端类型

这些语句可以放在环境变量 EXINIT中,也可以在运行vi时通过在它们前面加上:来给出。

您可以随时在vi中输入:set来查看所有选项及其设置的列表。要查看特定选项的当前设置,请使用命令:set opt ?。

set可以缩写为se。可以在一行上设置多个选项,例如:se ai aw nu。

用:set设置的选项仅在当前编辑器会话中持续。

如果要将缺省设置放入环境变量EXINIT中,以便每次运行vi时都将其加载,则可以用竖线(“ | ”)分隔它们,从而在一行上指定多个命令。例如,如果您的外壳是csh,则可以使用以下外壳命令设置EXINIT:

setenv EXINIT'set ai aw terse | map @ dd | map#x'
恢复丢失的线

如果您删除多行然后希望将它们返回,则可能会遇到问题。但是,编辑器将最后删除的9个文本块保存在一组编号为1到9的寄存器中。您可以通过命令“ n p ”将第n个先前删除的文本恢复到文件中。这些文件与命名缓冲区相似,不同之处在于它们引用了先前删除的文本块的编号。

通常,当您键入一个句点(“ 。 ”)时,它会重复您所做的最后一次更改。作为一种特殊情况,当该时间段重复引用带编号的缓冲区的更改时,该数字在重复命令之前递增。让我们看看它是如何工作的,以及为什么有用:

假设您删除了一些重要的内容,并且知道它在您之前的九个块删除之一中,但是您不确定哪一个。如果要返回查看先前删除的所有九个块,可以按照以下方法进行操作:可以使用以下命令

" 1p

粘贴最近删除的文本的内容(缓冲区号1)。如果这不是您想要的文本,则可以撤消(u)并使用重复该命令。,粘贴缓冲区2的命令。换句话说,输入命令

"1pu.u.u.u.

和打字一样

"1pu"2pu"3pu"4pu"5p

并且您可以轻松地返回编号的缓冲区,直到找到要返回的文本。

当然,在上例中,可以使用命令P代替p来将文本放在光标之前而不是光标之后。

恢复丢失的文件

如果系统崩溃,您可以在几处更改后恢复所做的工作。回到系统后,使用以下命令启动vi:

vi -r name

其中name是您正在编辑的文件的名称。这将使您的工作恢复到尽可能靠近上次停止的位置。

如果您不使用名称指定文件,而是输入

vi -r

那么您将收到可以恢复的文件列表。

连续文字输入

当您输入大量文本时,在右边界附近自动断行很方便。您可以通过给出命令:se wm = 10来引起这种情况。这将导致所有行在距屏幕右边缘至少10列的位置断开。

如果编辑器中断了输入行,并且您希望将其放回原处,则可以告诉它使用J将这些行连接起来。您可以像3J中连接3行那样给J计数要连接的行数。如果合适,编辑器在连接的行的接合处提供空白,并将光标留在该空白处。如果不想,可以用x杀死空白。

编辑程序的功能

编辑器具有许多用于编辑程序的命令。程序编辑与文本编辑最不同的地方是程序主体的缩进结构。编辑器有一个自动缩进帮助你生成正确缩进项目设施。

要启用此功能,可以给出命令:se ai。现在尝试用o打开新行,并在几个标签后的行中键入一些字符。如果现在开始另一行,请注意,编辑器在该行的开头提供空白以使其与上一行对齐。您不能在此缩进上退格,但是可以使用^ D键在提供的缩进上退格。

每次键入^ D时,您都将备份一个位置,通常到8列边界。此金额是可设置的;编辑器有一个称为shiftwidth的选项,您可以设置该选项来更改此值。尝试输入命令:se sw = 4,然后再次尝试自动缩进。

为了在程序中左右移动,有运算符<和>。这些将您指定的行右移或左移一个shiftwidth。尝试<<和>>左右移动一行,而 L左右移动显示的其余部分。

如果您有一个复杂的表达式,并且希望了解括号的匹配方式,请将光标放在左括号或右括号上,然后点击%。这将显示匹配的括号。这也适用于括号{和}和方括号[和]。

如果您正在编辑C程序,则可以使用[[和]]键前进或后退到以{开头的行,即一次声明一个函数。当]]与运算符一起使用时,它将在以}开头的行之后停止。有时对y]]有用。

缓冲区的过滤部分

您可以使用运算符在缓冲区的各个部分上运行系统命令!(感叹号)。本质上,它将文件的一部分作为标准输入发送到shell命令,然后用命令的标准输出替换文本的该部分。

总而言之,指定发送什么文本和发送什么命令的方式是!(运动说明符)命令名。该运动-符可以W¯¯对下一个字操作,例如,或}下一个段落,等命令的名称是shell命令,任何参数可能需要。

这对于使用sort命令进行排序特别有用。您可以使用它来对文件中的行进行排序。例如,如果您要输入一个随机单词的列表,每行一个,并以空行结束,那么您将备份到列表的开头,并给出命令!} sort,这将对数据进行排序您刚刚输入就在眼前。这是一个非常强大的功能!自己尝试一下。

用于编辑LISP代码的命令

如果您正在编辑一个LISP程序,你应该将选项口齿不清做SE口齿不清:。这将更改(和)命令以在s表达式上向前和向后移动。的{和}命令是像(和),但在原子不停止。这些可用于跳到下一个列表,或快速通过注释。

该自动缩进选项适用不同的LISP,在第一供应缩进对齐参数到最后打开的列表。如果没有这样的参数,则缩进比最后一级多两个空格。

还有另一个对LISP键入有用的选项,即showmatch选项。尝试使用:se sm进行设置,然后尝试键入“ ( “,一些单词,然后输入“” )。注意,光标显示的“位置( ”其中匹配“ )简单地”。仅当匹配的“ ( ”出现在屏幕上,并且光标停留在那里最多一秒钟时,才会发生这种情况。

编辑器还具有一个运算符,用于重新对齐现有的行,就像使用lisp和autoindentset 键入它们一样。这是=运算符。在函数开头尝试命令=%。这将重新对齐函数声明的所有行。

编辑LISP时,[[和]]前进和后退至以(开头的行,这对于处理整个函数定义很有用。


vi有一个无参数的宏功能,该功能可让您进行设置,以便在单击单个键时,编辑器的作用就好像您已经按了更长的键序列一样。如果您发现自己重复输入相同的命令序列,则可以进行设置。

宏一般有两种:

  1. 将宏主体放在命名宏缓冲区x中的位置。然后,您可以键入@ x来调用宏。该@可以再接再@重复上次的宏。
  2. 您可以将vi中的map命令(通常在EXINIT环境变量中)与以下形式的命令一起使用:

    :map lhs rhsCR
    将lhs映射到rhs中。有限制:lhs应该是一个按键(1个字符或一个功能键),因为必须在1秒钟之内输入(除非设置了notimeout,在这种情况下,您可以根据需要缓慢键入它,并且vi将等待)您要在回声之前完成它)。lhs不能超过10个字符,rhs不能超过100 个字符。要在lhs或rhs中添加空格,制表符或换行符,应使用^ V对其进行转义。如果在vi内给出map命令,可能需要将^ V加倍。,而不是ex。rhs内部的空格和制表符无需转义。

因此,要使q键写入并退出编辑器,可以给出以下命令

:map q :wq^V^VCR CR

这意味着每当您键入q时,就好像您已经键入了四个字符:wqCR一样。需要一个^ V,因为没有它,回车符将结束:命令,而不是成为地图定义的一部分。之所以有两个^ V,是因为在vi内必须键入两个^ V才能得到一个。第一个CR是rhs的一部分,第二个CR 终止:命令。

宏可以用删除

unmap lhs

如果宏的lhs是“ #0 ”到“ #9 ”,则它将映射特定的功能键,而不是2个字符的“ #”序列。为了使没有功能键的终端可以访问此类定义,形式“ #x ”表示所有终端上的功能键x(并且无需在一秒钟内键入)。可以使用宏以通常的方式更改字符“ # ”:

:map ^V^V^I #

例如使用标签。这不会影响仍然使用#的map命令,而不会影响可视模式的调用。

的撤消命令反转整个宏呼叫作为一个单元,如果所作的任何更改。

在单词映射表之后放置“ ! ”会导致映射应用于输入模式,而不是命令模式。因此,要使^ T在输入模式下与4个空格相同,可以键入:

:map! ^T ^Vb/b/b/b/

其中b /是空白。所述^ V是必要的,以防止坯料被取作之间的空白LHS和RHS。

单词缩写

单词缩写与输入模式下的宏类似。这使您可以键入一个简短的单词,并将其扩展为一个或多个更长的单词。这些命令是:abbreviate和:unabbreviate(:ab和:una),并且语法与:map相同。例如:

:ab eecs Electrical Engineering and Computer Sciences

导致“ eecs ” 一词始终更改为“ 电气工程和计算机科学 ”一词。单词缩写与宏的区别在于仅影响整个单词。如果将“ eecs ”键入为较大单词的一部分,则将其保留下来。同样,分词在键入时也会回显。无需将缩写作为单个击键,因为它应该与宏相同。

Setting Options, and Using Editor Startup Files

The editor has a set of options, which you can set with the:setcommand. To set optionoptto valueval, use the command:setopt=val.

Toggle options can be set with the commandssetopt, and unset with the commandset noopt.

Here are some of the most useful options you can set:

option name default value description
autoindent noai Supply indentation automatically
autowrite noaw Automatically write to file while editing
ignorecase noic Ignore case when searching
lisp nolisp (,{,), and}commands deal with S-expressions
list nolist Tabs print as^I; end of lines marked with$
magic nomagic The characters.,[, and*are special ("magical") in scans
number nonu Lines are displayed prefixed with their line numbers
paragraphs para=IPLPPPQPbpP LI Macro names which start paragraphs
redraw nore Simulate a smart terminal on a dumb terminal
sections sect=NHSHH HU Macro names which start new sections
shiftwidth sw=8 Shift distance for<,>, and other "shift" commands
showmatch nosm Show matching(or{locations as)or}is typed, for example
showmode nosmd Show input mode description
slowopen slow Postpone display updates during inserts
term dumb The kind of terminal you are using

These statements can be placed in theenvironment variableEXINIT, or can be given while you are runningviby preceding them with a:, as described above.

You can view a list of all options and their settings by typing:setwithinviat any time. To view the current setting of a particular option, use the command:setopt?.

setcan be abbreviated asse. Multiple options can be set on one line, for instance:se ai aw nu.

Options set with:setonly last for your current editor session.

If you want to place your default settings into the environment variableEXINITso that they are loaded every time you runvi, you can specify multiple commands on a single line by separating them with the vertical bar ("|"). For instance, if your shell iscsh, you could setEXINITwith the following shell command:

setenv EXINIT 'set ai aw terse|map @ dd|map # x'
Recovering Lost Lines

You might have a problem if you delete several lines and then want them back. However, the editor saves the last 9 deleted blocks of text in a set of registers numbered1through9. You can get thenth previous deleted text back in your file by the command"np. These are similar to your named buffers, except they refer to the number of the previous block of text that was deleted.

Usually, when you type a single period ("."), it repeats the last change you've made. As a special case, when this period repeats a change that refers to a numbered buffer, the number is incremented before the command is repeated. Let's see how this works, and why it's useful:

Let's say you deleted something important, and you know it's in one of your nine previous block-deletes, but you're not sure which one. If you want to go back and look at all nine previously deleted blocks, here's how you could do it: you could use the command

"1p

To paste the contents of the most recently-deleted text (buffer number1). If that's not the text you wanted, you could then undo (u) and repeat the command with., pasting the command of buffer2. In other words, typing the command

"1pu.u.u.u.

is the same as typing

"1pu"2pu"3pu"4pu"5p

and you can go back in the numbered buffers easily until you find the text you want to have back.

The commandPcan, of course, be used instead ofpin the above example, to place the text before the cursor instead of after it.

Recovering Lost Files

If the system crashes, you can recover the work you were doing to within a few changes. When you get back into your system, use this command to startvi:

vi -r name

Wherenameis the name of the file you were editing. This will recover your work to a point as close as possible to where you left off.

If you don't specify a file withnameand instead just type

vi -r

then you will receive a list of files which can be recovered.

Continuous Text Input

When you are typing in large amounts of text it is convenient to have lines broken near the right margin automatically. You can cause this to happen by giving the command:se wm=10. This causes all lines to be broken at a space at least 10 columns from the right hand edge of the screen.

If the editor breaks an input line and you wish to put it back together you can tell it to join the lines withJ. You can giveJa count of the number of lines to be joined as in3Jto join 3 lines. The editor supplies white space, if appropriate, at the juncture of the joined lines, and leaves the cursor at this white space. You can kill the white space withxif you don't want it.

Features For Editing Programs

The editor has a number of commands for editing programs. The thing that most distinguishes editing of programs from editing of text is the indented structure to the body of the program. The editor has anautoindentfacility for helping you generate correctly indented programs.

To enable this facility you can give the command:se ai. Now try opening a new line withoand type some characters on the line after a few tabs. If you now start another line, notice that the editor supplies white space at the beginning of the line to line it up with the previous line. You cannot backspace over this indentation, but you can use^Dkey to backtab over the supplied indentation.

Each time you type^Dyou back up one position, normally to an 8 column boundary. This amount is settable; the editor has an option calledshiftwidthwhich you can set to change this value. Try giving the command:se sw=4and then experimenting withautoindentagain.

For shifting lines in the program left and right, there are operators. These shift the lines you specify right or left by one shiftwidth. Try<>which shift one line left or right, andLshifting the rest of the display left and right.

If you have a complicated expression and wish to see how the parentheses match, put the cursor at a left or right parenthesis and hit%. This will show you the matching parenthesis. This works also for braces{and}, and brackets[and].

If you are editingCprograms, you can use the`and`keys to advance or retreat to a line starting with a{, i.e. a function declaration at a time. When]]is used with an operator it stops after a line which starts with}; this is sometimes useful withy]].

Filtering Portions of the Buffer

You can run system commands over portions of the buffer using the operator!(the exclamation point). Essentially, it sends a section of your file asstandard inputto a shell command, then replaces that portion of your text with the standard output of the command.

All together, the way to specify what text to send and what command to send it to, is!(movement-specifier) command-name. Themovement-specifiercould bewfor operating on the next word, for instance, or}for the next paragraph, etc. Thecommand-nameis the shell command, and any arguments it may take.

This is especially useful for sorting using thesortcommand. You can use this to sort lines in your file. For instance, if you were to type in a list of random words, one per line, and end them with a blank line, then you backed up to the beginning of the list and gave the command!}sort, this would sort the data you just typed in, right before your eyes. This is a very powerful function! Try it for yourself.

Commands For Editing LISP Code

If you are editing aLISPprogram you should set the optionlispby doing:se lisp. This changes the(and)commands to move backward and forward over s-expressions. The{and}commands are like(and)but don't stop at atoms. These can be used to skip to the next list, or through acommentquickly.

Theautoindentoption works differently for LISP, supplying indent to align at the firstargumentto the last open list. If there is no such argument then the indent is two spaces more than the last level.

There is another option that is useful for typing in LISP, theshowmatchoption. Try setting it with:se smand then try typing a "(", some words, and then a ")". Notice that the cursor shows the position of the "(" which matches the ")" briefly. This happens only if the matching "(" is on the screen, and the cursor stays there for at most one second.

The editor also has an operator to realign existing lines as though they had been typed in withlispandautoindentset. This is the=operator. Try the command=%at the beginning of afunction. This will realign all the lines of the function declaration.

When you are editing LISP, the`and`advance and retreat to lines beginning with a(, and are useful for dealing with entire function definitions.

Macros

vihas a parameterlessmacrofacility, which lets you set it up so that when you hit a single keystroke, the editor will act as though you had hit some longer sequence of keys. You can set this up if you find yourself typing the same sequence of commands repeatedly.

There are two general kinds of macros:

  1. Ones where you put the macro body in a named macro bufferx. You can then type@xto invoke the macro. The@may be followed by another@to repeat the last macro.
  2. You can use the map command fromvi(typically in yourEXINITenvironment variable) with a command of the form:

    :map lhs rhsCR
    mappinglhsintorhs. There are restrictions:lhsshould be one keystroke (either 1 character or one function key) since it must be entered within one second (unlessnotimeoutis set, in which case you can type it as slowly as you wish, andviwill wait for you to finish it before it echoes anything). The lhs can be no longer than 10 characters, therhsno longer than 100. To get a space, tab ornewlineintolhsorrhsyou should escape them with a^V. It may be necessary to double the^Vif the map command is given insidevi, rather than inex. Spaces and tabs inside therhsneed not be escaped.

Thus to make theqkey write and exit the editor, you can give the command

:map q :wq^V^VCR CR

which means that whenever you typeq, it will be as though you had typed the four characters:wqCR. A^V's is needed because without it the carriage return would end the:command, rather than becoming part of the map definition. There are two^V's because from withinvi, two^V's must be typed to get one. The first CR is part of therhs, the second terminates the:command.

Macros can be deleted with

unmap lhs

If thelhsof a macro is "#0" through "#9", this maps the particular function key instead of the 2 character "#" sequence. So that terminals without function keys can access such definitions, the form "#x" will mean function keyxon all terminals (and need not be typed within one second). The character "#" can be changed by using a macro in the usual way:

:map ^V^V^I #

to use tab, for example. This won't affect themapcommand, which still uses#, but just the invocation from visual mode.

Theundocommand reverses an entire macro call as a unit, if it made any changes.

Placing a "!" after the word map causes the mapping to apply to input mode, rather than command mode. Thus, to arrange for^Tto be the same as 4 spaces in input mode, you can type:

:map! ^T ^Vb/b/b/b/

whereb/is a blank. The^Vis necessary to prevent the blanks from being taken as white space between thelhsandrhs.

Word Abbreviations

A feature similar to macros in input mode is word abbreviation. This allows you to type a short word and have it expanded into a longer word or words. The commands are:abbreviateand:unabbreviate(:aband:una) and have the same syntax as:map. For example:

:ab eecs Electrical Engineering and Computer Sciences

causes the word "eecs" to always be changed into the phrase "Electrical Engineering and Computer Sciences". Word abbreviation is different from macros in that only whole words are affected. If "eecs" were typed as part of a larger word, it would be left alone. Also, the partial word is echoed as it is typed. There is no need for an abbreviation to be a single keystroke, as it should be with a macro.

查看英文版

查看中文版

技术细节

显示屏中的线表示

编辑器将长长的逻辑行折叠到显示中的许多物理行上。使行前进的命令使逻辑行前进,并且将以一个动作跳过一行的所有段。命令|将光标移动到特定的列,这可能对于将光标移到长线的中间以将其分成两部分很有用。

编辑器仅在显示屏上显示整行。如果显示屏上没有足够的空间容纳逻辑行,则编辑器将物理行留空,仅在该行上放置一个@作为占位符。当您在哑终端上删除行时,编辑器通常只会将@行清除以节省时间(而不是重写屏幕的其余部分)。您始终可以通过提供^ R命令来最大化屏幕上的信息。

如果需要,可以使编辑器在显示屏上的每一行之前放置行号。输入命令:se nu启用此功能,并输入命令:se nonu将其关闭。通过给出命令:se list,可以将制表符表示为^ I并以“$”表示行尾。:se nolist将其关闭。

最后,当文件的最后一行在屏幕中间时,将显示仅包含字符“〜”的行。这些代表超出文件逻辑末尾的物理行。

更多文件操作命令

下表列出了使用vi时可以使用的文件操作命令。

:w 写回更改
:wq 写并退出
:x 编写(如有必要)并退出(与ZZ相同)。
:ename 编辑文件名
:e! 重新编辑,放弃更改
:wname 编辑,从头开始
:e +n 编辑,从第n行开始
:e # 编辑备用文件
:wname 写文件名
:w!name 覆盖文件名
:x,ywname 写行X通过ÿ到名
:rname 将文件名读入缓冲区
:r !cmd 将cmd的输出读取到缓冲区
:n 编辑参数列表中的下一个文件
:n! 编辑下一个文件,放弃对当前文件的更改
:nargs 指定新的参数列表
:tatag 编辑包含标签tag的文件,位于标签

所有这些命令后均带有CR或Esc。最基本的命令是:w和:e。对单个文件的正常编辑会话将以ZZ命令结束。如果您要进行长时间的编辑,则可以在进行大量编辑后偶尔给出:w命令,然后以ZZ结尾。当您编辑多个文件时,可以用:w结尾一个文件,并通过给出:e命令开始编辑新文件,或者设置自动写入并使用:nfile。

如果您对文件的编辑者副本进行了更改,但又不想将其写回,则必须输入!!在命令之后,您将否则使用;这将迫使编辑器放弃您所做的任何更改。请谨慎使用。

的:电子命令可以给出+参数在该文件的末尾开始,或+Ñ参数在一行开始Ñ。实际上,n可以是任何不包含空格的编辑器命令,有用的是类似+ /pat或+?的扫描。拍拍。在为e命令形成新名称时,可以使用字符%替换为当前文件名,或使用字符#替换为备用文件名。备用文件名通常是您键入的姓氏,而不是当前文件。因此,如果您尝试执行:e并得到尚未写入文件的诊断信息,则可以给出:w命令,然后给出:e#命令以重做先前的:e。

可以通过找出该绑定到使用被写入的范围的线写缓冲区的一部分到一个文件^ g,和之后给予这些数字:和前瓦特,通过分离,的。您也可以用m标记这些行,然后在w命令上使用格式为'x,'y的地址。

您可以使用:r命令在当前行之后将另一个文件读入缓冲区。您可以类似地从命令中读取输出,只需使用!cmd而不是文件名即可。

如果要连续编辑一组文件,可以在命令行上给出所有名称,然后使用命令:n依次编辑每个文件。也可以通过给:n命令一个文件名列表或一个要扩展的模式来重新指定要编辑的文件列表,就像您在初始vi命令中指定的那样。

如果您正在编辑大型程序,您会发现:ta命令非常有用。它利用功能名称及其位置的数据库(可以由ctags之类的程序创建)来快速查找您提供其名称的功能。如果:ta命令要求编辑器切换文件,则必须:w或放弃任何更改,然后再切换。您可以重复:ta命令,不带任何参数来再次查找相同的标签。

有关搜索字符串的更多信息

当使用/和?在文件中搜索字符串时。,编辑器通常会将您置于字符串的下一个或上一个出现的位置。如果您使用的运算符是d,c或y,那么您很可能希望影响到包含模式的行之前的行。你可以给一个搜索形式/拍/ -ñ来指代ñ“日行下一行之前包含拍拍,或者您可以使用+而不是-来指代线包含一个后拍。如果您不提供行偏移量,那么编辑器将影响到匹配位置的字符,而不是整行。因此使用“+0”影响匹配的行。

通过提供命令:se ic,可以使编辑器在搜索时忽略单词的大小写。:se noic命令关闭此功能。

提供给搜索的字符串实际上可能是正则表达式。如果您不需要或不需要此功能,则应

set nomagic

在您的EXINIT中。在这种情况下,只有字符^和$在模式中是特殊的。然后,字符\也很特殊(因为它在系统中的每个地方都是最多的),可用于获取扩展的模式匹配工具。在向前扫描中的/之前或\之前还必须使用\。在任何情况下都可以向后扫描。下表列出了设置魔术后的扩展形式。

^ 在模式的开头,匹配行的开头
$ 在模式末尾,与行尾匹配
. 匹配任何字符
\ < 匹配单词的开头
\> 匹配单词的结尾
[str] 匹配str中的任何单个字符
[str] 匹配任何不在str中的单个字符
[x-y] 匹配x和y之间的任何字符
* 匹配任意数量的前面的模式

如果您使用魔法模式,则。[和*原语以\开头。

有关输入模式的更多信息

在输入模式下,可以使用许多字符进行更正。下表总结了这些内容。

^H 删除最后一个输入字符
^W 删除由b定义的最后一个输入词
erase 您的擦除字符,与^ H相同
Kill 您的杀死角色,删除此行的输入
\ 逃脱后面的^ H,然后擦除并杀死
Esc 结束插入
Del 中断插入,异常终止
CR 开始新的一行
^D 自动缩进
0^D 杀死所有自动缩进
^^D (caret-control-D)与0 ^ D相同,但恢复缩进的下一行
^V 引用文件中的下一个非打印字符

对输入进行更正的最常用方法是键入^ H来纠正单个字符,或者键入一个或多个^ W来支持不正确的单词。如果在常规系统中将#用作擦除字符,它将像^ H一样工作。

您的系统终止字符(通常为@,^ X或^ U)将删除您在当前行上输入的所有内容。通常,您既不能擦除行边界周围的输入,也不能擦除使用此插入命令未插入的字符。要在开始新行之后对上一行进行更正,您可以按Esc键结束插入,移至上方进行更正,然后返回要继续的位置。附加在当前行末尾的命令A通常对于继续操作很有用。

如果您希望输入擦除或杀死字符(例如#或@),则必须在其前面加上\,就像在普通系统命令级别上那样。键入非打印字符到文件的更一般的方式是用先于他们^ V。所述^ V回波作为^字符在其上光标休止符。这表明编辑器希望您键入控制字符。实际上,您可以键入任何字符,然后它将被插入文件中。

如果您正在使用自动缩进,你可以通过它提供通过键入缩进BACKTAB^ d。这备份到一个shiftwidth边界。这仅在提供的autoindent之后立即起作用。

使用自动缩进时,您可能希望在行的左边缘放置一个标签。可以轻松地做到这一点的方法是输入^然后^ d。编辑器将光标移到左边距为一行,然后在下一行恢复上一个缩进。如果您希望取消所有缩进并且不让它回到下一行,也可以键入0,然后紧跟一个^ D。

仅大写的终端

如果您的终端只有大写字母,您仍然可以通过使用常规系统约定在此类终端上打字来使用vi。通常将键入的字符转换为小写,并且可以在它们前面加上\来键入大写字母。字符{〜}|`在此类终端上不可用,但您可以将它们转义为\(\ ^\)\!\'。这些字符以与键入相同的方式显示在显示屏上。

Vi和Ex

vi实际上是在ex编辑器中进行编辑的一种模式。运行vi时,可以通过给出命令Q转到ex的面向行的编辑器。上面介绍的所有:命令都可以在ex中获得。同样,大多数ex命令可以使用:从vi调用。只要给他们不带:,然后给他们加上CR。

在极少数情况下,vi可能会发生内部错误。在这种情况下,您将得到诊断,并处于ex的命令模式下。然后,您可以保存您的工作,如果你希望通过给命令退出X之后的:它的前提示您,或者您也可以重新进入VI通过给前一个VI命令。

有一些东西,你可以在做更容易EX比六。面向生产线的材料的系统更改特别容易。编辑者可以读取高级编辑文档编找出了很多有关这种风格的编辑。有经验的用户经常将ex命令模式和vi命令模式混合使用,以加快工作速度。

Line Representation In The Display

The editor folds long logical lines onto many physical lines in the display. Commands which advance lines advance logical lines and will skip over all the segments of a line in one motion. The command|moves the cursor to a specific column, and may be useful for getting near the middle of a long line to split it in half.

The editor only puts full lines on the display; if there is not enough room on the display to fit a logical line, the editor leaves the physical line empty, placing only an@on the line as a place holder. When you delete lines on a dumb terminal, the editor will often just clear the lines to@to save time (rather than rewriting the rest of the screen). You can always maximize the information on the screen by giving the^Rcommand.

If you wish, you can have the editor place line numbers before each line on the display. Give the command:se nuto enable this, and the command:se nonuto turn it off. You can have tabs represented as^Iand the ends of lines indicated with "$" by giving the command:se list;:se nolistturns this off.

Finally, lines consisting of only the character "~" are displayed when the last line in the file is in the middle of the screen. These represent physical lines that are past the logical end of file.

More File Manipulation Commands

The following table lists the file manipulation commands which you can use when you are invi.

:w write back changes
:wq write and quit
:x write (if necessary) and quit (same asZZ).
:ename edit filename
:e! reedit, discarding changes
:e +name edit, starting at end
:e +n edit, starting at linen
:e # edit alternate file
:wname write filename
:w!name overwrite filename
:x,ywname write linesxthroughytoname
:rname read filenameinto buffer
:r !cmd read output ofcmdinto buffer
:n edit next file in argument list
:n! edit next file, discarding changes to current
:nargs specify new argument list
:tatag edit file containing tagtag, attag

All of these commands are followed by a CR or Esc. The most basic commands are:wand:e. A normal editing session on a single file will end with aZZcommand. If you are editing for a long period of time you can give:wcommands occasionally after major amounts of editing, and then finish with aZZ. When you edit more than one file, you can finish with one with a:wand start editing a new file by giving a:ecommand, or setautowriteand use:nfile.

If you make changes to the editor's copy of a file, but do not wish to write them back, then you must give an!after the command you would otherwise use; this forces the editor to discard any changes you have made. Use this carefully.

The:ecommand can be given a+argument to start at the end of the file, or a+nargument to start at linen. In actuality,nmay be any editor command not containing a space, usefully a scan like+/pator+?pat. In forming new names to theecommand, you can use the character%which is replaced by the current file name, or the character#which is replaced by the alternate filename. The alternate filenameis generally the last name you typed other than the current file. Thus if you try to do a:eand get a diagnostic that you haven't written the file, you can give a:wcommand and then a:e #command to redo the previous:e.

You can write part of the buffer to a file by finding out the lines that bound the range to be written using^G, and giving these numbers after the:and before thew, separated by,'s. You can also mark these lines withmand then use an address of the form'x,'yon thewcommand here.

You can read another file into the buffer after the current line by using the:rcommand. You can similarly read in the output from a command, just use!cmdinstead of a file name.

If you wish to edit a set of files in succession, you can give all the names on the command line, and then edit each one in turn using the command:n. It is also possible to respecify the list of files to be edited by giving the:ncommand a list of file names, or a pattern to be expanded as you would have given it on the initialvicommand.

If you are editing large programs, you will find the:tacommand very useful. It utilizes a data base of function names and their locations, which can be created by programs such asctags, to quickly find a function whose name you give. If the:tacommand will require the editor to switch files, then you must:wor abandon any changes before switching. You can repeat the:tacommand without any arguments to look for the same tag again.

More About Searching For Strings

When you are searching for strings in the file with/and?, the editor normally places you at the next or previous occurrence of the string. If you are using an operator such asd,cory, then you may well wish to affect lines up to the line before the line containing the pattern. You can give a search of the form/pat/-nto refer to then'th line before the next line containingpat, or you can use+instead of-to refer to the lines after the one containingpat. If you don't give a line offset, then the editor will affect characters up to the match place, rather than whole lines; thus use "+0" to affect to the line which matches.

You can have the editor ignore the case of words in the searches it does by giving the command:se ic. The command:se noicturns this off.

Strings given to searches may actually beregular expressions. If you do not want or need this facility, you should

set nomagic

in yourEXINIT. In this case, only the characters^and$are special in patterns. The character\is also then special (as it is most everywhere in the system), and may be used to get at the an extended pattern matching facility. It is also necessary to use a\before a/in a forward scan or a?in a backward scan, in any case. The following table gives the extended forms when magic is set.

^ at beginning of pattern, matches beginning of line
$ at end of pattern, matches end of line
. matches any character
\< matches the beginning of a word
\> matches the end of a word
[str] matches any single character instr
[str] matches any single character not instr
[x-y] matches any character betweenxandy
* matches any number of the preceding pattern

If you usenomagicmode, then the. [and*primitives are given with a preceding\.

More About Input Mode

There are a number of characters which you can use to make corrections during input mode. These are summarized in the following table.

^H deletes the last input character
^W deletes the last input word, defined as byb
erase your erase character, same as^H
kill your kill character, deletes the input on this line
\ escapes a following^Hand your erase and kill
Esc ends an insertion
Del interrupts an insertion, terminating it abnormally
CR starts a new line
^D backtabs over autoindent
0^D kills all the autoindent
^^D (caret-control-D) same as0^D, but restores indent next line
^V quotes the next non-printing character into the file

The most usual way of making corrections to input is by typing^Hto correct a single character, or by typing one or more^W's to back over incorrect words. If you use#as your erase character in the normal system, it will work like^H.

Your system kill character, normally@,^Xor^U, will erase all the input you have given on the current line. In general, you can neither erase input back around a line boundary nor can you erase characters which you did not insert with this insertion command. To make corrections on the previous line after a new line has been started you can hit Esc to end the insertion, move over and make the correction, and then return to where you were to continue. The commandAwhich appends at the end of the current line is often useful for continuing.

If you wish to type in your erase or kill character (say#or@) then you must precede it with a\, just as you would do at the normal system command level. A more general way of typing non-printing characters into the file is to precede them with a^V. The^Vechoes as a^character on which the cursor rests. This indicates that the editor expects you to type a control character. In fact you may type any character and it will be inserted into the file at that point.

If you are usingautoindentyou can backtab over the indent which it supplies by typing a^D. This backs up to ashiftwidthboundary. This only works immediately after the suppliedautoindent.

When you are usingautoindentyou may wish to place a label at the left margin of a line. The way to do this easily is to type^and then^D. The editor moves the cursor to the left margin for one line, and restore the previous indent on the next. You can also type a0followed immediately by a^Dif you wish to kill all the indent and not have it come back on the next line.

Uppercase-Only Terminals

If your terminal has only upper case, you can still useviby using the normal system convention for typing on such a terminal. Characters which you normally type are converted to lower case, and you can type upper case letters by preceding them with a\. The characters{~}|`are not available on such terminals, but you can escape them as\(\^\)\!\'. These characters are represented on the display in the same way they are typed.

Vi and Ex

viis actually one mode of editing within the editorex. When you are runningviyou can escape to the line oriented editor ofexby giving the commandQ. All of the:commands which were introduced above are available inex. Likewise, mostexcommands can be invoked fromviusing:. Just give them without the:and follow them with a CR.

In rare instances, an internal error may occur invi. In this case you will get a diagnostic and be left in the command mode ofex. You can then save your work and quit if you wish by giving a commandxafter the:whichexprompts you with, or you can reenterviby givingexavicommand.

There are a number of things which you can do more easily inexthan invi. Systematic changes in line-oriented material are particularly easy. You can read the advanced editing documents for the editoredto find out a lot more about this style of editing. Experienced users often mix their use ofexcommand mode andvicommand mode to speed the work they are doing.

查看英文版

查看中文版

快速参考

这是所有最有用的vi命令的摘要。任何时候需要快速记住vi的任何功能时,都可以参考它。

进入/离开编辑器

在命令行中:

viname
启动vi并加载文件名
vi+nname 启动vi并加载文件名,将光标放在第n行
vi-ttag 启动vi,然后从标签开始编辑
vi-r 列出可以恢复的文件
vi-rname 启动vi,并恢复以前编辑的文件名
viname1name2... 启动vi,加载文件name1进行编辑;使用:n命令加载任何其他命名的文件
vi-Rname 启动vi,并以只读模式加载文件名

在vi内部:

ZZ 从vi退出,保存更改
^Z 后台vi并返回到命令外壳。使用fgshell内置命令恢复前台vi操作
文件操作
:w 写入更改(保存文件)
:wq 写更改(保存)并退出
:q 如果没有未保存的更改,请立即退出
:q! 立即退出,丢弃所有未保存的更改
:ename 编辑文件名
:e! 恢复为当前文件的最后保存版本
:e+name 加载要编辑的文件名,将光标置于文件末尾
:e+nname 从第n行开始加载要编辑的文件名
:e# 编辑备用文件
^^ (控制符)。:e#的同义词
:wname 写入文件名(另存为...)
:w!name 覆盖文件名
:sh 放入命令行外壳;^ D返回
:!cmd 运行shell命令cmd,然后返回至vi
:n 编辑指定为vi参数的下一个文件
:nargs 指定文件的新参数列表VI来编辑
:f 显示正在编辑的当前文件和当前行数
^G :f的同义词
:tatag 标记文件条目标记
^] 与:ta相同,以下单词是标签
在文件内定位
^F 向前一屏
^B 向后一屏
^D 向下滚动半屏
^U 向上滚动半屏
numG 转到线NUM(如果最后一行NUM不指定)
/pat 搜索模式拍的下一次出现
?pat 搜索先前出现的图案拍
n 沿最后搜索方向重复最后搜索
N 重复最后搜索,与最后搜索方向相反
/pat/+n 在下一次出现模式pat之后搜索到第n行
?pat?-n 在上一次出现模式pat之前搜索到第n行
]] 移至下一部分/功能
[[ 移至上一部分/功能
% 查找匹配的(,),{或}
插入和替换

的Esc键键离开每个下面的文本输入模式,除了[R,这是仅用于输入单个字符。

i 插入方式:在光标之前插入文本
a 追加模式:将文本追加到光标之后
A 文本被添加到该行的末尾
I 文本插入到行的开头
o 在当前行之后打开新行以输入文本
O 在当前行之前打开新行以输入文本
rx 用字符x替换光标下的字符
R 替换多个字符
线定位
H 将光标移动到窗口的第一行(“主页”)
L 将光标移到窗口的最后一行
M 将光标移到窗口的中线
+ 将光标移动到下一行的第一个非空白字符
- 将光标移动到上一行的第一个非空白字符
CR (回车/ Enter键。)与+相同
downorj 下一行,同一列
upork 前一行,同一列
角色定位
^ 将光标移到行上的第一个非空白字符
0 将光标移到行首
$ 将光标移到行尾
rightorlorSPACE 将光标向前移动一个字符
leftorhor^H 将光标移回一个字符
fx 查找该行中字符x的下一个出现
Fx 查找该行中字符x的上一次出现
tx 向上移动到X向前
TX 向后移回x
; 重复最后一个f,F,t或T
, 逆;
num| 移至第num列
经营者

(这些命令中的每个命令都可以使用两次来影响整行):

d 删除
C 更改(例如删除,但将您置于插入模式下以输入应更改为的文本)
< 左移线shiftwidth空格数
> 右移线shiftwidth空格数
!(movement-command)shell-command 通过shell命令过滤文本
= 缩进LISP
y 将线拉入缓冲区
单词,句子,段落
w 向前的话
b 向后单词
e 字尾
) 下一个句子休息
} 到下一段
( 到上一个句子的中断
{ 至上一段
W 下一个以空格分隔的单词
B 到前一个以空格分隔的单词
E 到以空格分隔的单词的末尾
拉动到缓冲区,并从缓冲区粘贴
p 光标后粘贴
P 在光标之前粘贴
“Xp 从缓冲区x粘贴
“Xÿ 拉入缓冲区x
“Xd 删除到缓冲区x
撤消,重做,检索
u 撤消上一次更改
u 恢复当前行
. 重复上一次更改
“dp 找回Ð日最后一个删除
标记和退货
`` 转到先前的位置
'' 转到先前的位置,在行中的第一个非空白处
mx 用字母x标记位置
`x 移到标记为x的位置
'x 移动到行中第一个非空白字符x的位置
杂项操作
C 换行
D 删除其余行
s 替代字符
S 替代线
J 连接线
x 删除单个字符
X 删除光标前的单个字符
y 拉线和yy一样
调整屏幕
^L 清除并重画
z(Enter) 重画屏幕,当前行在顶部
z- 重画屏幕,当前行在底部
z. 以当前行为中心重绘屏幕
zz 与z相同。
^E 向下滚动窗口一行(但不要移动光标)
^Y 向上滚动窗口一行(但不要移动光标)
有用的命令组合
dw 删除一个词
de 删除一个单词,保留其所有标点符号不变
dd 删除一行
3dd 删除3行(例如)
cwnew 改成新词
eas 将单词复数(在其末尾加s)
xp 转置两个字符
例子
vi myfile.txt

编辑文件myfile.txt。

This is a summary of all the most usefulvicommands. Refer to it any time you need to remember any ofvi's functions quickly, and at a glance.

Entering/Leaving the Editor

From the command line:

viname launchvi, and load the filename
vi+nname launchvi, and load filename, placing the cursor at linen
vi-ttag launchvi, and start editing attag
vi-r list files that can be recovered
vi-rname launchvi, and recover previously-edited filename
viname1name2... launchvi, loading filename1for editing; any other files named are loaded with the:ncommand
vi-Rname launchvi, and load filenameinread-onlymode

From withinvi:

ZZ exit fromvi, saving changes
^Z backgroundviand return to the command shell. Resume foregroundvioperation with thefgshell builtin command
File Manipulation
:w write changes (save the file)
:wq write changes (save) and quit
:q quit immediately, if there are no unsaved changes
:q! quit immediately, discarding any unsaved changes
:ename edit filename
:e! revert to the last-saved version of the current file
:e+name load filenamefor editing, placing the cursor at the end of the file
:e+nname load filenamefor editing, starting at linen
:e# edit alternate file
^^ (control-caret). synonym for:e #
:wname write to filename(save as...)
:w!name overwrite filename
:sh drop to command line shell;^Dto return
:!cmd run shell commandcmd, then return tovi
:n edit next file that was specified as an argument tovi
:nargs specify new argument list of files forvito edit
:f show current file being edited, and current line count
^G synonym for:f
:tatag to tag file entrytag
^] same as:ta, and the following word is thetag
Positioning Within File
^F position forward one screenful
^B position backward one screenful
^D scroll down one half-screenful
^U scroll up one half-screenful
numG goto linenum(the last line ifnumnot specified)
/pat search for next occurrence of patternpat
?pat search for previous occurrence of patternpat
n repeat last search, in last search direction
N repeat last search, in the opposite of last search direction
/pat/+n search to thenth line after next occurrence of patternpat
?pat?-n search to thenth line before the previous occurrence of patternpat
]] move to next section/function
[[ move to previous section/function
% find matching(,),{, or}
Insert and Replace

TheEsckey exits each of the following text-entry modes, except forr, which is only used to enter a single character.

i insert mode: text is inserted before the cursor
a append mode: text is appended after the cursor
A text is appended at the end of the line
I text is inserted at the beginning of the line
o open a new line, after the current line, for text entry
O open a new line, before the current line, for text entry
rx replace a the character under the cursor with characterx
R replace multiple characters
Line Positioning
H move cursor to the first ("home") line of the window
L move cursor to the last line of the window
M move cursor to the middle line of the window
+ move cursor to the first non-whitespace character of the next line
- move cursor to the first non-whitespace character of the previous line
CR (carriage return/the Enter key.) same as+
downorj next line, same column
upork previous line, same column
Character Positioning
^ move cursor to first non-whitespace character on the line
0 move cursor to beginning of line
$ move cursor to end of line
rightorlorSPACE move cursor forward one character
leftorhor^H move cursor back one character
fx find the next occurrence of characterxin the line
Fx find the previous occurrence of characterxin the line
tx move up toxforward
Tx move back toxbackward
; repeat lastf,F,t, orT
, inverse of;
num| Move to columnnum
Operators

(Each of these commands can be used twice to affect entire lines):

d delete
c change (like delete, but places you in insert mode to enter the text it should change to)
< shift line leftshiftwidthnumber of spaces
> shift line rightshiftwidthnumber of spaces
!(movement-command)shell-command filter text through a shell command
= indent for LISP
y yank lines into buffer
Words, Sentences, Paragraphs
w word forward
b word backward
e end of word
) to next sentence break
} to next paragraph break
( to previous sentence break
{ to previous paragraph break
W to next blank-delimited word
B to previous blank-delimited word
E to end of blank-delimited word
Yank to Buffer, and Paste from Buffer
p paste after cursor
P paste before cursor
"xp paste from bufferx
"xy yank into bufferx
"xd delete into bufferx
Undo, Redo, Retrieve
u undo last change
U restore current line
. repeat last change
"dp retrievedth last delete
Marking and Returning
`` go to previous location
'' go to previous location, at first non-whitespace in line
mx Mark position with letterx
`x move to the position markedx
'x move to the position markedx, at the first non-whitespace character in the line
Miscellaneous Operations
C change rest of line
D delete rest of line
s substitute characters
S substitute lines
J join lines
x delete a single character
X delete a single characterbeforethe cursor
Y yank a line; same asyy
Adjusting the Screen
^L clear and redraw
z(Enter) redraw screen with current line at top
z- redraw screen with current line at bottom
z. redraw screen with current line at center
zz same asz.
^E scroll window down one line (but don't move cursor)
^Y scroll window up one line (but don't move cursor)
Useful Command Combinations
dw delete a word
de delete a word, leaving any of its punctuation intact
dd delete a line
3dd delete 3 lines (for example)
cwnew change word tonew
eas pluralize a word (stick an s at the end of it)
xp transpose two characters
Examples
vi myfile.txt

Edits the filemyfile.txt.

查看英文版

查看中文版