0

I tried doing the following mapping in NERDCommenter:

nnoremap <leader>/ <leader>c<space>

However this was not consistently working. I verified the ttimeout was a high value, and also I verified that manually pressing \c worked. The only way I got it to consistently work is by doing:

nnoremap <leader>/ :call NERDComment(1, 'invert')<CR>

Why isn't the original mapping working? I suppose it might be because the nnoremap does not allow <leader> on the rhs to re-map to my leader, \? If so, what would be the appropriate way to fix this, or is actually calling the function the proper way to do this? (Or should I change this from nnoremap to nmap ?)

1

Yes, you need a recursive mapping here, since <Leader>c<Space> is itself a mapping, from NERDCommenter.

This should work:

nmap <Leader>/ <Leader>c<Space>

Plug-ins typically also create <Plug> mappings you can use to map your own keybindings to, that's the case with NERDCommenter.

In that case, you can use:

nmap <Leader>/ <Plug>NERDCommenterToggle

This mapping is also valid in Visual mode, so you might also want to define:

xmap <Leader>/ <Plug>NERDCommenterToggle

Here <Plug> serves as a "virtual" keystroke, that can't really be triggered by a key combination. Effectively just creating a new namespace for keymaps from plug-ins. It is best practice for plug-ins to define their own set of <Plug> macros, to make it easier for users to customize the actual key mappings to trigger specific plug-in features.

The advantage of mapping it to a <Plug> mapping is that it then allows you to undefine or redefine the <Leader>c<Space> mapping and keep your new <Leader>/ mapping working.

| improve this answer | |
  • thanks again. Given the above, is it ever possible that the function name(spaces) collide across plugins? Or is the function usually called something like [PluginName][FunctionName] ? – David542 Jun 22 at 4:20
  • @David542 You mean the mapping <Leader>c<Space> or even prefix <Leader>c? Yeah it's quite possible other plug-ins try to define the same ones by default... Often, using many plug-ins requires managing the mappings so they won't clash. – filbranden Jun 22 at 4:22
  • I mean the <Plug>FunctionNameHere part, in this case <Plug>NERDCommenterToggle. – David542 Jun 22 at 4:54
  • 1
    @David542 Well those names can be fairly long and plug-ins will typically use their name as prefix. You have the same issue with namespace with global functions or command registered by plug-ins. It's a much lesser issue than keymappings, since keymappings typically need to be short, there are only so many keys and Vim itself already takes over most of the keys, so clashes between plug-ins are much more likely there... – filbranden Jun 22 at 5:00
  • For this mapping: nnoremap <leader>/ <Plug>NERDCommenterToggle. It current doesn't work. Do I need to define a <Plug> or something in my vimrc ? ... Hmm...I. guess in that the non-recursive mode is needed also (is that an issue, I always read about to never ever use a non-recursive mapping?) – David542 Jun 22 at 18:15

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.