6

I am using https://github.com/scrooloose/nerdcommenter/ plugin to comment the code. It's working fine, but I would like to do a modification.

Currently these are the default mappings:

  • <leader>cc comments the line
  • <leader>ci inverts the comment
  • <leader>cu undo last comment/uncomment action

I am looking to map <leader>cc to the action of invert comment i.e. <leader>ci. It's not a nerdcommenter question, but a question of vim mappings itself, hence asking here.

I tried

nnoremap <leader>cc <leader>ci

and also

nmap <leader>cc <leader>ci

But there doesn't seems to be any effect of these mapping. On pressing <leader>cc, I am seeing original action of it not the one I am trying to define.

EDIT: FYI, answer to this question https://vi.stackexchange.com/a/763/8092 doesn't help either. i.e. to define custom mappings in .vim/after/plugin/somefile.vim

6
  • 1
    Does it work to put this in your .vim/after/plugin/somefile.vim: call s:CreateMaps('nx', 'Invert', 'Invert', 'cc')
    – DJMcMayhem
    Commented Jun 20, 2016 at 2:23
  • 5
    It is a nerdcommenter question, since the answer can be found in the manual: nmap <Leader>cc <Plug>NERDCommenterInvert. Commented Jun 20, 2016 at 3:46
  • 4
    No, it's a plugin-specific question. Remove any reference to NERDCommenter and rework the title to make it a generic question.
    – romainl
    Commented Jun 20, 2016 at 6:23
  • 1
    nmap <leader>cc <leader>ci works here when I source my vimrc file manually (but not when vim opens). What I suspect is that your mapping gets overridden by the plugin.
    – nobe4
    Commented Jun 20, 2016 at 8:04
  • 2
    You can use verbose nmap <leader>cc to see where the mapping was defined. Commented Jun 20, 2016 at 8:33

2 Answers 2

1

Like many other plug-ins, NERDCommenter creates internal mappings using the <plug> virtual key as a prefix, that you can use to map your own key-bindings to.

See :help NERDCommenterMappings:

To change a mapping just map another key combo to the internal <plug> mapping.

For example, to remap the NERDCommenterComment mapping to ,omg you would put this line in your vimrc:

map ,omg NERDCommenterComment

This will stop the corresponding default mappings from being created.

See the help for the mapping in question to see which <plug> mapping to map to.

So:

  • You can find which <plug> mapping to use by looking it up at the help for the original mapping.

  • NERDCommenter will not add its default mapping if it finds another mapping pointing to the <plug> mapping already, so you can use that to prevent it from creating its own mappings (such as <leader>cc), potentially overwriting yours.

Note that these mappings need to be recursive, since the <plug> ones are mappings themselves.

In your case, I believe this is what you want:

nmap <leader>cc <plug>NERDCommenterInvert 
xmap <leader>cc <plug>NERDCommenterInvert 

This mapping is relevant in Normal and Visual modes, therefore remapping it with nmap and xmap here.

Note also that since you're using a default mapping of NERDCommenter, you'll need to remap that <plug> mapping to some other key, in order to prevent NERDCommenter from overwriting your mapping. For example, to use <leader>ci for the Comment mapping (effectively swapping the two keys):

nmap <leader>ci <plug>NERDCommenterComment
xmap <leader>ci <plug>NERDCommenterComment

But note that you don't need to use <leader>ci here, you can use any other key combination.


If you would like to prevent NERDCommenter from adding all its mappings, you can use this in your vimrc:

let g:NERDCreateDefaultMappings = 0

See :help 'NERDCreateDefaultMappings'.

Unfortunately it seems there's no granular way to prevent it from adding one mapping only (if you wanted to leave one or a few functions without key-bindings), it's all or nothing.

0

Seems that plugins have "actions" that you can call with :call.

For example to map Alt+; to nerdcomment toggle:

nnoremap <M-;> :call NERDComment(0,"toggle")<CR>
vnoremap <M-;> :call NERDComment(0,"toggle")<CR>

Related answer: https://stackoverflow.com/a/30831547/1663462

1
  • 1
    Welcome to this site Chris! Just a clarification about vocabulary: something that you can call with call is a "function", see :h user-functions. And of course they exists only with the plugin author decides to expose them to the user :)
    – statox
    Commented Mar 2, 2020 at 16:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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