tcomment_vim icon indicating copy to clipboard operation
tcomment_vim copied to clipboard

Bad Toggling?

Open orbisvicis opened this issue 9 years ago • 5 comments

asdf
" zxcv
:set ft=vim
vip
gc
" asdf
" " zxcv

That doesn't seem right.

orbisvicis avatar Apr 01 '15 00:04 orbisvicis

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.

tomtom avatar Apr 01 '15 15:04 tomtom

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 ?

orbisvicis avatar Apr 01 '15 17:04 orbisvicis

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

orbisvicis avatar Apr 01 '15 18:04 orbisvicis

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?

mcandre avatar Jul 11 '16 16:07 mcandre

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?

tomtom avatar Jul 11 '16 17:07 tomtom