Make vim perform function on all highlighted lines, not just first line

2018-05-02 18:43:15

I am trying to get vim 7.4 to do block commenting and uncommenting of highlighted lines. I am so frustratingly close.

let b:commentChar='//'

autocmd BufReadPost *.[ch] let b:commentChar='//'

autocmd BufReadPost *.cpp let b:commentChar='//'

autocmd BufReadPost *.py let b:commentChar='#'

autocmd BufReadPost *.*sh let b:commentChar='#'

function! Docomment ()

"make comments on all the lines we've grabbed

execute 's/^\s*/&'.escape(b:commentChar, '\/').' /e'

nohl

endfunction

function! Uncomment ()

"uncomment on all our lines

execute 's/\v(^\s*)\V'.escape(b:commentChar, '\/').'\v\s*/\1/e'

nohl

endfunction

function! Comment ()

"does the first line begin with a comment?

let l:line=getpos("'<")[1]

"if there's a match

if match(getline(l:line), '^\s*'.b:commentChar)>-1

echom "calling uncomment"

call Uncomment()

else

echom "calling docomment"

call Docomment()

endif

endfunction

vnoremap :call Comment()

Curre