The Linux Command Line-WILLIAM(7)-文本编辑器

261 阅读4分钟

学习使用 vi

1.1 为什么使用vi

  • vi总是可用,比如在没有图形界面的云服务器

  • vi处理快速且是轻量级

1.2 vi常用操作

可以使用vi命令编辑一个不存在的文件名来创建一个文件:

[root@izuf67yuy6secatlp4sztez exercise]# vi foo.txt

进入编辑界面~符号代表此行没有字符。

编辑模式 插入模式 为了增加文本到文件,必须要进入插入模式(insert mode)。可以再进入编辑界面后按下 I 键。然后在编辑界面下方显示:

-- INSERT --

现在可以增加一些字符。按下 ESC 按键来退出插入模式

要保存对文件所做的更改,必须在命令模式下输入ex命令(ex command )。

:w

按下后,文本被保存在硬盘中,可以在屏幕下面看到相关信息。

"foo.txt" [New] 1L, 46C written

注:在vim文档中命令模式(command mode)称为普通模式( normal mode)并且 ex命令( commands)称为命令模式。

移动光标按键

Table 12-1: Cursor Movement Keys

Key Moves the cursor
L or right arrow Right one character
H or left arrow Left one character
J or down arrow Down one line
K or up arrow Up one line
0 (zero) To the beginning of the current line
SHIFT-6 (^) To the first non-whitespace character on the current line
SHIFT-4 ($) To the end of the current line
W To the beginning of the next word or punctuation character
SHIFT-W (W) To the beginning of the next word, ignoring punctu ation characters
B To the beginning of the previous word or punctuation character
SHIFT-B (B) To the beginning of the previous word, ignoring punctuation characters
CTRL-F or PAGE DOWN Down one page
CTRL-B or PAGE UP Up one page
number-SHIFT-G To line number (for example, 1G moves to the first line of the file)
SHIFT-G (G) To the last line of the file

命令5j使vi将光标向下移动五行。

2 基本的编辑

vi还提供了有限的撤销命令。 如果在命令模式下按U键,vi将撤消所做的最后更改。

2.1 操作文本

追加文本

The quick brown fox jumped over the lazy dog.

如果将光标移至行尾并键入a,光标将移至行尾,vi将进入插入模式。

The quick brown fox jumped over the lazy dog. It was cool.

新增一行

Table 12-2: Line Opening Keys

Command Opens
o The line below the current line
O The line above the current line

文本删除

Table 12-3: Text Deletion Commands

Command Deletes
x The current character
3x The current character and the next two characters
dd The current line
5dd The current line and the next four line
dW From the current cursor location to the beginning of the next word
d$ From the current cursor location to the end of the current line
d0 From the current cursor location to the beginning of the line
d^ From the current cursor location to the first non-whitespace character in the line
dG From the current line to the end of the file
d20G From the current line to the 20th line of the file

文本拷贝

Table12-4: Yanking Commands

Command Copies
yy The current line
5yy The current line and the next four lines
yW From the current cursor location to the beginning of the next word
y$ From the current cursor location to the end of the current line
y0 From the current cursor location to the beginning of the line
y^ From the current cursor location to the first non-whitespace character in the line
yG From the current line to the end of the file
y20G From the current line to the 20th line of the file

连接行 如果将光标放在第3行并输入J命令,结果如下所示:

The quick brown fox jumped over the lazy dog. It was cool.
Line 2
Line 3 Line 4
Line 5

查找与替换 文本中有文本:

The quick brown fox jumped over the lazy dog. It was cool.
Line 2
Line 3
Line 4
Line 5

在行内搜索 f命令搜索一行,并将光标移动到指定字符的下一个实例。 例如,键入命令fa会将光标移动到当前行中下一个出现的字符a。 在一行中执行字符搜索后,可以通过键入分号来重复搜索。

搜索整个文件 搜索字符下个出现的地方,可以使用/符号。less命令也可以使用这种方式来搜索。在/符号后添加要搜索的字符串按下回车来搜索字符。然后可以使用n建来重复搜索。搜索的字符可以使用正则表达式(regular expressions)。

全局搜索和替换 vi使用ex命令在一定范围的行或整个文件中执行搜索和替换操作(在vi中称为替换(substitution))。 要替换整个文件的Line 为line,我们将输入以下命令:

:%s/Line/line/g

输入命令后,文本变为:

The quick brown fox jumped over the lazy dog. It was cool.
line 2
line 3
line 4
line 5

命令含义如下表: Table12-5: An Example of Global Search-and-Replace Syntax

Item Meaning
: The colon character starts an ex command
% Specifies the range of lines for the operation. % is a shortcut meaning from the first line to the last line. Alternatively, the range could have been specified 1,5 (because our file is five lines long), or 1,$, which means “from line 1 to the last line in the file.” If the range of lines is omitted, the operation is performed only on the current line
s Specifies the operation—in this case, substitution (search and replace).
/Line/line/ The search pattern and the replacement text
g This means global, in the sense that the substitution is per-formed on every instance of the search string in each line. If g is omitted, only the first instance of the search string on each line is replaced.

