Tip #369: Comment/UnComment visually selected text
tip karma |
Rating 60/25, Viewed by 4509
|
Read and edit this tip on the
Vim tip wiki.
The wiki may have a more recent version of this tip.
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
------------------------------------------------------------------------------
<< 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.
|
[email protected],
April 7, 2003 3:02
|
This is my current setup, it works very nicely.
au FileType haskell,vhdl,ada let b:comment_leader = '-- '
au FileType vim let b:comment_leader = '" '
au FileType c,cpp,java let b:comment_leader = '// '
au FileType sh,make let b:comment_leader = '# '
au FileType tex let b:comment_leader = '% '
noremap <silent> ,c :<C-B>sil <C-E>s/^/<C-R>=escape(b:comment_leader,'\/')<CR>/<CR>:noh<CR>
noremap <silent> ,u :<C-B>sil <C-E>s/^\V<C-R>=escape(b:comment_leader,'\/')<CR>//e<CR>:noh<CR>
,c comments out a region
,u uncomments a region
|
[email protected],
May 2, 2003 1:04
|
Is there a way to add a popup item too? Menu becomes too cumbersome to access.
|
Okki,
March 27, 2004 5:42
|
d95mback -> You da man! Kudos for you!
|
|