Question

I am using a vim plugin called tComment

It allows me to comment a line by pressing gc or <c-_><c-_>

Also, it works on the shortcut <c-/><c-/> but the visual selection is lost.

So, I tried:

  • To make it work on single <c-/>
  • To retain the visual selection.

My attempts :

inoremap <c-/> gc
vnoremap <c-/> gc gv
nnoremap <c-/> gc

=========

imap <c-/> gc
vmap <c-/> gc gv
nmap <c-/> gc

=========

imap <c-/> gc$
vmap <c-/> gc$ gv
nmap <c-/> gc$

=========

inoremap <c-/> <c-_><c-_>
vnoremap <c-/> <c-_><c-_> gv
nnoremap <c-/> <c-_><c-_>

=========

imap <c-/> <c-_><c-_>
vmap <c-/> <c-_><c-_> gv
nmap <c-/> <c-_><c-_>

( Non of the above seems to work )

Note:

  1. I have not done any other customizations from my side.
  2. My attempts are listed above
  3. Installing tComment on native vim (Ubuntu) lands you to my setup.
Was it helpful?

Solution 2

If I understand you correctly, you want to have one map (in i, n, & v-mode) that either comments the current line or the visual selection. This is what tcomment's <c-_><c-_> map does now (with the exception that you want to maintain the visual selection). In order to use <c-/> you have to set g:tcommentMapLeader1 = '' (or some other map, since <c-/> seems to be the same as <c-_> as echristopherson pointed out) in vimrc and then define your maps for <c-/>.

This should work (add these lines to .vimrc):

let g:tcommentMapLeader1 = ''
noremap <silent> <c-/> :TComment<cr>
vnoremap <silent> <c-/> :TCommentMaybeInline<cr>gv
inoremap <silent> <c-/> <c-o>:TComment<cr>

You might have to replace <c-/> with <c-_> to make this work. Since you reported that tcomment already worked when typing <c-/><c-/>, the <c-_> map should work.

Anyway, I'd also recommend to use the operator maps since those fit better the way vim works. I don't think using a single key is still a good idea though.

OTHER TIPS

  1. If you want to map keys to another mapping, you need to use :map, not :noremap.
  2. For most plugins, this shouldn't be necessary; they usually provide either configuration variables or <Plug>PluginName... for that. Read :help g:tcommentMaps for instructions for this particular plugin, then place your overrides into your ~/.vimrc.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top