Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NERD commenter : How to comment out a range

Tags:

vim

I am using NERD commenter

Let's say that I want to comment out lines from 78 to 172. This is what I do. I calculate the difference. That is 94. put my cursor on line 78 and then I do: 94 , c space

In this way I comment 94 lines starting from line number 78.

I don't like the calculation that I need to do. I was hoping to pass a range something like

78,172 to comment the code from line 78 to 172. Am I missing something.

Looking at the popularity of this plugin, it seems there must be a better way to comment and uncomment.

like image 925
Roger Avatar asked Sep 03 '09 18:09

Roger


People also ask

How do you comment out code all at once?

The leading characters // are added to the beginning of each line when commenting one or more lines of code. You can also block comment multiple lines of code using the characters /* */ .

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.


1 Answers

Sexy C comments mode

Ranges of lines just work by default with visual selection + <leader>cc, tested on 2.5.2, as mentioned at: https://stackoverflow.com/a/1375070/895245

But above all, I want to highlight in this answer the "sexy mode" comments with <leader>cs, which generates nice C multiline comments.

For example, if you start with:

This is a c style sexy comment
So there!

then <leader>cs from visual selection transforms it into:

/* This is a c style sexy comment
 * So there! */

And you can also switch to non-compact mode by adding to your .vimrc:

let g:NERDCompactSexyComs = 0

which works as follows:

------------------------------------------------------------------------------

                                                         *'NERDCompactSexyComs'*
Values: 0 or 1.
Default 0.

Some people may want their sexy comments to be like this: >
    /* Hi There!
     * This is a sexy comment
     * in c */
<
As opposed to like this: >
    /*
     * Hi There!
     * This is a sexy comment
     * in c
     */

Another related format which might be of interest is the "Minimal comment map" accessible with <leader>cm and which produces:

/* Hi There!
   This is a sexy comment
   in C */

I wasn't able to find my preferred style unfortunately:

/* Hi There!
 * This is a sexy comment
 * in c
 */

so I opened: https://github.com/scrooloose/nerdcommenter/issues/379