comment out blocks of text in vim

On June 29, 2010, in howto, by nate

This is useful when commenting out (or in) blocks of code/text in a file.

Move the cursor to the first line you wish to comment out.
Press shift - v to begin block selection mode. The bottom of the screen will say:

-- VISUAL LINE --

Press down arrow to select the text you wish to comment out. When finished, press : (colon)
The bottom of the screen will change to:

:'<,'>

Append the search and replace statement (replace the beginning of the line – nothing – with a # symbol):

:'<,'>s/^/#

To comment in – or uncomment blocks use this statement (replace the # symbol at the beginning with nothing):

:'<,'>s/^#//

Tagged with:  

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*