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