Vim Plugins: NERD Commenter

The NERD Commenter is an indispensable tool when programming in VIM. It understands like a zillion different file types and properly comments each. It can handle single line, multi line, partial line commenting as well as nesting. If you’re programming in VIM you really should be using it.

It is simple enough to use. Most commands are mapped to ,c[character]. The command you are probably going to use the most is ,c<space> which intelligently toggles a comment on or off.

You can find the plugin at The NERD Commenter.

Here are the instructions on usage, courtesy of the documentation file. With some commentary and examples using php, courtesy of me.

Here is the default block of code we will be working with.

// Change to the location of the folder containing the images
$image_folder = "images/days";

// You do not need to edit below this line
$today = date('l');

if (file_exists($image_folder."/".$today.".gif")) {
  echo "<img src="image_folder/".$today.".gif">";
}else {
  echo "No image was found for $today";
}

,cc |NERDComComment|

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

  //// Change to the location of the folder containing the images
  //$image_folder = "images/days";

  //// You do not need to edit below this line
  ///$today = date('l');

,cn |NERDComNestedComment|

Same as |NERDComComment| but forces nesting.

  //// Change to the location of the folder containing the images
  //$image_folder = "images/days";

  //// You do not need to edit below this line
  ///$today = date('l');

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

  Change to the location of the folder containing the images
  $image_folder = "images/days";

  You do not need to edit below this line
  $today = date('l');

Running again on the second half

  //if (file_exists($image_folder."/".$today.".gif")) {
      //echo "<img src="image_folder/".$today.".gif">";
  //} else {
      //echo "No image was found for $today";
  //}

,cm |NERDComMinimalComment|

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

  /*// Change to the location of the folder containing the images
  $image_folder = "images/days";

  // You do not need to edit below this line
  $today = date('l');*/

,ci |NERDComInvertComment|

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

  Change to the location of the folder containing the images
  //$image_folder = "images/days";

  You do not need to edit below this line
  //$today = date('l');

,cs |NERDComSexyComment|

Comments out the selected lines “sexily”

  /*
   *if (file_exists($image_folder."/".$today.".gif")) {
   *    echo "<img src="image_folder/".$today.".gif">";
   *} else {
   *    echo "No image was found for $today";
   *}
   */

,cy |NERDComYankComment|

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

  //// Change to the location of the folder containing the images
  //$image_folder = "images/days";

  //// You do not need to edit below this line
  //$today = date('l');

,c$ |NERDComEOLComment|

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

  $today = /*date('l');*/

,cA |NERDComAppendComment|

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

  $today = date('l'); /* cursor here */

,cI |NERDComPrependComment|

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

  /* cursor here */$today = date('l');

,ca |NERDComAltDelim|

Switches to the alternative set of delimiters.

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

  /*// Change to the location of the folder containing the images*/
  /*$image_folder = "images/days";*/

  /*// You do not need to edit below this line*/
  /*$today = date('l');*/

,cl OR ,cr OR ,cb |NERDComAlignedComment|

Same as |NERDComComment| except that the delimiters are aligned down the
left side (,cl), the right side (,cr) or both sides
(,cb).

,cl

  /*if (file_exists($image_folder."/".$today.".gif")) {*/
  /*    echo "<img src="image_folder/".$today.".gif">";*/
  /*} else {*/
  /*    echo "No image was found for $today";*/
  /*}*/

,cr

  /*if (file_exists($image_folder."/".$today.".gif")) {    */
      /*echo "<img src="image_folder/".$today.".gif">";*/
  /*} else {                                               */
      /*echo "No image was found for $today";              */
  /*}                                                      */

,cb

  /*if (file_exists($image_folder."/".$today.".gif")) {    */
  /*    echo "<img src="image_folder/".$today.".gif">";*/
  /*} else {                                               */
  /*    echo "No image was found for $today";              */
  /*}                                                      */

,cu |NERDComUncommentLine|

Uncomments any commented lines in the block (only one level if nested)

  Change to the location of the folder containing the images
  $image_folder = "images/days";

  You do not need to edit below this line
  $today = date('l');

Related articles