Vim logo vim online Vim Book Ad

 Tip #369: Comment/UnComment visually selected text

 tip karma   Rating 18/10, Viewed by 875 

created:   November 15, 2002 0:57      complexity:   intermediate
author:   Amit Neogy      as of Vim:   5.7

Comment/UnComment visually selected code
========================================

Visually selected code can be easily Commented out and uncommented by using
the following functions. The functions insert/delete C/C++/Java style
comments. The comment characters can be modified by editing the functions.

Add the following to your .vimrc file. It will add two menu items under
the "Edit" menu in gVim. The function calls can be mapped to keystrokes
if desired.

------------------------------------------------------------------------------
"Menu items for Commenting and Un-Commenting code
amenu 20.435 &Edit.-SEP4-; :
amenu Edit.Comment <ESC>`<:let fl=line(".")<CR>`>:let ll=line(".")<CR>:call Comment(fl, ll)<CR>
amenu Edit.UnComment <ESC>`<:let fl=line(".")<CR>`>:let ll=line(".")<CR>:call UnComment(fl, ll)<CR>

"Function for commenting a block of Visually selected text
function Comment(fl, ll)
    let i=a:fl
let comment="//"
while i<=a:ll
    let cl=getline(i)
let cl2=comment.cl
call setline(i, cl2)
let i=i+1
endwhile
endfunction

"Function for Un-Commenting a block of Visually selected text
function UnComment(fl, ll)
    let i=a:fl
let comment="//"
while i<=a:ll
    let cl=getline(i)
let cl2=substitute(cl, "//", "", "")
call setline(i, cl2)
let i=i+1
endwhile
endfunction
------------------------------------------------------------------------------

 rate this tip  Life Changing Helpful Unfulfilling 

<<Use gvim in VS.Net | always cd to the current file's directory >>

Additional Notes

[email protected], November 15, 2002 3:55
There are a few scripts here already, that will do the same in a more general way, nevertheless I'd suggest the following changes

let comment="// "

in Comment() for better readability

and

let cl2=substitute(cl, "^\s*// ", "", "")

in UnComment() to ensure the comment will only be replaced at the beginning of a line.


Anonymous, November 16, 2002 22:52
I find it easier to simply select the area you want to comment out in visual-block mode (usually CTRL-V or CTRL-Q), type "I" (CAPS - EYE) type the comment character '//' and ESC. This gives me the flexibility to change my comment character (for instance make it /// for later searches) and is straight-forward. To uncomment, simply select the comment characters is visual-block mode, and delete them.
anom, November 18, 2002 0:09
What about C (/* ... */)?
Anonymous, November 20, 2002 4:09
isn't more pratical select the lines (block-visual) and insert your comment (i do it inserting 'c' on fortran sources):

<c-v>
... move around ...
0   (be sure to stay at first column!)
I   (insert)
//  (or what you like)
<esc>

to remove them, select it (<c-v> do the magic), and press 'x'
[email protected], November 21, 2002 7:15
I use the following (for haskell style comments, just replace -- with whatever your line comment characters are)

map ,c :s/^/-- /<CR>
map ,u :s/^-- //<CR>

So, typing ,c in normal mode will comment lines, typing ,u will uncomment them.

Works in visual mode as well.
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
SourceForge Logo