还可以使用户来手动确认指定替换命令。

:%s/line/Line/gc

执行命令后,vi停止执行并要求我们通过以下消息确认替换:

replace with Line (y/n/a/q/l/^E/^Y)?

Table 12-6: Replace Confirmation Keys

Key Action
y Perform the substitution
n Skip this instance of the pattern
a Perform the substitution on this and all subsequent instances of the pattern
q or ESC Quit substituting
l Perform this substitution and then quit. Short for last
CTRL-E, CTRL-Y Scroll down and scroll up, respectively. Useful for viewing the context of the proposed substitution

2.2 编辑多个文件

可以使用vi来编辑多个文件,命令格式:

vi file1 file2 file3...

首先创建文件

[me@linuxbox ~]$ ls -l /usr/bin > ls-output.txt

接着输入命令编辑两个文件

[me@linuxbox ~]$ vi foo.txt ls-output.txt

切换文件 为了从一个文件切换到另一个文件,使用此 ex command:

:n

返回上一个文件,使用命令

:N

当从一个文件切换到另一个文件的时候,vi强制执行一项策略,如果当前文件中有未保存的更改,该策略将阻止切换文件。 为了强制切换文件并且作废更改,可以在命令后面增加感叹号。 除了上述切换方法外,vim(和vi的某些版本)还提供了一些ex命令,这些命令使多个文件的管理更加容易。 可以执行 :buffers 命令来查看文件列表。

:buffers
  1 %a   "foo.txt"                      line 1
  2      "ls-output.txt"                line 0
Press ENTER or type command to continue

如果要切换文件,在 buffer 后面加入想要切换的buffer数字。比如,要从包含文件foo.txt的 buffer 1切换到包含文件ls-output.txt的 buffer 2,输入命令:

:buffer 2

打开其他文件进行编辑 可以在当前编辑会话中增加文件。使用ex command

:e (short for edit) + filename

例如: 首先打开文件 foo.txt

[me@linuxbox ~]$ vi foo.txt

接着输入命令:

:e ls-output.txt

可以使用 :buffers 命令来确认:

:buffers
  1 #    "foo.txt"                      line 1
  2 %a   "ls-output.txt"                line 8
Press ENTER or type command to continue

注:不能使用 :n or :N进行文件切换。可以使用 :buffer + 数字 来切换文件。

3 练习

3.1 在两个文件间复制内容

首先 切换到 buffer 1

:buffer 1

文件内容应为:

The quick brown fox jumped over the lazy dog. It was cool.
Line 2
Line 3
Line 4
Line 5

然后移动光标到文件第一行,然后键入yy来拷贝一整行,切换到 buffer 2

:buffer 2

可以看到文本

total 85672
-rwxr-xr-x    1 root root     41488 Aug 20  2019 [
-rwxr-xr-x    1 root root    107904 Jan 22  2019 a2p
...

将光标移动到文件的第二行,然后按下 p 键来粘贴文本:

total 85672
The quick brown fox jumped over the lazy dog. It was cool.
-rwxr-xr-x    1 root root     41488 Aug 20  2019 [
-rwxr-xr-x    1 root root    107904 Jan 22  2019 a2p
...

可以看到复制的文本已经粘贴到了第二行

3.2 将整个文件内容插入到另一个文件

可以将整个文件内容插入到正在编辑的文件 首先编辑一个文件

[me@linuxbox ~]$ vi ls-output.txt

文件内容如下:

total 85672
-rwxr-xr-x    1 root root     41488 Aug 20  2019 [
-rwxr-xr-x    1 root root    107904 Jan 22  2019 a2p
-rwxr-xr-x    1 root root     29112 Mar 18 07:50 addr2line
-rwxr-xr-x    1 root root        29 Aug  8  2019 alias
...

移动光标到第一行,然后输入以下命令:

:r foo.txt

:r命令(short for read)将指定的文件插入光标位置之前。

total 85672
The quick brown fox jumped over the lazy dog. It was cool.
Line 2
Line 3
Line 4
Line 5




-rwxr-xr-x    1 root root     41488 Aug 20  2019 [
-rwxr-xr-x    1 root root    107904 Jan 22  2019 a2p
-rwxr-xr-x    1 root root     29112 Mar 18 07:50 addr2line

3.3 保存文件更改

  • :w命令

  • 命令模式下,键入zz可以保存并退出文件(:wq命令也可以做到)

  • :w + 文件名 类似于 Save As 命令。我们正在编辑foo.txt,并想保存一个名为foo1.txt的备用版本,可以使用命令

:w foo1.txt

注意:虽然这将文件保存在一个新名称下,但它不会更改正在编辑的文件的名称。继续编辑时,仍将编辑foo.txt而不是foo1.txt