Vim tip: Visual block editing

One of the features I find very convenient in Vim editing in visual blocks.

With CTRL+V (while in normal mode) you can activate the visual block mode, by using the standard movement keys (h, j, k, l or the arrow keys). You can perform various operations on visual blocks (check the help with :help v), in this article I will describe how you can prepend or append text to a block.

After selecting your block you can press SHIFT+i to start inserting before every line of the block, after you type what you want to insert hit the ESC key to apply the insert to all the lines. The below images show you how this would work:

Visual mode active, selected a block:

Visual mode active, selected a block

Hit the ESC key to apply the insert:

Hit the ESC key to apply the insert

To append instead you can press SHIFT+a instead of SHIFT+i, the mechanism is exactly the same:

Visual mode active, selected a block:

Visual mode active, selected a block

Append mode activated (using SHIFT+a) and typed a “:

Hit the ESC key to apply the append:

Hit the ESC key to apply the append

As you can see in the above example white space is appended to Dog and Cat, because these words are shorter than Bird. If you don’t want that white space you can use CTRL+V $ instead of CTRL+V to activate visual mode:

Visual mode active, selected a block:

Visual mode active, selected a block

Append mode activated (using SHIFT+a) and typed a “:

Hit ESC to apply the append:

Hit ESC to apply the append

As noted earlier in the article there are more thing that you can do with visual mode in vim, you can read about it in Vim’s help using “:help v”.

Happy Vimming!

Advertisements
Advertisements
%d bloggers like this: