How to use NERD Commenter for vim -- how to use <Leader> key
VimVim Problem Overview
I found the NERD comment plugin for vim, but I don't understand the documentation. Say I want to comment some code in c/c++ e.g.
code line 1
code line 2
becomes
/*
code line 1
code line 2
*/
I have seen other threads on this, but to be honest I don't know what <leader>
is in the NERD commenter documentation. When I try the accepted answer in this other thread, I end up executing the change command on the selected lines. What is <leader>
and, step-by-step, how do I go about commenting lines with /* */
?
Vim Solutions
Solution 1 - Vim
Try the built-in help, it's excellent. :help <Leader>
brings you to the relevant documentation. It's just an identifier for an unused key (by default </kbd>, but many change it to ,) that is recommended for starting any custom mappings.
NERD_commenter defines the <Leader>cc
mapping, so you'd press (one after the other): </kbd> C C.
Solution 2 - Vim
Your keyboard is the problem
On many non-English keyboard layouts, the backslash \
is only accessible through an AltGr key combination. This makes the default setting of the <Leader>
character rather impractical for these non-English keyboard users.
Moreover, <Leader>
key combinations are subject to a default 1000ms time-out. This can be observed by the disappearance of the <Leader>
key at the right edge of the command line.
So, non-English keyboard users need to be real quick typists for </kbd>cc to work. Failure to do so, results in an undesired cc
line change.
Here is an example of a German keyboard layout with the backslash-bearing key on the top row, third from right:
#Solution
Non-English keyboard users are advised to change the <Leader>
key from the \
character to the more accessible , key. Vim beginners should also consider a slightly longer 1500ms time-out. Here is what needs to be added to ~/.vimrc
let mapleader=","
set timeout timeoutlen=1500
One final caveat; these changes only take effect after completely exiting Vim. Resourcing with :source $MYVIMRC
will not work!
Done that, one can now use ,cc for commenting with NERD Commenter.
Solution 3 - Vim
> how do I go about commenting lines with /* */?
Аs is stated in the documentation:
[count]<leader>cm |NERDComMinimalComment|
Comments the given lines using only one set of multipart delimiters.
So, just use
-
</kbd> c m (if your
<leader>
is </kbd>), or -
, c m (if your
<leader>
is ,)
Solution 4 - Vim
> @IngoKarkatNote: You don't need to hold down the
Completely the wrong advice for the original question!
With NerdCommenter, you DO have to hold down the <leader>
key. Otherwise, as OP found, you just end up activating 'change' mode instead.