Pages

May 13, 2011

vim:Function to comment a visually selected text

In your vimrc add following function

"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
" map visual mode keycombo 'com' to this function
vmap com :call Comment(fl, ll)
vmap uncom :call UnComment(fl, ll)

No comments: