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";
}
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');
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');
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";
//}
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');*/
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');
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";
*}
*/
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');
Comments the current line from the cursor to the end of line.
Adds comment delimiters to the end of line and goes into insert mode
between
them.
$today = date('l'); /* cursor here */
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');*/
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"; */
/*} */
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