3

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.
Yugal Jindle
  • 44,057
  • 43
  • 129
  • 197

2 Answers2

5
  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.
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • Tried all you said. But didn't help. – Yugal Jindle Nov 08 '13 at 09:49
  • Please edit your question to reflect your attempts. Since tComment uses config variables, you need to define then _before_ the plugin is loaded, e.g. in your .vimrc. – Ingo Karkat Nov 08 '13 at 10:04
  • I am comparatively new to `vim`. I have not done any customizations from my side. You can install `tcomment` and will have the same setup as me. – Yugal Jindle Nov 08 '13 at 10:49
  • I have updated the question with additional findings. – Yugal Jindle Nov 08 '13 at 10:53
  • Your update still says `noremap`, which I told you does not work. And as per 2., rather try something like `:let g:tcommentMapLeaderOp1 = ''` – Ingo Karkat Nov 08 '13 at 11:04
  • Also note that the `gc` command wants a `{motion}` after it (to cover the text to be commented), so `gc$` would comment the current line. – Ingo Karkat Nov 08 '13 at 11:06
  • Added other attempts. Please note even after I deleted the mapping from my vimrc - it still seems to work with ( 2 times ). But I am unable to find a corresponding binding the plugin or anywhere in the mappings. – Yugal Jindle Nov 08 '13 at 11:30
  • @yugal: The maps are defined in plugin/tcomment.vim. – lith Nov 11 '13 at 12:25
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.

lith
  • 929
  • 8
  • 23
  • As I understand : I can invoke the `g:tcommentMapLeader` with a key mapping as required. So, I didn't exactly understood what you said. Were you able to achieve to the solution ? – Yugal Jindle Nov 11 '13 at 07:59
  • Sorry, but after adding the above - tComment stopped working with `` or ``. Now, I am looking into `Operator Maps` ( New to me ). But the issue stands un-resolved. :( Can you try it yourself - if I am messing something else ! – Yugal Jindle Nov 11 '13 at 12:46
  • @yugal-jindle: The maps stopped working because `let g:tcommentMapLeader1 = ''` disabled them -- you can use an alternative map here if you want to keep those maps. Could you then please on the command-line type: and check what is inserted on the commandline? (E.g. I cannot type on my keyboard, only , which probably is the reason why echristopherson meant that they are internally the same. If nothing gets inserted, you might want to use in the above example.) – lith Nov 11 '13 at 13:05
  • Sir, I got what you are saying. And expected the following to work : `let g:tcommentMapLeader1 = 'mm'` `noremap :TComment` `vnoremap :TCommentMaybeInlinegv` `inoremap :TComment`. Now, `mmmm` works as expected but doesn't seems to work. – Yugal Jindle Nov 12 '13 at 04:13
  • Not possible as I am expecting it to be. – Yugal Jindle Nov 12 '13 at 13:24
  • Thanks for the solution ( Works in Gvim but not in Vim ) – Yugal Jindle Nov 15 '13 at 11:27
  • Even in `Gvim`, I had to use `` instead of `` – Yugal Jindle Nov 16 '13 at 04:00