0

I have downloaded the vim-commentary plugin for commenting in vim. I was trying to remap the default shortcuts for the plugin so as to use ctrl+/ for commenting and remove commenting (default is gc). I was also trying to create a map so that I can comment while being in insert mode.

In my vimrc I wrote this:

vnoremap <C-/> gc
inoremap <C-/> <Esc>VgcA

The first one isn't doing anything. While the second one stops working after entering the Visual mode. What should I do to correct these remaps?

2
  • I think you need the recursive version of map here: When you type <c-/> you want it to execute what vim-commentary mapped to gc not the built-in gc feature.
    – statox
    Jul 8 at 12:24
  • @statox Thanks. It worked
    – Cs_rlm
    Jul 8 at 14:38
2

The commentary.vim plugin file uses the <Plug> notation for mapping.

  xmap gc  <Plug>Commentary
  nmap gc  <Plug>Commentary
  omap gc  <Plug>Commentary
  nmap gcc <Plug>CommentaryLine

The xmap mode is like vmap, but excludes Select mode (which is rarely used).

<Plug>Commentary and <Plug>CommentaryLine are easy-to-understand labels which are mapped elsewhere to less obvious Vim commmands. As a result, you must use the recursive forms of the map commands.

<Plug> is character which is impossible to type, so it acts as a placeholder in recursive maps that shouldn't interfere with anything else.

You should be able to do your visual map like this:

xmap <c-/> <Plug>Commentary

Your insert mode plugin could be:

imap <c-/> <Esc><Plug>CommentaryLineA

Also, be aware that <c-/> is not a standard terminal control code. These mappings worked fine in gvim but not with Vim in my konsole terminal.

1
  • thanks for the comment. I edited my answer with the correct info. Jul 8 at 17:59
1

You need the recursive version of map here: When you type <c-/> you want it to execute what vim-commentary mapped to gc not the built-in gc feature.

So your mappings should be:

vmap <C-/> gc
imap <C-/> <Esc>VgcA

I wrote multiple times on this website that you should always use the non recursive version of map unless you know what you are doing, this is the exception which proves the rule :)

Also you probably want to use :h i_CTRL-O in your insert mode mapping instead of using <Esc> and A. Probably something like this (not tested):

imap <C-/> <C-o>gc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.