If you need to add the same text on multiple lines e.g. comment out multiple lines, insert semi-colons etc, then visual block mode is your friend!

Let’s take a look at the following ansible YAML playbook :

Assuming we want to comment out the nginx, php-fpm and wordpress roles (lines 12 – 14). In YAML the comment character is ‘#’.

We need to go to line 12, start visual block mode and select the 3 lines, insert a ‘#’ char at beginning of line, exit to normal mode.

In vim you start visual block by pressing <Ctrl+v>. Now these commands:

12G
<Ctrl+v>jj$
I#
<Esc>

Lines 12 to 14 should now be preceeded by a ‘#’. This is because in visual block mode all insert commands are repeated for each line. Nice!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s