70

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 /* */?

Community
  • 1
  • 1
Matthew Turner
  • 3,564
  • 2
  • 20
  • 21
  • 2
    Also check this for more info on the leader key(s): http://learnvimscriptthehardway.stevelosh.com/chapters/06.html – greduan Jan 09 '13 at 01:03
  • 1
    @SantoshKumar Rolled back: With your edit it becomes a duplicate of http://stackoverflow.com/q/1764263! also note the last bit "and, step-by-step, how do I go about commenting lines with `/* */`?" – glts Aug 29 '13 at 16:56

4 Answers4

72

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 \, 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): \ C C.

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • 5
    Note: You don't need to hold down the `` key, just press it like any other key, and release it. The only modifier keys that can be pressed together with others are Shift, Ctrl, and Alt. – Ingo Karkat Jan 09 '13 at 01:30
31

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: German keyboard layout

#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.

Serge Stroobandt
  • 28,495
  • 9
  • 107
  • 102
7

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

  • \ c m (if your <leader> is \), or
  • , c m (if your <leader> is ,)
hooke
  • 736
  • 8
  • 20
1

@IngoKarkatNote: You don't need to hold down the key, just press it like any other key, and release it.

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.

stíobhart
  • 27
  • 3