Merge remote-tracking branch 'LCTT/master'

This commit is contained in:
Xingyu.Wang 2019-03-03 20:46:47 +08:00
commit f23352fc19
5 changed files with 324 additions and 338 deletions

View File

@ -1,5 +1,5 @@
[#]: collector: (lujun9972)
[#]: translator: ( )
[#]: translator: (MjSeven)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )

View File

@ -1,211 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: (HankChow)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (And, Ampersand, and & in Linux)
[#]: via: (https://www.linux.com/blog/learn/2019/2/and-ampersand-and-linux)
[#]: author: (Paul Brown https://www.linux.com/users/bro66)
And, Ampersand, and & in Linux
======
![](https://www.linux.com/sites/lcom/files/styles/rendered_file/public/ampersand.png?itok=7GdFO36Y)
Take a look at the tools covered in the [three][1] [previous][2] [articles][3], and you will see that understanding the glue that joins them together is as important as recognizing the tools themselves. Indeed, tools tend to be simple, and understanding what _mkdir_ , _touch_ , and _find_ do (make a new directory, update a file, and find a file in the directory tree, respectively) in isolation is easy.
But understanding what
```
mkdir test_dir 2>/dev/null || touch images.txt && find . -iname "*jpg" > backup/dir/images.txt &
```
does, and why we would write a command line like that is a whole different story.
It pays to look more closely at the sign and symbols that live between the commands. It will not only help you better understand how things work, but will also make you more proficient in chaining commands together to create compound instructions that will help you work more efficiently.
In this article and the next, we'll be looking at the the ampersand (`&`) and its close friend, the pipe (`|`), and see how they can mean different things in different contexts.
### Behind the Scenes
Let's start simple and see how you can use `&` as a way of pushing a command to the background. The instruction:
```
cp -R original/dir/ backup/dir/
```
Copies all the files and subdirectories in _original/dir/_ into _backup/dir/_. So far so simple. But if that turns out to be a lot of data, it could tie up your terminal for hours.
However, using:
```
cp -R original/dir/ backup/dir/ &
```
pushes the process to the background courtesy of the final `&`. This frees you to continue working on the same terminal or even to close the terminal and still let the process finish up. Do note, however, that if the process is asked to print stuff out to the standard output (like in the case of `echo` or `ls`), it will continue to do so, even though it is being executed in the background.
When you push a process into the background, Bash will print out a number. This number is the PID or the _Process' ID_. Every process running on your Linux system has a unique process ID and you can use this ID to pause, resume, and terminate the process it refers to. This will become useful later.
In the meantime, there are a few tools you can use to manage your processes as long as you remain in the terminal from which you launched them:
* `jobs` shows you the processes running in your current terminal, whether be it in the background or foreground. It also shows you a number associated with each job (different from the PID) that you can use to refer to each process:
```
$ jobs
[1]- Running cp -i -R original/dir/* backup/dir/ &
[2]+ Running find . -iname "*jpg" > backup/dir/images.txt &
```
* `fg` brings a job from the background to the foreground so you can interact with it. You tell `fg` which process you want to bring to the foreground with a percentage symbol (`%`) followed by the number associated with the job that `jobs` gave you:
```
$ fg %1 # brings the cp job to the foreground
cp -i -R original/dir/* backup/dir/
```
If the job was stopped (see below), `fg` will start it again.
* You can stop a job in the foreground by holding down [Ctrl] and pressing [Z]. This doesn't abort the action, it pauses it. When you start it again with (`fg` or `bg`) it will continue from where it left off...
...Except for [`sleep`][4]: the time a `sleep` job is paused still counts once `sleep` is resumed. This is because `sleep` takes note of the clock time when it was started, not how long it was running. This means that if you run `sleep 30` and pause it for more than 30 seconds, once you resume, `sleep` will exit immediately.
* The `bg` command pushes a job to the background and resumes it again if it was paused:
```
$ bg %1
[1]+ cp -i -R original/dir/* backup/dir/ &
```
As mentioned above, you won't be able to use any of these commands if you close the terminal from which you launched the process or if you change to another terminal, even though the process will still continue working.
To manage background processes from another terminal you need another set of tools. For example, you can tell a process to stop from a a different terminal with the [`kill`][5] command:
```
kill -s STOP <PID>
```
And you know the PID because that is the number Bash gave you when you started the process with `&`, remember? Oh! You didn't write it down? No problem. You can get the PID of any running process with the `ps` (short for _processes_ ) command. So, using
```
ps | grep cp
```
will show you all the processes containing the string " _cp_ ", including the copying job we are using for our example. It will also show you the PID:
```
$ ps | grep cp
14444 pts/3 00:00:13 cp
```
In this case, the PID is _14444_. and it means you can stop the background copying with:
```
kill -s STOP 14444
```
Note that `STOP` here does the same thing as [Ctrl] + [Z] above, that is, it pauses the execution of the process.
To start the paused process again, you can use the `CONT` signal:
```
kill -s CONT 14444
```
There is a good list of many of [the main signals you can send a process here][6]. According to that, if you wanted to terminate the process, not just pause it, you could do this:
```
kill -s TERM 14444
```
If the process refuses to exit, you can force it with:
```
kill -s KILL 14444
```
This is a bit dangerous, but very useful if a process has gone crazy and is eating up all your resources.
In any case, if you are not sure you have the correct PID, add the `x` option to `ps`:
```
$ ps x| grep cp
14444 pts/3 D 0:14 cp -i -R original/dir/Hols_2014.mp4
  original/dir/Hols_2015.mp4 original/dir/Hols_2016.mp4
  original/dir/Hols_2017.mp4 original/dir/Hols_2018.mp4 backup/dir/
```
And you should be able to see what process you need.
Finally, there is nifty tool that combines `ps` and `grep` all into one:
```
$ pgrep cp
8
18
19
26
33
40
47
54
61
72
88
96
136
339
6680
13735
14444
```
Lists all the PIDs of processes that contain the string " _cp_ ".
In this case, it isn't very helpful, but this...
```
$ pgrep -lx cp
14444 cp
```
... is much better.
In this case, `-l` tells `pgrep` to show you the name of the process and `-x` tells `pgrep` you want an exact match for the name of the command. If you want even more details, try `pgrep -ax command`.
### Next time
Putting an `&` at the end of commands has helped us explain the rather useful concept of processes working in the background and foreground and how to manage them.
One last thing before we leave: processes running in the background are what are known as _daemons_ in UNIX/Linux parlance. So, if you had heard the term before and wondered what they were, there you go.
As usual, there are more ways to use the ampersand within a command line, many of which have nothing to do with pushing processes into the background. To see what those uses are, we'll be back next week with more on the matter.
Read more:
[Linux Tools: The Meaning of Dot][1]
[Understanding Angle Brackets in Bash][2]
[More About Angle Brackets in Bash][3]
--------------------------------------------------------------------------------
via: https://www.linux.com/blog/learn/2019/2/and-ampersand-and-linux
作者:[Paul Brown][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.linux.com/users/bro66
[b]: https://github.com/lujun9972
[1]: https://www.linux.com/blog/learn/2019/1/linux-tools-meaning-dot
[2]: https://www.linux.com/blog/learn/2019/1/understanding-angle-brackets-bash
[3]: https://www.linux.com/blog/learn/2019/1/more-about-angle-brackets-bash
[4]: https://ss64.com/bash/sleep.html
[5]: https://bash.cyberciti.biz/guide/Sending_signal_to_Processes
[6]: https://www.computerhope.com/unix/signals.htm

View File

@ -1,126 +0,0 @@
[#]: collector: (lujun9972)
[#]: translator: (MjSeven)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Getting started with Vim visual mode)
[#]: via: (https://opensource.com/article/19/2/getting-started-vim-visual-mode)
[#]: author: (Susan Lauber https://opensource.com/users/susanlauber)
Getting started with Vim visual mode
======
Visual mode makes it easier to highlight and manipulate text in Vim.
![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/programming_code_keyboard_orange_hands.png?itok=G6tJ_64Y)
Ansible playbook files are text files in a YAML format. People who work regularly with them have their favorite editors and plugin extensions to make the formatting easier.
When I teach Ansible with the default editor available in most Linux distributions, I use Vim's visual mode a lot. It allows me to highlight my actions on the screen—what I am about to edit and the text manipulation task I'm doing—to make it easier for my students to learn.
### Vim's visual mode
When editing text with Vim, visual mode can be extremely useful for identifying chunks of text to be manipulated.
Vim's visual mode has three versions: character, line, and block. The keystrokes to enter each mode are:
* Character mode: **v** (lower-case)
* Line mode: **V** (upper-case)
* Block mode: **Ctrl+v**
Here are some ways to use each mode to simplify your work.
### Character mode
Character mode can highlight a sentence in a paragraph or a phrase in a sentence. Then the visually identified text can be deleted, copied, changed, or modified with any other Vim editing command.
#### Move a sentence
To move a sentence from one place to another, start by opening the file and moving the cursor to the first character in the sentence you want to move.
![](https://opensource.com/sites/default/files/uploads/vim-visual-char1.png)
* Press the **v** key to enter visual character mode. The word **VISUAL** will appear at the bottom of the screen.
* Use the Arrow keys to highlight the desired text. You can use other navigation commands, such as **w** to highlight to the beginning of the next word or **$** to include the rest of the line.
* Once the text is highlighted, press the **d** key to delete the text.
* If you deleted too much or not enough, press **u** to undo and start again.
* Move your cursor to the new location and press **p** to paste the text.
#### Change a phrase
You can also highlight a chunk of text that you want to replace.
![](https://opensource.com/sites/default/files/uploads/vim-visual-char2.png)
* Place the cursor at the first character you want to change.
* Press **v** to enter visual character mode.
* Use navigation commands, such as the Arrow keys, to highlight the phrase.
* Press **c** to change the highlighted text.
* The highlighted text will disappear, and you will be in Insert mode where you can add new text.
* After you finish typing the new text, press **Esc** to return to command mode and save your work.
![](https://opensource.com/sites/default/files/uploads/vim-visual-char3.png)
### Line mode
When working with Ansible playbooks, the order of tasks can matter. Use visual line mode to move a task to a different location in the playbook.
#### Manipulate multiple lines of text
![](https://opensource.com/sites/default/files/uploads/vim-visual-line1.png)
* Place your cursor anywhere on the first or last line of the text you want to manipulate.
* Press **Shift+V** to enter line mode. The words **VISUAL LINE** will appear at the bottom of the screen.
* Use navigation commands, such as the Arrow keys, to highlight multiple lines of text.
* Once the desired text is highlighted, use commands to manipulate it. Press **d** to delete, then move the cursor to the new location, and press **p** to paste the text.
* **y** (yank) can be used instead of **d** (delete) if you want to copy the task.
#### Indent a set of lines
When working with Ansible playbooks or YAML files, indentation matters. A highlighted block can be shifted right or left with the **>** and **<** keys.
![]9https://opensource.com/sites/default/files/uploads/vim-visual-line2.png
* Press **>** to increase the indentation of all the lines.
* Press **<** to decrease the indentation of all the lines.
Try other Vim commands to apply them to the highlighted text.
### Block mode
The visual block mode is useful for manipulation of specific tabular data files, but it can also be extremely helpful as a tool to verify indentation of an Ansible playbook.
Tasks are a list of items and in YAML each list item starts with a dash followed by a space. The dashes must line up in the same column to be at the same indentation level. This can be difficult to see with just the human eye. Indentation of other lines within the task is also important.
#### Verify tasks lists are indented the same
![](https://opensource.com/sites/default/files/uploads/vim-visual-block1.png)
* Place your cursor on the first character of the list item.
* Press **Ctrl+v** to enter visual block mode. The words **VISUAL BLOCK** will appear at the bottom of the screen.
* Use the Arrow keys to highlight the single character column. You can verify that each task is indented the same amount.
* Use the Arrow keys to expand the block right or left to check whether the other indentation is correct.
![](https://opensource.com/sites/default/files/uploads/vim-visual-block2.png)
Even though I am comfortable with other Vim editing shortcuts, I still like to use visual mode to sort out what text I want to manipulate. When I demo other concepts during a presentation, my students see a tool to highlight text and hit delete in this "new to them" text only editor.
--------------------------------------------------------------------------------
via: https://opensource.com/article/19/2/getting-started-vim-visual-mode
作者:[Susan Lauber][a]
选题:[lujun9972][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/susanlauber
[b]: https://github.com/lujun9972

View File

@ -0,0 +1,206 @@
[#]: collector: (lujun9972)
[#]: translator: (HankChow)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (And, Ampersand, and & in Linux)
[#]: via: (https://www.linux.com/blog/learn/2019/2/and-ampersand-and-linux)
[#]: author: (Paul Brown https://www.linux.com/users/bro66)
Linux 中的 &
======
![](https://www.linux.com/sites/lcom/files/styles/rendered_file/public/ampersand.png?itok=7GdFO36Y)
如果阅读过我之前的[三][1][篇][2][文章][3],你会觉得掌握连接各个命令之间的连接符号用法也是很重要的。实际上,命令的用法并不难,例如 `mkdir`、`touch` 和 `find` 也分别可以简单概括为“建立新目录”、“更新文件”和“在目录树中查找文件”而已。
但如果要理解
```
mkdir test_dir 2>/dev/null || touch images.txt && find . -iname "*jpg" > backup/dir/images.txt &
```
这一串命令的目的,以及为什么要这样写,就没有这么简单了。
关键之处就在于命令之间的连接符号。掌握了这些符号的用法,不仅可以让你更好理解整体的工作原理,还可以让你知道如何将不同的命令有效地结合起来,提高工作效率。
在这一篇文章和下一篇文章中,我会介绍如何使用 `&` 号和管道符号(`|`)在不同场景下的使用方法。
### 幕后工作
我来举一个简单的例子,看看如何使用 `&` 号将下面这个命令放到后台运行:
```
cp -R original/dir/ backup/dir/
```
这个命令的目的是将 `original/dir/` 的内容递归地复制到 `backup/dir/` 中。虽然看起来很简单,但是如果原目录里面的文件太大,在执行过程中终端就会一直被卡住。
所以,可以在命令的末尾加上一个 `&` 号,将这个任务放到后台去执行:
```
cp -R original/dir/ backup/dir/ &
```
任务被放到后台执行之后,就可以立即继续在同一个终端上工作了,甚至关闭终端也不影响这个任务的正常执行。需要注意的是,如果要求这个任务输出内容到标准输出中(例如 `echo``ls`),即使使用了 `&`,也会等待这些输出任务在前台运行完毕。
当使用 `&` 将一个进程放置到后台运行的时候Bash 会提示这个进程的进程 ID。在 Linux 系统中运行的每一个进程都有一个唯一的进程 ID你可以使用进程 ID 来暂停、恢复或者终止对应的进程,因此进程 ID 是非常重要的。
这个时候,只要你还停留在启动进程的终端当中,就可以使用以下几个命令来对管理后台进程:
* `jobs` 命令可以显示当前终端正在运行的进程,包括前台运行和后台运行的进程。它对每个正在执行中的进程任务分配了一个序号(这个序号不是进程 ID可以使用这些序号来引用各个进程任务。
```
$ jobs
[1]- Running cp -i -R original/dir/* backup/dir/ &
[2]+ Running find . -iname "*jpg" > backup/dir/images.txt &
```
* `fg` 命令可以将后台运行的进程任务放到前台运行,这样可以比较方便地进行交互。根据 `jobs` 命令提供的进程任务序号,再在前面加上 `%` 符号,就可以把相应的进程任务放到前台运行。
```
$ fg %1 # 将上面序号为 1 的 cp 任务放到前台运行
cp -i -R original/dir/* backup/dir/
```
如果这个进程任务是暂停状态,`fg` 命令会将它启动起来。
* 使用 `ctrl+z` 组合键可以将前台运行的任务暂停,仅仅是暂停,而不是将任务终止。当使用 `fg` 或者`bg` 命令将任务重新启动起来的时候,任务会从被暂停的位置开始执行。但 [`sleep`][4] 命令是一个特例,`sleep` 任务被暂停的时间会计算在 `sleep` 时间之内。因为 `sleep` 命令依据的是系统时钟的时间,而不是实际运行的时间。也就是说,如果运行了 `sleep 30`,然后将任务暂停 30 秒以上,那么任务恢复执行的时候会立即终止并退出。
* `bg` 命令会将任务放置到后台执行,如果任务是暂停状态,也会被启动起来。
```
$ bg %1
[1]+ cp -i -R original/dir/* backup/dir/ &
```
如上所述,以上几个命令只能在同一个终端里才能使用。如果启动进程任务的终端被关闭了,或者切换到了另一个终端,以上几个命令就无法使用了。
如果要在另一个终端管理后台进程,就需要其它工具了。例如可以使用 [`kill`][5] 命令从另一个终端终止某个进程:
```
kill -s STOP <PID>
```
这里的 PID 就是使用 `&` 将进程放到后台时 Bash 显示的那个进程 ID。如果你当时没有把进程 ID 记录下来,也可以使用 `ps` 命令(代表 process来获取所有正在运行的进程的进程 ID就像这样
```
ps | grep cp
```
执行以后会显示出包含 `cp` 字符串的所有进程,例如上面例子中的 `cp` 进程。同时还会显示出对应的进程 ID
```
$ ps | grep cp
14444 pts/3 00:00:13 cp
```
在这个例子中,进程 ID 是 14444因此可以使用以下命令来暂停这个后台进程
```
kill -s STOP 14444
```
注意,这里的 `STOP` 等同于前面提到的 `ctrl+z` 组合键的效果,也就是仅仅把进程暂停掉。
如果想要把暂停了的进程启动起来,可以对进程发出 `CONT` 信号:
```
kill -s CONT 14444
```
这个给出一个[可以向进程发出的常用信号][6]列表。如果想要终止一个进程,可以发送 `TERM` 信号:
```
kill -s TERM 14444
```
如果进程不响应 `TERM` 信号并拒绝退出,还可以发送 `KILL` 信号强制终止进程:
```
kill -s KILL 14444
```
强制终止进程可能会有一定的风险,但如果遇到进程无节制消耗资源的情况,这样的信号还是能够派上用场的。
另外,如果你不确定进程 ID 是否正确,可以在 `ps` 命令中加上 `x` 参数:
```
$ ps x| grep cp
14444 pts/3 D 0:14 cp -i -R original/dir/Hols_2014.mp4
original/dir/Hols_2015.mp4 original/dir/Hols_2016.mp4
original/dir/Hols_2017.mp4 original/dir/Hols_2018.mp4 backup/dir/
```
这样就可以看到是不是你需要的进程 ID 了。
最后介绍一个将 `ps``grep` 结合到一起的命令:
```
$ pgrep cp
8
18
19
26
33
40
47
54
61
72
88
96
136
339
6680
13735
14444
```
`pgrep` 可以直接将带有字符串 `cp` 的进程的进程 ID 显示出来。
可以加上一些参数让它的输出更清晰:
```
$ pgrep -lx cp
14444 cp
```
在这里,`-l` 参数会让 `pgrep` 将进程的名称显示出来,`-x` 参数则是让 `pgrep` 完全匹配 `cp` 这个命令。如果还想了解这个命令的更多细节,可以尝试运行 `pgrep -ax`
### 总结
在命令的末尾加上 `&` 可以让我们理解前台进程和后台进程的概念,以及如何管理这些进程。
在 UNIX/Linux 术语中,在后台运行的进程被称为 daemon。如果你曾经听说过这个词那你现在应该知道它的意义了。
和其它符号一样,`&` 在命令行中还有很多别的用法。在下一篇文章中,我会更详细地介绍。
阅读更多:
[Linux Tools: The Meaning of Dot][1]
[Understanding Angle Brackets in Bash][2]
[More About Angle Brackets in Bash][3]
--------------------------------------------------------------------------------
via: https://www.linux.com/blog/learn/2019/2/and-ampersand-and-linux
作者:[Paul Brown][a]
选题:[lujun9972][b]
译者:[HankChow](https://github.com/HankChow)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.linux.com/users/bro66
[b]: https://github.com/lujun9972
[1]: https://www.linux.com/blog/learn/2019/1/linux-tools-meaning-dot
[2]: https://www.linux.com/blog/learn/2019/1/understanding-angle-brackets-bash
[3]: https://www.linux.com/blog/learn/2019/1/more-about-angle-brackets-bash
[4]: https://ss64.com/bash/sleep.html
[5]: https://bash.cyberciti.biz/guide/Sending_signal_to_Processes
[6]: https://www.computerhope.com/unix/signals.htm

View File

@ -0,0 +1,117 @@
[#]: collector: (lujun9972)
[#]: translator: (MjSeven)
[#]: reviewer: ( )
[#]: publisher: ( )
[#]: url: ( )
[#]: subject: (Getting started with Vim visual mode)
[#]: via: (https://opensource.com/article/19/2/getting-started-vim-visual-mode)
[#]: author: (Susan Lauber https://opensource.com/users/susanlauber)
Vim 可视化模式入门
======
可视化模式使得在 Vim 中高亮显示和操作文本变得更加容易。
![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/programming_code_keyboard_orange_hands.png?itok=G6tJ_64Y)
Ansible playbook 文件是 YAML 格式的文本文件,经常与它们打交道的人有他们最喜欢的编辑器和扩展插件以使格式化更容易。
当我使用大多数 Linux 发行版中提供的默认编辑器来教 Ansible 时,我经常使用 Vim 的可视化模式。它允许我在屏幕上高亮显示我的操作 -- 我要编辑什么以及我正在做的文本处理任务,以便使我的学生更容易学习。
### Vim 的可视化模式
使用 Vim 编辑文本时,可视化模式对于识别要操作的文本块非常有用。
Vim 的可视模式有三个模式:字符,行和块。进入每种模式的按键是:
* 字符模式: **v** (小写)
* 行模式: **V** (大写)
* 块模式: **Ctrl+v**
下面是使用每种模式简化工作的一些方法。
### 字符模式
字符模式可以高亮显示段落中的一个句子或句子中的一个短语,然后,可以使用任何 Vim 编辑命令删除、复制、更改或修改可视化模式识别的文本。
#### 移动一个句子
要将句子从一个地方移动到另一个地方,首先打开文件并将光标移动到要移动的句子的第一个字符。
![](https://opensource.com/sites/default/files/uploads/vim-visual-char1.png)
* 按下 **v** 键进入可视化字符模式。单词 **VISUAL** 将出现在屏幕底部。
* 使用箭头来高亮显示所需的文本。你可以使用其他导航命令,例如 **w** 高亮显示至下一个单词的开头,**$** 来包含其余行。
* 在文本高亮显示后,按下 **d** 删除文本。
* 如果你删除得太多或不够,按下 **u** 撤销并重新开始。
* 将光标移动到新位置,然后按 **p** 粘贴文本。
#### 改变一个短语
你还可以高亮显示要替换的文本块。
![](https://opensource.com/sites/default/files/uploads/vim-visual-char2.png)
* 将光标放在要更改的第一个字符处。
* 按下 **v** 进入可视化字符模式。
* 使用导航命令(如箭头键)高亮显示短语。
* 按下 **c** 可更改高亮显示的文本。
* 高亮显示的文本将消失,你将处于插入模式,你可以在其中添加新文本。
* 输入新文本后,按下 **Esc** 返回命令模式并保存你的工作。
![](https://opensource.com/sites/default/files/uploads/vim-visual-char3.png)
### 行模式
使用 Ansible playbooks 时,任务的顺序很重要。使用可视化行模式将任务移动到 playbooks 中的其他位置。
#### 操纵多行文本
![](https://opensource.com/sites/default/files/uploads/vim-visual-line1.png)
* 将光标放在要操作的文本的第一行或最后一行的任何位置。
* 按下 **Shift+V** 进入行模式。单词 **VISUAL LINE** 将出现在屏幕底部。
* 使用导航命令(如箭头键)高亮显示多行文本。
* 高亮显示所需文本后,使用命令来操作它。按下 **d** 删除,然后将光标移动到新位置,按下 **p** 粘贴文本。
* 如果要复制任务,可以使用 **y**(yank) 来代替 **d**(delete)。
#### 缩进一组行
使用 Ansible playbooks 或 YAML 文件时,缩进很重要。高亮显示的块可以使用 **>** 和 **<** 键向右或向左移动。
![](https://opensource.com/sites/default/files/uploads/vim-visual-line2.png)
* 按下 **>** 增加所有行的缩进。
* 按下 **<** 减少所有行的缩进。
尝试其他 Vim 命令将它们应用于高亮显示的文本。
### 块模式
可视化块模式对于操作特定的表格数据文件非常有用,但它作为验证 Ansible playbook 缩进的工具也很有帮助。
任务是项目列表,在 YAML 中,每个列表项都以破折号和空格开头。破折号必须在同一列中对齐,以达到相同的缩进级别。仅凭肉眼很难看出这一点。缩进任务中的其他行也很重要。
#### 验证任务列表缩进相同
![](https://opensource.com/sites/default/files/uploads/vim-visual-block1.png)
* 将光标放在列表项的第一个字符上。
* 按下 **Ctrl+v** 进入可视化块模式。单词 **VISUAL BLOCK** 将出现在屏幕底部。
* 使用箭头键高亮显示单个字符列。你可以验证每个任务的缩进量是否相同。
* 使用箭头键向右或向左展开块,以检查其它缩进是否正确。
![](https://opensource.com/sites/default/files/uploads/vim-visual-block2.png)
尽管我对其它 Vim 编辑快捷方式很熟悉,但我仍然喜欢使用可视化模式来整理我想要出来处理的文本。当我在演示过程总演示其它概念时,我的学生会看到一个高亮显示文本的工具,并在这个“仅限他们”的文本编辑器中点击删除。
--------------------------------------------------------------------------------
via: https://opensource.com/article/19/2/getting-started-vim-visual-mode
作者:[Susan Lauber][a]
选题:[lujun9972][b]
译者:[MjSeven](https://github.com/MjSeven)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://opensource.com/users/susanlauber
[b]: https://github.com/lujun9972