2

I installed NERDCommenter plugin and its default bindings unfortunately conflict with my own. For instance I use <Leader>-c and <Leader>-v to emulate CTRL-c and CTRL-v. And since IDEs usually use CTRL-/ to toggle comments I'd like to preserve this behavior.

I tried the following in my vimrc:

let g:NERDCommenterToggle = "<C-/>"

But when reloading the vimrc the old mapping <Leader>c<Space> is still in effect; the <C-/> mapping has no effect.

Is there a way to cancel all the default bindings introduced by NERDCommenter and bind only g:NERDCommenterToggle to C-/?

  • 1
    notice that c-/ is like ESC and can make the computer beep. – eyal karni Dec 23 '19 at 20:46
2

I'm just going by the help doc in github but it appears that 'NERDCreateDefaultMappings' does exactly what you want for the first part, ie. toggle on/off all mappings (bindings)...

If set to 0, none of the default mappings will be created.

Then you can manually bind to your heart's content. The help for NERDCommenterMappings has a bit of advice for that:

To change a mapping just map another key combo to the internal mapping. For example, to remap the |NERDCommenterComment| mapping to ",omg" you would put this line in your vimrc:

map ,omg <plug>NERDCommenterComment

Update: If you need more help let's look at how the default for NERDCommenterToggle is constructed. It should be clear now that we don't override like a normal variable (:let x = y) but rather we need to define a mapping. The relevant bits responsible for defining the default mapping...

call s:CreateMaps('nx', 'Toggle', 'Toggle', 'c<space>')

Signature of CreateMaps:

function! s:CreateMaps(modes, target, desc, combo)

Actual map definition where modes is split and iterated over such that mode has each value, n and x, in turn...

execute mode . 'map <leader>' . a:combo . ' ' . plug

Where plug is

let plug = '<plug>NERDCommenter' . a:target

So that translates to

nmap <leader>c<space> <plug>NERDCommenterToggle
xmap <leader>c<space> <plug>NERDCommenterToggle

So you want

nmap <C-/> <plug>NERDCommenterToggle
xmap <C-/> <plug>NERDCommenterToggle

You may not need the xmap version.

(Don't forget you need to set NERDCreateDefaultMappings to 0, too.)

| improve this answer | |

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.