Skip to main content

Comments in vim

Posted in

When writing code, I always wanted to be able to select a block of code and then [un]comment it. Here are my attempts to make it possible. The main achievent is that it works in 'VISUAL' mode too.

map // :CommentBlock<CR>
 
command! -range CommentBlock :noh|execute CommentRange(<line1>, <line2>)
 
function! CommentRange(start, stop)
    if strpart(getline(a:start), 0, 2) == "//"
        return ":" . a:start. "," . a:stop . ' s/\/\///|noh'
    else
        return ":" . a:start. "," . a:stop . 's/^/\/\//|noh'
    endif
endfunction

Limitation:

  • comment presence is detected by ^//
  • C/C++ oriented

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
 o.OOOo.                          .oOOOo.             
O `o .oOOo. .O o.
o O O o O
O o o O o
o O ooOO .oOo. O' o O 'o O
O o o O o O O Oo o O o o
o .O' O o O .O `o O' o O O
OooOO' OooO oOoO' oOoOoO `OoooO Oo `Oo'oO'
O
o'
Enter the code depicted in ASCII art style.