Monday, March 30, 2009

Toggle Comment in VIM in one line using perl

There are lot of scripts in VIM which can toggle comments for you. But interestingly there is not a one liner which I can execute on systems where I do not have my vimrc file.

try this mapping

:map F2 V:!perl -nle 'if (/^\s*\/\//) { s/\s*\/\/\s*(.*)/$1/g; print } else {print "//".$_}'^Mj

You can map Alt+C or Ctrl+T something which is faster than F2

This mapping will comment and uncomment the C-style comment. I have tried this on Ubuntu and VIM 7.1 and works fine for me

Note: The map command may not work on copying as the VIM may not recognize the Ctrl - M character. You have to Ctrl- V and then Enter to make it work

1 comment:

  1. Perl is the best scripting language for Text processing and handle regex. I have posted few articles related to those at my blog

    Also Perl's Cpan has lots of support that I don't even need to think extra while developing project. I didn't find such help on other programming language except Java and .NET