2009-09-25

vim - Commenting multiple lines, block comment

Perl does not have block comment feature. The Acme::Comment module was the easiest way to make Perl do block commenting, with additional features, it allows customization of open & close punctuation mark.

Until yesterday, my first Perl code got some problem and had to seek help from the IRC channel perl-help @ irc.perl.org (these guys are really helpful, thanks dudes!). Just when they were helping me to solve the problem, Caelum tip me on how to block comment in Perl using vim. I thought it was a good idea as it doesn't need to load additional module to handle the block comment thing, which save some CPU processing.

Here it is :
1. While editing the Perl program in vim, press Ctrl-v to enter "visual block" mode.
2. Move the cursor downwards / upwards so that it marks the lines, for comments.
3. Activate "insert" mode with beginning of the line (shift "i" and not "i" only).
4. Type the "#" character.
5. End the insert mode with "esc" key
6. After a second, the lines marked in step 2 are all commented.

Voilla !!!

4 comments:

Peter Makholm said...

You can also use pod for comments:

print "something";

=begin comment

print "this is not executed";

=cut

print "Here we go again";

monkey said...

Hi Peter,

Hmm, you're right... But coming from Bash script, the "#" comment seems more obvious to my eyes ... :P

Thanks for the suggestion though. now i know of another alternative to comment. :)

Regards.

Michael Peters said...

I have a simple Perl script (http://gist.github.com/193541) I use for this and I call it from vim. It's called "comment". I also symlink it to be "uncomment" as well and it changes based on how it's called. Not only does it work for Perl, but also works for HTML, CSS, Javascript, C, etc. So after you've highlighted your lines you can just say:

:!comment

which will comment Perl by default

Or

:!comment css

Which will do CSS. And then when you want to uncomment that block...

:!uncomment css

I know there's a vim plugin that will do something like this too, but it requires that you learn some new key bindings and also doesn't handle files that have multiple languages in them (like HTML files with CSS or Javascript).

monkey said...

Hi Michael,

That's very generous of you to help out! I'll sure give it a try, thanks dude. :)

Regards.