I'm trying to set up the NERDComment plugin in vim, but I'm having some trouble with the keys. I'd like to set the basic toggle functionality (comment a line if it's uncommented, uncomment if it's commented) to be c. The problem is that I've remapped the Leader to be ,
, which is the same key that NERD wants for all of it's hotkeys. Anyone have any idea as to how to set this up?
Just call NERDComment function in your mapping. For example, my mapping to comment the current line:
inoremap ,c <C-o>:call NERDComment(0,"toggle")<C-m>
Here's a breakdown of how this vim remap works.
The i
in inoremap means that the remap only applies in insert mode.
The noremap
means that the remap can't be overridden later in your .vimrc file by accident, or by a plugin.
The ,c
is the key combination that triggers the key map.
The <C-o>
temporarily takes you out of insert mode for one command, so the next section of the remap can call the NERDComment function.
The :call NERDComment(0,"toggle")
is the NERDComment function being called.
Then <C-m>
is another way of saying carriage return, which executes the command.
If you want the comment shortcut to work in normal mode and visual mode, but not in insert mode where it might do something weird when you try to type a comma, you can use the following remaps:
nnoremap ,c :call NERDComment(0,"toggle")<CR>
vnoremap ,c :call NERDComment(0,"toggle")<CR>
documented method of remapping key is located here: remapping documentation
reference
map <leader>d <Plug>NERDCommenterToggle
"silently rejects remap will not work
nnoremap <leader>d <Plug>NERDCommenterToggle
I fell into the pitfall of attempting to use "nnoremap" to remap on my first attempt resulting in unresponsive mapping. You must use "map", "nmap", etc to properly remap the function
:map <C-z> <plug>NERDCommenterToggle
Maps 'toggle comments' to ctrl+z
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With