Vim 可视化模式入门
Vim

> 
> 可视化模式使得在 Vim 中高亮显示和操作文本变得更加容易。
> 
> 
> 


![](/data/attachment/album/201903/05/072920nj49tttutz0h3hdt.png)


Ansible 剧本文件是 YAML 格式的文本文件，经常与它们打交道的人通过他们偏爱的编辑器和扩展插件以使格式化更容易。


当我使用大多数 Linux 发行版中提供的默认编辑器来教学 Ansible 时，我经常使用 Vim 的可视化模式。它可以让我在屏幕上高亮显示我的操作 —— 我要编辑什么以及我正在做的文本处理任务，以便使我的学生更容易学习。


### Vim 的可视化模式


使用 Vim 编辑文本时，可视化模式对于识别要操作的文本块非常有用。


Vim 的可视模式有三个模式：字符、行和块。进入每种模式的按键是：


* 字符模式: `v` （小写）
* 行模式: `V` （大写）
* 块模式: `Ctrl+v`


下面是使用每种模式简化工作的一些方法。


### 字符模式


字符模式可以高亮显示段落中的一个句子或句子中的一个短语，然后，可以使用任何 Vim 编辑命令删除、复制、更改/修改可视化模式识别的文本。


#### 移动一个句子


要将句子从一个地方移动到另一个地方，首先打开文件并将光标移动到要移动的句子的第一个字符。


![](/data/attachment/album/201903/05/072921kucslglcg8vvmlqz.png)


* 按下 `v` 键进入可视化字符模式。单词 `VISUAL` 将出现在屏幕底部。
* 使用箭头来高亮显示所需的文本。你可以使用其他导航命令，例如 `w` 高亮显示至下一个单词的开头，`$` 来包含该行的其余部分。
* 在文本高亮显示后，按下 `d` 删除文本。
* 如果你删除得太多或不够，按下 `u` 撤销并重新开始。
* 将光标移动到新位置，然后按 `p` 粘贴文本。


#### 改变一个短语


你还可以高亮显示要替换的一段文本。


![](/data/attachment/album/201903/05/072921l8y97c49y4yh0440.png)


* 将光标放在要更改的第一个字符处。
* 按下 `v` 进入可视化字符模式。
* 使用导航命令（如箭头键）高亮显示该部分。
* 按下 `c` 可更改高亮显示的文本。
* 高亮显示的文本将消失，你将处于插入模式，你可以在其中添加新文本。
* 输入新文本后，按下 `Esc` 返回命令模式并保存你的工作。


![](/data/attachment/album/201903/05/072921kuoo3y7ah6o76z37.png)


### 行模式


使用 Ansible 剧本时，任务的顺序很重要。使用可视化行模式将 Ansible 任务移动到该剧本文件中的其他位置。


#### 操纵多行文本


![](/data/attachment/album/201903/05/072922g95coemsmrqrqv1g.png)


* 将光标放在要操作的文本的第一行或最后一行的任何位置。
* 按下 `Shift+V` 进入行模式。单词 `VISUAL LINE` 将出现在屏幕底部。
* 使用导航命令（如箭头键）高亮显示多行文本。
* 高亮显示所需文本后，使用命令来操作它。按下 `d` 删除，然后将光标移动到新位置，按下 `p` 粘贴文本。
* 如果要复制该 Ansible 任务，可以使用 `y`（yank）来代替 `d`（delete）。


#### 缩进一组行


使用 Ansible 剧本或 YAML 文件时，缩进很重要。高亮显示的块可以使用 `>` 和 `<` 键向右或向左移动。


![](/data/attachment/album/201903/05/072922igy72g6gmwfmwffg.png)


* 按下 `>` 增加所有行的缩进。
* 按下 `<` 减少所有行的缩进。


尝试其他 Vim 命令将它们应用于高亮显示的文本。


### 块模式


可视化块模式对于操作特定的表格数据文件非常有用，但它作为验证 Ansible 剧本文件缩进的工具也很有帮助。


Ansible 任务是个项目列表，在 YAML 中，每个列表项都以一个破折号跟上一个空格开头。破折号必须在同一列中对齐，以达到相同的缩进级别。仅凭肉眼很难看出这一点。缩进 Ansible 任务中的其他行也很重要。


#### 验证任务列表缩进相同


![](/data/attachment/album/201903/05/072923yl4ltqq647llaql6.png)


* 将光标放在列表项的第一个字符上。
* 按下 `Ctrl+v` 进入可视化块模式。单词 `VISUAL BLOCK` 将出现在屏幕底部。
* 使用箭头键高亮显示单个字符列。你可以验证每个任务的缩进量是否相同。
* 使用箭头键向右或向左展开块，以检查其它缩进是否正确。


![](/data/attachment/album/201903/05/072923rsjsahtiiipp5sah.png)


尽管我对其它 Vim 编辑快捷方式很熟悉，但我仍然喜欢使用可视化模式来整理我想要出来处理的文本。当我在讲演过程中演示其它概念时，我的学生将会在这个“对他们而言很新”的文本编辑器中看到一个可以高亮文本并可以点击删除的工具。




---


via: <https://opensource.com/article/19/2/getting-started-vim-visual-mode>


作者：[Susan Lauber](https://opensource.com/users/susanlauber) 选题：[lujun9972](https://github.com/lujun9972) 译者：[MjSeven](https://github.com/MjSeven) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
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.

![Highlighted sentence in character visual mode Highlighted sentence in character visual mode](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.

![Phrase highlighted to change in visual character mode Phrase highlighted to change in visual character mode](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.

![Changed text in visual mode Changed text in visual mode](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

![Highlighting text to move in Line mode Highlighting text to move in Line mode](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.

![Highlighting task to decrease indentation in Line mode Highlighting task to decrease indentation in Line mode](https://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

![Block highlighting one column Block highlighting one column](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.

![Block multi-column Block multi-column](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.

## 2 Comments