📅  最后修改于: 2020-10-31 14:50:20             🧑  作者: Mango
在本章中,我们将了解vi编辑器在Unix中的工作方式。在Unix中有许多编辑文件的方法。使用面向屏幕的文本编辑器vi编辑文件是最好的方法之一。使用此编辑器,您可以在上下文中与文件中的其他行一起编辑行。
现在还提供了称为VIM的vi编辑器的改进版本。在这里,VIM代表Vi IM经过证明。
vi通常被认为是Unix编辑器中的事实上的标准,因为-
通常可以在所有Unix系统上使用。
它的实现全都非常相似。
它需要很少的资源。
它比ed或ex等其他编辑器更加用户友好。
您可以使用vi编辑器编辑现有文件或从头开始创建新文件。您也可以使用此编辑器来读取文本文件。
下表列出了使用vi编辑器的基本命令-
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的示例(如果当前工作目录中尚不存在该文件)-
$vi testfile
上面的命令将生成以下输出-
|
~
~
~
~
~
~
~
~
~
~
~
~
"testfile" [New File]
您会在光标之后的每一行上看到一个波浪号(〜)。代字号代表未使用的行。如果某行不是以波浪号开头并且看起来为空白,则表示存在空格,制表符,换行符或某些其他不可见的字符。
现在,您有一个打开的文件开始工作。在继续进行之前,让我们理解一些重要的概念。
使用vi编辑器时,我们通常会遇到以下两种模式-
命令模式-此模式使您可以执行管理任务,例如保存文件,执行命令,移动光标,剪切(拖动)和粘贴行或单词以及查找和替换。在这种模式下,您键入的任何内容都将被解释为命令。
插入模式-使用此模式可以将文本插入文件。在此模式下键入的所有内容都将解释为输入并放置在文件中。
vi始终以命令模式启动。要输入文本,您必须处于插入模式,只需键入i即可。要退出插入模式,请按Esc键,这将使您返回命令模式。
提示-如果不确定所处的模式,请按两次Esc键;这将带您进入命令模式。使用vi编辑器打开文件。首先输入一些字符,然后进入命令模式以了解不同之处。
退出vi的命令是:q 。进入命令模式后,键入冒号和’q’,然后返回。如果您的文件有任何修改,编辑器将警告您,并且不允许您退出。要忽略此消息,不保存而退出vi的命令是:q!。 。这使您可以退出vi而无需保存任何更改。
保存编辑器内容的命令是:w 。您可以将以上命令与quit命令结合使用,或使用:wq并返回。
保存更改并退出vi的最简单方法是使用ZZ命令。在命令模式下,输入ZZ 。 ZZ命令的工作方式与:wq命令相同。
如果要指定/声明文件的任何特定名称,可以在:w之后指定它。例如,如果您想将正在处理的文件另存为另一个名为filename2的文件名,则可以输入:w filename2并返回。
要在文件内四处移动而又不影响文本,您必须处于命令模式(两次按Esc键)。下表列出了一些可用于一次移动一个字符的命令-
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 |
在文件内移动时需要考虑以下几点-
vi区分大小写。使用命令时需要注意大写。
vi中的大多数命令都可以以您希望操作发生的次数为开头。例如, 2j将光标向下移动光标两行。
在vi中的文件中还有许多其他移动方式。请记住,您必须处于命令模式(按两次Esc )。下表列出了一些在文件中移动的命令-
Sr.No. | Command & Description |
---|---|
1 |
0 or | Positions the cursor at the beginning of a line |
2 |
$ Positions the cursor at the end of a line |
3 |
w Positions the cursor to the next word |
4 |
b Positions the cursor to the previous word |
5 |
( Positions the cursor to the beginning of the current sentence |
6 |
) Positions the cursor to the beginning of the next sentence |
7 |
E Moves to the end of the blank delimited word |
8 |
{ Moves a paragraph back |
9 |
} Moves a paragraph forward |
10 |
[[ Moves a section back |
11 |
]] Moves a section forward |
12 |
n| Moves to the column n in the current line |
13 |
1G Moves to the first line of the file |
14 |
G Moves to the last line of the file |
15 |
nG Moves to the nth line of the file |
16 |
:n Moves to the nth line of the file |
17 |
fc Moves forward to c |
18 |
Fc Moves back to c |
19 |
H Moves to the top of the screen |
20 |
nH Moves to the nth line from the top of the screen |
21 |
M Moves to the middle of the screen |
22 |
L Move to the bottom of the screen |
23 |
nL Moves to the nth line from the bottom of the screen |
24 |
😡 Colon followed by a number would position the cursor on the line number represented by x |
以下命令可与Control Key一起使用以执行下表中给出的功能-
Sr.No. | Command & Description |
---|---|
1 |
CTRL+d Moves forward 1/2 screen |
2 |
CTRL+f Moves forward one full screen |
3 |
CTRL+u Moves backward 1/2 screen |
4 |
CTRL+b Moves backward one full screen |
5 |
CTRL+e Moves the screen up one line |
6 |
CTRL+y Moves the screen down one line |
7 |
CTRL+u Moves the screen up 1/2 page |
8 |
CTRL+d Moves the screen down 1/2 page |
9 |
CTRL+b Moves the screen up one page |
10 |
CTRL+f Moves the screen down one page |
11 |
CTRL+I Redraws the screen |
要编辑文件,您需要处于插入模式。有多种方法可以从命令模式进入插入模式-
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 |
这是重要命令列表,可用于删除打开文件中的字符和行-
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删除光标所在的两行。
建议先练习命令,然后再继续。
您还可以更改vi中的字符,单词或行而不删除它们。这是相关的命令-
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. |
您可以从一个位置复制行或单词,然后可以使用以下命令将它们粘贴到另一位置:
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. |
有一些高级命令可以简化日常编辑,并可以更有效地使用vi-
Sr.No. | Command & Description |
---|---|
1 |
J Joins the current line with the next one. A count of j commands join many lines. |
2 |
<< Shifts the current line to the left by one shift width. |
3 |
>> Shifts the current line to the right by one shift width. |
4 |
~ Switches the case of the character under the cursor. |
5 |
^G Press Ctrl and G keys at the same time to show the current filename and the status. |
6 |
U Restores the current line to the state it was in before the cursor entered the line. |
7 |
u This helps undo the last change that was done in the file. Typing ‘u’ again will re-do the change. |
8 |
J Joins the current line with the next one. A count joins that many lines. |
9 |
:f Displays the current position in the file in % and the file name, the total number of file. |
10 |
:f filename Renames the current file to filename. |
11 |
:w filename Writes to file filename. |
12 |
:e filename Opens another file with filename. |
13 |
:cd dirname Changes the current working directory to dirname. |
14 |
:e # Toggles between two open files. |
15 |
:n In case you open multiple files using vi, use :n to go to the next file in the series. |
16 |
:p In case you open multiple files using vi, use :p to go to the previous file in the series. |
17 |
:N In case you open multiple files using vi, use :N to go to the previous file in the series. |
18 |
:r file Reads file and inserts it after the current line. |
19 |
:nr file Reads file and inserts it after the line n. |
vi编辑器有两种搜索: 字符串和字符 。对于字符串搜索, /和?使用命令。当您启动这些命令时,刚键入的命令将显示在屏幕的最后一行,您在其中键入要查找的特定字符串。
这两个命令仅在搜索发生的方向上有所不同-
/命令在文件中向前(向下)搜索。
?命令在文件中向后(向上)搜索。
n和N命令分别在相同或相反的方向上重复前一个搜索命令。有些字符有特殊含义。这些字符前面必须带有反斜杠( \ ),才能作为搜索表达式的一部分包含在内。
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向后搜索,光标移至找到的字符的位置。
t和T命令仅在当前行上搜索字符,但对于t ,光标移至该字符之前的位置,而T向后搜索该行至该字符之后的位置。
您可以使用以下:set命令更改vi屏幕的外观。进入命令模式后,键入:set,然后键入以下任何命令。
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 |
vi可以从编辑器中运行命令。要运行命令,只需进入命令模式并输入:!。命令。
例如,如果要在尝试使用该文件名保存文件之前检查文件是否存在,可以键入:!。 ls ,您将在屏幕上看到ls的输出。
您可以按任意键(或命令的转义序列)以返回到vi会话。
替换命令( :s / )使您可以快速替换文件中的单词或单词组。以下是替换文本的语法-
:s/search/replace/g
g代表全局。此命令的结果是更改了光标行上的所有匹配项。
以下几点将帮助您成功使用vi-
您必须处于命令模式才能使用命令。 (可以随时按两次Esc以确保您处于命令模式。)
您必须小心使用这些命令。这些区分大小写。
您必须处于插入模式才能输入文本。