tcomment_vim
tcomment_vim copied to clipboard
Bad Toggling?
asdf
" zxcv
:set ft=vim
vip
gc
" asdf
" " zxcv
That doesn't seem right.
That doesn't seem right. That's the way, tcomment works. If any line is not a comment, it is assumed you want to comment the selected lines. If all lines are commented out, it is assumed you want to uncomment the selected lines.
For line-wise toggling of comments if that's what you want, you either have to run tcomment linewise (e.g. by invoking :'<,'> norm gcc) or use another plugin.
That does make more sense.
However I have a use for pure toggling - is it possible to make :'<,'> norm gcc
an operator on-par with gc
?
Ok, got it:
noremap <silent> gC :set opfunc=ToggleComment<CR>g@
vnoremap <silent> gC :<C-U>call ToggleComment(visualmode())<CR>
function! ToggleComment(type)
" motion
if a:type == "line" || a:type == "char" || a:type == "block"
silent '[,'] norm gcc
" visual
else
silent '<,'> norm gcc
endif
endfunction
Edit to avoid slowdown: gct
-> gC
In my experience, tcomment's toggling behavior is often surprising, commenting things deeper rather than truly toggling in/out of comment syntax, compared to lots of other text editor comment toggles.
Could we amend the way that tcomment treats marked regions as commented/uncommented, or offer a command like gc
, but with a more traditional toggling behavior?
tcomment was modelled after how I remembered emacs was working. Remove comments if all lines are commented, comment out if any line is not commented out. The main reason I wrote tcomment was that I hated the other commenting plugins that simply did line-wise toggling, which is cheaper to implement.
Do the g< and g> maps (|g:tcommentMapLeaderUncommentAnyway| and |g:tcommentMapLeaderCommentAnyway|) improve the situation for you?