Commenting Multiple Lines with Vim

This could be really useful. As usual, I’m documenting this for myself as well.

To do this you need to understand something called a Visual Block in Vim. People who don’t want to understand can skip this paragraph. A visual block selection is a way of selection by which you consider the characters in the text file as part of a matrix. So when you make a visual selection, you could select one column or multiple columns. Selection by Columns!. That is something you cannot find in your usual lightweight text editor right?

Anyways, the process goes like this. Use Ctrl + v to begin visual block selection. Select the first column of the lines you want to comment. Then, Shift + i + <whatever_comment_character>. Then press Esc to exit the visual block selection mode. Give it a second to process. You should see the comment character on the lines that you selected.

To remove comments, follow a similar procedure. Use visual block selection to select the column of comment characters by pressing Ctrl + v and then using the arrow keys to select them. Then press to cut them away. Done!

Advertisements

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