Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm trying to comment multiple source code lines with following map:

vmap  <leader>// :'<,'>-1s/^/\/\//

However it doesn't work. What's wrong?

share|improve this question
    
FYI, people always rave about NERD commenter –  pydave Nov 19 '11 at 1:12

1 Answer 1

up vote 2 down vote accepted

Try:

:vmap <leader>// :s/^/\/\//<cr>

(You need to remove the range, because vmap does that automatically. Adding the <cr> will automatically execute it.)

This will only match lines that have one or more characters in it. That means it will skip the last line, but it also means it will skip empty lines...

:vmap <leader>// :s/^\(.\+\)\@=/\/\//g<cr>  

This may be what you are specifically looking for:

:vmap <leader>// :<C-U>'<,'>-1s/^/\/\//g<cr> 

The <C-U> removes the automatically included range, and then you can specify your own range like in your original question...

share|improve this answer
    
John Weldon: Thanks for your help. But, what if I want the change the range? –  Jichao Sep 27 '10 at 7:03
    
i.e. Not include all of the text you've selected? –  John Weldon Sep 27 '10 at 14:35
    
Yes, because when I am using gvim under Windows, '> will always get a more line. –  Jichao Sep 28 '10 at 5:03
    
Added a couple more alternatives. –  John Weldon Sep 28 '10 at 5:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.