NERDCommenter is an awesome plugin for commenting code. It works primarily in the normal mode and is aimed at more efficient coding and debugging.
What it does is allow you to comment out a line in any file using the
same keybindings. Let’s say you want to have the shortcut to comment
out a line as
// because that is easy to type. Now for a block of
code, you go to a particular line and press
//, the line gets commented.
// again, the line gets uncommented into the
This adds a very powerful tool to your workflow allowing you comment
out as well as comment in lines very fast. You could also type something
5// to comment out 5 lines.
This is what would happen:
For vim-plug, add the following line to your
README.md as well as
:help commenter for a complete description
of features. If you are wondering what is
<leader>, it is a special
in vim with default value
\. You can change it to whatever you like. Now to get
the setting we discussed before, i.e., fast commenting out and in using
In the README,
you will find that
<leader>c<space> has the behaviour we want
of toggling comments. So in order to have that same command for
//, you will
need to map
// to it:
And now open up your favourite file and try this out. It is very efficient and something
very valuable to my current workflow. I have found myself pressing
// on someone else’s
machine to comment code ( which usually works in C :P ) many times.
In order to understand, why we used
you might want to Learn Vimscript.
Its not compulsory, but it allows you to modify vim to your settings more easily.