Home > HOW-TOS, VIM > Vim Plugin: NERD Commenter

Vim Plugin: NERD Commenter

January 21st, 2010

vimHi all, this is the second post about ‘Vim Plugins’. This time I am going to introduce you a new plugin called ‘NERD_commenter.vim’.

NERD commenter is a plugin for Vim that allows for easy commenting for many filetypes. The NERD commenter provides many different commenting operations and styles which are invoked via key mappings and a menu. NERD_commenter can handle single line, multi line, partial line commenting. This is a must have plugin if you’re programming in VIM.

How to use NERD_commenter.vim

This is the default block of code we will be working with:

$queue  = '/queue/foo';
$msg    = 'bar';
 
/* connection */
try {
     $stomp = new Stomp('tcp://localhost:61613');
} catch(StompException $e) {
     die('Connection failed: ' . $e->getMessage());
}
 
/* send a message to the queue 'foo' */
$stomp->send($queue, $msg);

,cc |NERDComComment|

Comments out the current line or text selected in visual mode.

//$queue  = '/queue/foo';
//$msg    = 'bar';
 
// connection
try {
    $stomp = new Stomp('tcp://localhost:61613');
} catch(StompException $e) {
    die('Connection failed: ' . $e->getMessage());
}
 
/* send a message to the queue 'foo' */
$stomp->send($queue, $msg);

,cn |NERDComNestedComment|

Same as |NERDComComment| but forces nesting.

//$queue  = '/queue/foo';
//$msg    = 'bar';
 
//// connection
try {
    $stomp = new Stomp('tcp://localhost:61613');
} catch(StompException $e) {
    die('Connection failed: ' . $e->getMessage());
}
 
/* send a message to the queue 'foo' */
$stomp->send($queue, $msg);

,c<space>|NERDComToggleComment|

Toggles the comment state of the selected line(s). If the topmost selected
line is commented, all selected lines are uncommented and vice versa.

create connection
try {
    $stomp = new Stomp('tcp://localhost:61613');
} catch(StompException $e) {
    die('Connection failed: ' . $e->getMessage());
}

,cm |NERDComMinimalComment|

Comments the given lines using only one set of multipart delimiters if
possible.

// create connection
/*try {
    $stomp = new Stomp('tcp://localhost:61613');
} catch(StompException $e) {
    die('Connection failed: ' . $e->getMessage());
}*/

,ci |NERDComInvertComment|

Toggles the comment state of the selected line(s) individually. Each selected
line that is commented is uncommented and vice versa.

create connection
//try {
    //$stomp = new Stomp('tcp://localhost:61613');
//} catch(StompException $e) {
    //die('Connection failed: ' . $e->getMessage());
//}

,cs |NERDComSexyComment|

Comments out the selected lines “sexily”

// create connection
/*
*try {
*    $stomp = new Stomp('tcp://localhost:61613');
*} catch(StompException $e) {
*    die('Connection failed: ' . $e->getMessage());
*}
*/

,cy |NERDComYankComment|

Same as |NERDComComment| except that the commented line(s) are yanked
before commenting.

// create connection
//try {
    //$stomp = new Stomp('tcp://localhost:61613');
//} catch(StompException $e) {
    //die('Connection failed: ' . $e->getMessage());
//}

pasted the yanked lines you will get

try {
    $stomp = new Stomp('tcp://localhost:61613');
} catch(StompException $e) {
    die('Connection failed: ' . $e->getMessage());
}

,c$ |NERDComEOLComment|

Comments the current line from the cursor to the end of line.

try {
    $stomp = /*new Stomp('tcp://localhost:61613');*/
} catch(StompException $e) {

,cA |NERDComAppendComment|

Adds comment delimiters to the end of line and goes into insert mode between
them.

try {
    $stomp = new Stomp('tcp://localhost:61613'); // cursor here
} catch(StompException $e) {

,cI |NERDComPrependComment|

Adds comment delimiters to the start of line and goes into insert mode between
them.

try {
    // cursor here   $stomp = new Stomp('tcp://localhost:61613');
} catch(StompException $e) {

,ca |NERDComAltDelim|

Switches to the alternative set of delimiters.

switches between // and /* */ for example. Here’s ,cc after applying this

/*$queue  = '/queue/foo';*/
/*$msg    = 'bar';*/
 
// create connection
try {
    $stomp = new Stomp('tcp://localhost:61613');
} catch(StompException $e) {
die('Connection failed: ' . $e->getMessage());
}

,cl  |NERDComAlignedComment|

Same as |NERDComComment| except that the delimiters are aligned down the side.

// create connection
/*try {*/
/*    $stomp = new Stomp('tcp://localhost:61613');*/
/*} catch(StompException $e) {*/
/*    die('Connection failed: ' . $e->getMessage());*/
/*}*/

,cr |NERDComAlignedComment|

Same as |NERDComComment| except that the delimiters are aligned down the right side.

// create connection
// create connection
/*try {                                             */
    /*$stomp = new Stomp('tcp://localhost:61613');  */
/*} catch(StompException $e) {                      */
    /*die('Connection failed: ' . $e->getMessage());*/
/*}*/

,cb |NERDComAlignedComment|

Same as |NERDComComment| except that the delimiters are aligned down both sides.

// create connection
/*try {                                             */
/*    $stomp = new Stomp('tcp://localhost:61613');  */
/*} catch(StompException $e) {                      */
/*    die('Connection failed: ' . $e->getMessage());*/
/*}

,cu |NERDComUncommentLine|

Uncomments the selected line(s).

send a message to the queue 'foo'
$stomp->send($queue, $msg);

Installing NERD_commenter.vim

To get the latest version:

* 1. Download NERD_commenter.zip.
* 2. Extract NERD_commenter.zip to ~/.vim (on Unix/Linux) or ~\vimfiles (on Windows).
* 3. Run :helptags ~/.vim/doc (on Unix/Linux) or :helptags ~/vimfiles/doc (on Windows) to rebuild the tags file (so that you can read :help NERD_commenter.)
* 4. Restart Vim.

Categories: HOW-TOS, VIM Tags:
  1. Jithin
    August 14th, 2010 at 11:28 | #1

    Hi, Thanks for this useful post. I have one doubt though,
    When I am using ,cc in visual mode. The first line is always commented out using /*

    Like this,

    /* const IMG_SIZE_SMALL = ’32×32′;*/
    //const IMG_SIZE_MEDIUM = ’64×64′;
    //const IMG_SIZE_LARGE = ’192×192′;

    How can I make all the lines use // to comment ?
    Thanks in advance.

    • August 16th, 2010 at 12:14 | #2

      @Jithin

      use ,ca to switches to the alternative set of delimiters.

  2. Jithin
    August 18th, 2010 at 09:31 | #3

    I tried using ,ca

    It enabled me to change the delimiters to /* where each line was commented with /*
    When using // as the delimiter the first line is commented like I posted.

  1. No trackbacks yet.
Comments are closed.