Vim Tip 1

Welcome to vim tips, where I publish a blog post weekly with a tip on vim that I have found useful.

I stumbled onto one today that had some use. I wanted to wrap several lines of text in a paragraph tag. The text can look something like this:

This is the first line
This is the second line
This is the third line
This is the four line

So I hit V (Shift+v) in normal mode, and highligted those four lines. Next I hit : which brings me into command mode on a selection of text.

To add the tag at the beginning is as simple as this:

:'<,'>normal ^i<p>

Now this adds a to the beginning of each line, now in order to get to the end of each of the lines I need to get back into normal mode. I tried typing <Ctrl+[> and then I stumbled onto the answer from this reddit post.

When you’re in command mode, hit <Ctrl+v>, and this will insert ^[ into your command, note though you cannot just type that in and have it work the same. So with that we can now finish out the command, and have each line wrapped in a paragraph tag.

Command:

:'<,'>normal ^i<p>^[A<p>

Result:

<p>This is the first line</p>
<p>This is the second line</p>
<p>This is the third line</p>
<p>This is the four line</p>