tags:

views:

273

answers:

5

It happens more often than not that I have to comment several lines at once in vim. Methods that I know are not as fast as say TextMate way to comment lines out.

What is your favorite way to do it?

I currently use

Method 1:

 - go to first char of a line and use blockwise visual mode (CTRL-V)
 - go down/up until first char of all lines I want to comment out are selected
 - use SHIFT-I and then type my comment character (# for ruby)
 - use ESC to insert the comment character for all lines

Method 2:

 - Select lines I need to comment out using linewise visual mode (V)
 - typing : gives me :'<,'> promt which I extend to :'<,'>s/^/#/

Method 3:

 - go to the first line to be commented out
 - make a bookmark for example typing mm 
 - go to the last line to be commented out
 - type :'m,.s/^/#/

I like method 1 the most, but I still hope there is a better way.

A: 

I normally just save the step in a macro and then invoke the macro in whichever fashion I feel like.

Sridhar Iyer
+5  A: 

I think you described the most popular ways to comment code, but if you are open to use Vim Scripts, give a look to those:

CMS
+1 for NERD Commenter
Pierre-Antoine LaFayette
+1  A: 

I use a keymap for the regex part, but I do the same visual selection first. Usually using:

vip

to get the visual block (paragraph visual selection)

then using

\cc
\co

for comment add/remove (cc,co chosen for muscle memory reasons)

with the mappings defined in .vimrc as:

vmap <leader>cc :s/^/#/<cr>
vmap <leader>co :s/^#//<cr>
zen
A: 

Plugins are the way to go. They are extensible, they already support more filetypes that you would ever use, they are automagically able to toggle the commented state of a line (in other words: no need to consume two shortcuts where one is enough).

See the list given by CMS.

Luc Hermitte
A: 

I've been having trouble with the latest NERD_Commenter. It just stopped working and blindly applied /* */ to everything regardless of filetype.

That being said, I just use the Control+V Shift+I approach.

Bryan Ross