Unix 简明教程
Unix/Linux - The vi Editor Tutorial
在本章中,我们将了解 vi 编辑器在 Unix 中如何工作。在 Unix 中有许多方法可以编辑文件。使用面向屏幕的文本编辑器 vi 编辑文件是最好的方法之一。此编辑器使您能够在与文件中其他行同上下文中编辑行。
In this chapter, we will understand how the vi Editor works in Unix. There are many ways to edit files in Unix. Editing files using the screen-oriented text editor vi is one of the best ways. This editor enables you to edit lines in context with other lines in the file.
现在还推出了一个名为 VIM 的 vi 编辑器的改进版本。在此,VIM 代表 *Vi IM*proved。
An improved version of the vi editor which is called the VIM has also been made available now. Here, VIM stands for *Vi IM*proved.
vi 通常被认为是 Unix 编辑器的实际标准,因为 −
vi is generally considered the de facto standard in Unix editors because −
-
It’s usually available on all the flavors of Unix system.
-
Its implementations are very similar across the board.
-
It requires very few resources.
-
It is more user-friendly than other editors such as the ed or the ex.
您可以使用 vi 编辑器编辑现有文件或从头开始创建新文件。您还可以使用此编辑器来仅读取文本文件。
You can use the vi editor to edit an existing file or to create a new file from scratch. You can also use this editor to just read a text file.
Starting the vi Editor
下表列出了 vi 编辑器的基本命令 −
The following table lists out the basic commands to use the vi editor −
Sr.No. |
Command & Description |
1 |
vi filename Creates a new file if it already does not exist, otherwise opens an existing file. |
2 |
vi -R filename Opens an existing file in the read-only mode. |
3 |
view filename Opens an existing file in the read-only mode. |
以下是创建新文件 testfile 的示例(如果当前工作目录中尚不存在) −
Following is an example to create a new file testfile if it already does not exist in the current working directory −
$vi testfile
上述命令将生成以下输出 −
The above command will generate the following output −
|
~
~
~
~
~
~
~
~
~
~
~
~
"testfile" [New File]
您会注意到光标后每一行上都有一个 tilde (~)。波浪线表示未使用的行。如果一行不以波浪线开头并且看起来是空白的,则那里有一个空格、制表符、换行符或其他不可查看的字符。
You will notice a tilde (~) on each line following the cursor. A tilde represents an unused line. If a line does not begin with a tilde and appears to be blank, there is a space, tab, newline, or some other non-viewable character present.
现在您有一个可供开始处理的打开文件。在进一步处理之前,让我们了解一些重要概念。
You now have one open file to start working on. Before proceeding further, let us understand a few important concepts.
Operation Modes
使用 vi 编辑器时,我们通常会遇到以下两种模式 −
While working with the vi editor, we usually come across the following two modes −
-
Command mode − This mode enables you to perform administrative tasks such as saving the files, executing the commands, moving the cursor, cutting (yanking) and pasting the lines or words, as well as finding and replacing. In this mode, whatever you type is interpreted as a command.
-
Insert mode − This mode enables you to insert text into the file. Everything that’s typed in this mode is interpreted as input and placed in the file.
vi 始终从 command mode 开始。要输入文本,您必须处于仅需键入 i 即可进入的插入模式。要退出插入模式,请按 Esc 键,该键将使您返回命令模式。
vi always starts in the command mode. To enter text, you must be in the insert mode for which simply type i. To come out of the insert mode, press the Esc key, which will take you back to the command mode.
Hint - 如果您不确定自己处于哪种模式,请按两次 Esc 键;这将使您进入命令模式。您使用 vi 编辑器打开一个文件。首先键入一些字符,然后再进入命令模式以了解其差别。
Hint − If you are not sure which mode you are in, press the Esc key twice; this will take you to the command mode. You open a file using the vi editor. Start by typing some characters and then come to the command mode to understand the difference.
Getting Out of vi
退出 vi 的命令为 :q 。进入命令模式后,键入冒号、“q”,然后按回车。如果文件以任何方式被修改,编辑器将警告您,并且不允许您退出。要忽略此消息,退出 vi 而不用保存的命令为 :q! 。这使您可以退出 vi,而不保存任何更改。
The command to quit out of vi is :q. Once in the command mode, type colon, and 'q', followed by return. If your file has been modified in any way, the editor will warn you of this, and not let you quit. To ignore this message, the command to quit out of vi without saving is :q!. This lets you exit vi without saving any of the changes.
保存编辑器内容的命令为 :w 。您可以将以上命令与退出命令结合使用,或者使用 :wq 并回车。
The command to save the contents of the editor is :w. You can combine the above command with the quit command, or use :wq and return.
最简单的方法是使用 ZZ 命令 save your changes and exit vi 。当您处于命令模式时,键入 ZZ 。 ZZ 命令的操作方式与 :wq 命令相同。
The easiest way to save your changes and exit vi is with the ZZ command. When you are in the command mode, type ZZ. The ZZ command works the same way as the :wq command.
如果想要指定/说明文件的任何特定名称,可以通过 :w 后指定该名称。例如,如果您想要将您正在处理的文件保存为以 filename2 为名的另一个文件名,您将键入 :w filename2 并回车。
If you want to specify/state any particular name for the file, you can do so by specifying it after the :w. For example, if you wanted to save the file you were working on as another filename called filename2, you would type :w filename2 and return.
Moving within a File
要在不影响文本的情况下在一个文件中四处移动,您必须处于命令模式(按两次 Esc)。下表列出了一些可供您逐个字符移动的命令 −
To move around within a file without affecting your text, you must be in the command mode (press Esc twice). The following table lists out a few commands you can use to move around one character at a time −
Sr.No. |
Command & Description |
1 |
k Moves the cursor up one line |
2 |
j Moves the cursor down one line |
3 |
h Moves the cursor to the left one character position |
4 |
l Moves the cursor to the right one character position |
在文件中移动需要考虑以下几点 −
The following points need to be considered to move within a file −
-
vi is case-sensitive. You need to pay attention to capitalization when using the commands.
-
Most commands in vi can be prefaced by the number of times you want the action to occur. For example, 2j moves the cursor two lines down the cursor location.
vi 中还有许多其他方法可以在文件中移动。请记住,您必须处于命令模式 ( press Esc twice )。下表列出了一些在文件中四处移动的命令 −
There are many other ways to move within a file in vi. Remember that you must be in the command mode (press Esc twice). The following table lists out a few commands to move around the file −
Control Commands
在表中给出了使用控制键执行相应功能的命令:
The following commands can be used with the Control Key to performs functions as given in the table below −
Editing Files
要编辑该文件,您需要进入插入模式。有许多方法可从命令模式进入插入模式:
To edit the file, you need to be in the insert mode. There are many ways to enter the insert mode from the command mode −
Sr.No. |
Command & Description |
1 |
i Inserts text before the current cursor location |
2 |
I Inserts text at the beginning of the current line |
3 |
a Inserts text after the current cursor location |
4 |
A Inserts text at the end of the current line |
5 |
o Creates a new line for text entry below the cursor location |
6 |
O Creates a new line for text entry above the cursor location |
Deleting Characters
这里列出了用于删除打开文件中的字符和行的重要命令:
Here is a list of important commands, which can be used to delete characters and lines in an open file −
Sr.No. |
Command & Description |
1 |
x Deletes the character under the cursor location |
2 |
X Deletes the character before the cursor location |
3 |
dw Deletes from the current cursor location to the next word |
4 |
d^ Deletes from the current cursor position to the beginning of the line |
5 |
d$ Deletes from the current cursor position to the end of the line |
6 |
D Deletes from the cursor position to the end of the current line |
7 |
dd Deletes the line the cursor is on |
如上所述,vi 中的大多数命令均可以有该操作发生的次数作为前置条件。例如, 2x 删除光标位置下的两个字符, 2dd 删除光标所在的两个行。
As mentioned above, most commands in vi can be prefaced by the number of times you want the action to occur. For example, 2x deletes two characters under the cursor location and 2dd deletes two lines the cursor is on.
建议在继续之前练习这些命令。
It is recommended that the commands are practiced before we proceed further.
Change Commands
您还可以更改字符、单词或 vi 中的行,而不删除它们。相关命令如下:
You also have the capability to change characters, words, or lines in vi without deleting them. Here are the relevant commands −
Sr.No. |
Command & Description |
1 |
cc Removes the contents of the line, leaving you in insert mode. |
2 |
cw Changes the word the cursor is on from the cursor to the lowercase w end of the word. |
3 |
r Replaces the character under the cursor. vi returns to the command mode after the replacement is entered. |
4 |
R Overwrites multiple characters beginning with the character currently under the cursor. You must use Esc to stop the overwriting. |
5 |
s Replaces the current character with the character you type. Afterward, you are left in the insert mode. |
6 |
S Deletes the line the cursor is on and replaces it with the new text. After the new text is entered, vi remains in the insert mode. |
Copy and Paste Commands
您可以从一个地方复制行或单词,然后使用以下命令将它们粘贴到另一个地方 −
You can copy lines or words from one place and then you can paste them at another place using the following commands −
Sr.No. |
Command & Description |
1 |
yy Copies the current line. |
2 |
yw Copies the current word from the character the lowercase w cursor is on, until the end of the word. |
3 |
p Puts the copied text after the cursor. |
4 |
P Puts the yanked text before the cursor. |
Advanced Commands
有一些高级命令可以简化日常编辑并更有效地使用 vi −
There are some advanced commands that simplify day-to-day editing and allow for more efficient use of vi −
Word and Character Searching
vi 编辑器有两种搜索: string 和 character 。对于字符串搜索,使用 / 和 ? 命令。当您启动这些命令时,刚刚键入的命令将显示在屏幕的最后一行,您可以在其中键入要查找的特定字符串。
The vi editor has two kinds of searches: string and character. For a string search, the / and ? commands are used. When you start these commands, the command just typed will be shown on the last line of the screen, where you type the particular string to look for.
这两个命令仅在搜索发生的方向上有所不同 −
These two commands differ only in the direction where the search takes place −
-
The / command searches forwards (downwards) in the file.
-
The ? command searches backwards (upwards) in the file.
n 和 N 命令分别在相同或相反的方向重复先前的搜索命令。一些字符具有特殊含义。这些字符之前必须加反斜杠 ( \ ) 才能作为搜索表达式的组成部分。
The n and N commands repeat the previous search command in the same or the opposite direction, respectively. Some characters have special meanings. These characters must be preceded by a backslash (\) to be included as part of the search expression.
Sr.No. |
Character &Description |
1 |
^ Searches at the beginning of the line (Use at the beginning of a search expression). |
2 |
. Matches a single character. |
3 |
* Matches zero or more of the previous character. |
4 |
$ End of the line (Use at the end of the search expression). |
5 |
[ Starts a set of matching or non-matching expressions. |
6 |
< This is put in an expression escaped with the backslash to find the ending or the beginning of a word. |
7 |
> This helps see the '<' character description above. |
字符搜索在单行内进行,用于找到命令后输入的字符。 f 和 F 命令仅在当前行中搜索字符。 f 向前搜索, F 向后搜索,光标移动到找到的字符位置。
The character search searches within one line to find a character entered after the command. The f and F commands search for a character on the current line only. f searches forwards and F searches backwards and the cursor moves to the position of the found character.
t 和 T 命令仅在当前行中搜索字符,但对于 t ,光标移动到字符前面的位置,而 T 向后搜索整行直到字符的后面位置。
The t and T commands search for a character on the current line only, but for t, the cursor moves to the position before the character, and T searches the line backwards to the position after the character.
Set Commands
您可以使用以下 :set 命令来更改 vi 屏幕的外观。在命令模式后,键入 :set ,再输入下述任意命令。
You can change the look and feel of your vi screen using the following :set commands. Once you are in the command mode, type :set followed by any of the following commands.
Sr.No. |
Command & Description |
1 |
:set ic Ignores the case when searching |
2 |
:set ai Sets autoindent |
3 |
:set noai Unsets autoindent |
4 |
:set nu Displays lines with line numbers on the left side |
5 |
:set sw Sets the width of a software tabstop. For example, you would set a shift width of 4 with this command — :set sw = 4 |
6 |
:set ws If wrapscan is set, and the word is not found at the bottom of the file, it will try searching for it at the beginning |
7 |
:set wm If this option has a value greater than zero, the editor will automatically "word wrap". For example, to set the wrap margin to two characters, you would type this: :set wm = 2 |
8 |
:set ro Changes file type to "read only" |
9 |
:set term Prints terminal type |
10 |
:set bf Discards control characters from input |
Running Commands
vi 有能力在编辑器内运行命令。要运行命令,您只需进入命令模式并输入 :! 命令。
The vi has the capability to run commands from within the editor. To run a command, you only need to go to the command mode and type :! command.
例如,如果您希望在尝试使用该文件名保存文件之前检查是否存在文件,您可以键入 :! ls ,您将在屏幕上看到 ls 的输出。
For example, if you want to check whether a file exists before you try to save your file with that filename, you can type :! ls and you will see the output of ls on the screen.
您可以按任意键(或命令的转义序列)返回到 vi 会话。
You can press any key (or the command’s escape sequence) to return to your vi session.
Replacing Text
替换命令 ( :s/ ) 使您可以快速替换文件中的单词或单词组。以下是替换文本的语法 -
The substitution command (:s/) enables you to quickly replace words or groups of words within your files. Following is the syntax to replace text −
:s/search/replace/g
g 表示全局。此命令的结果是光标行上的所有出现的单词均发生改变。
The g stands for globally. The result of this command is that all occurrences on the cursor’s line are changed.
Important Points to Note
下列要点将让您使用 vi 更加成功 -
The following points will add to your success with vi −
-
You must be in command mode to use the commands. (Press Esc twice at any time to ensure that you are in command mode.)
-
You must be careful with the commands. These are case-sensitive.
-
You must be in insert mode to enter text.