Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use NERD Commenter for vim -- how to use <Leader> key

Tags:

vim

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

like image 959
Matthew Turner Avatar asked Jan 09 '13 00:01

Matthew Turner


People also ask

How do I map the leader key in Vim?

Use the mapleader variable in your . Now use the following to set the leader key. In the above example, I mapped the leader to , . This is much easier to access than \ , but you can map the leader to whatever key you'd like! For this change to take effect, you'll have to re-launch Vim.

How do you use nerd commenter?

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

What is leader key in Vim?

The "Leader key" is a way of extending the power of VIM's shortcuts by using sequences of keys to perform a command. The default leader key is backslash. Therefore, if you have a map of <Leader>Q, you can perform that action by typing \Q.

What is NerdCommenter?

GitHub - preservim/nerdcommenter: Vim plugin for intensely nerdy commenting powers.


2 Answers

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.

like image 52
Ingo Karkat Avatar answered Sep 16 '22 17:09

Ingo Karkat


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.

like image 32
Serge Stroobandt Avatar answered Sep 18 '22 17:09

Serge Stroobandt