Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom keys with NERDComment plugin and remapped Leader?

Tags:

vim

vi

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?

like image 837
Paul Wicks Avatar asked Apr 13 '10 03:04

Paul Wicks


4 Answers

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.

like image 54
ZyX Avatar answered Oct 05 '22 14:10

ZyX


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>
like image 22
tobuslieven Avatar answered Oct 05 '22 13:10

tobuslieven


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

like image 23
Aundre Avatar answered Oct 05 '22 13:10

Aundre


:map <C-z> <plug>NERDCommenterToggle

Maps 'toggle comments' to ctrl+z

like image 31
featherweight Avatar answered Oct 05 '22 12:10

featherweight