11

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.

Roger
  • 1,843
  • 3
  • 20
  • 22

4 Answers4

12

I'd do (in visual mode) :78v172G to select and highlight lines 78 to 172, then just ,c<space>.

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
  • Love this answer - it works for both commenting and uncommenting text in visual mode – arcseldon Oct 24 '17 at 12:35
  • I also use this method only but even this isn't very efficient. It would have been nice if this plugin supports syntax, as it can make commenting really fast. So what i mean is like this 5c-/j to toggle comments. I have mapped c-/ to toggle comments. I really miss this feature. – Rohit Aug 22 '18 at 17:04
6

It doesn't appear that NERDCommenter supports ranges, so Alex's answer would be correct, but he has a typo - it should be 78GV172G to select (note - no preceeding :), followed by your comment function ( in my case \cc).

Ivar
  • 5,102
  • 2
  • 31
  • 43
  • I do as you said that the lines are well selected. But how can I use nerdCommenter comment them out. What should I input. Document says `[count]cc |NERDComComment|`, but I don't know what does each part mean. – hakunami Nov 02 '14 at 02:16
3

Enter Visual mode <ctrl> + V, using your arrow keys, highlight the lines you want then /c<space> (perhaps you have another key binded for commenting).

1

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

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985