S E P
22
2009

vim: comments revisited

A while back I posted a tip about how to quickly comment out a block of lines in vim. Since then I’ve gotten even more lazy and found an even quicker way that involves much less effort on my part. I think this originally came from a vim tip that I CBA to find now, so I’m reposting here to spread the love.

First, add a simple function to do the work:

" Toggle comments on a visual block
function! CommentLines()
    try
            execute ":s@^".g:StartComment." @\@g"
            execute ":s@ ".g:EndComment."$@@g"
    catch
            execute ":s@^@".g:StartComment." @g"
            execute ":s@$@ ".g:EndComment."@g"
    endtry
endfunction

This function comments starting at the beginning of the line. You can modify this behavior to suit your needs by changing the regular expression.

Next you’ll need to set the comment characters for the language you’re working in. My preferred way is to use autocmds to set these based on the current filetype.

" Set comment characters for common languages
autocmd FileType python,sh,bash,zsh,ruby,perl let StartComment="#" | let EndComment=""
autocmd FileType html let StartComment="<!--" | let EndComment="-->"
autocmd FileType php,cpp,javascript let StartComment="//" | let EndComment=""
autocmd FileType c,css let StartComment="/*" | let EndComment="*/"
autocmd FileType vim let StartComment="\"" | let EndComment=""
autocmd FileType ini let StartComment=";" | let EndComment=""

That’s it! Optionally (do this, really it’s stupid not to), you can add a keymap to quickly call our function.

vmap <Leader>c :call CommentLines()<CR>

Now to use this function, just select a visual block and hit your keymap to toggle comments on that block. Voila!

Note: If you need anything more complex than what this tip provides, check out the NERD Commenter. Everything I’ve read about it suggest that it’s excellent.

· · ·

Discussion

Good tip. I think the only problem is your StartComment and EndComment chars: C uses / / and cpp uses //, not the opposite

Lucas De Marchi  posted on Mar. 11th, '10

Well look at that. I never knew it but ‘//’ is a Microsoftism in C. I’ve fixed this error both here and in my vimrc, thank you.

I’ve also updated the post to get rid of the extra function and simplified it down to one function.

randy  posted on Mar. 24th, '10

Join the discussion