tcomment

March 12, 2012

When working with code, there are plenty of times you want to comment out large sections of code.  I do this a lot when refactoring or going through a new codebase.  There are plenty of tricks in vim for commenting out blocks of code however they may operate differently based on the language you’re working in.  The best way I’ve found to comment multiple lines is tcomment.  This little vim plugin allows you to comment out large blocks of code in almost any language.  Once you’ve set it up (I use pathogen), you can use the command:

:TComment

after highlighting a section of text and like magic, its all commented out.  The only time that I’ve ran into issues is when I’m editing a rails html.erb file and I wish to comment out a section of html.  In that case I use the command:

:TComment As html

because tcomment assumes that I am working in ruby.  This will only provide html comments, the ruby inside would still be executed.

I added the two lines below to my vimrc file.  This gives me the ability to simply highlight the text and then use <Leader>cc to comment out ruby or <Leader>ch to comment out html blocks in a rails file.

"map over the tcomment command
map cc :TComment
"This does tcomment for html
map ch :TCommentAs html

Discussion, links, and tweets

I'm a Senior Software Engineer at Heroku

Follow me on Twitter. I rarely post anything but it can't hurt to try.