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
------------------------------------------------------------------------------
<<Use gvim in VS.Net |
always cd to the current file's directory >>
Additional Notes
|