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 /* */
?
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.
@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.
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.
GitHub - preservim/nerdcommenter: Vim plugin for intensely nerdy commenting powers.
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.
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.
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