I have a mapping in my vimrc that downwardly comments out regions of c code:
nmap comc :normal! I//
Since the \'normal\' ex command
While it's commendable to go as far as possible without any plugins, sometimes they're just the best option. What will you do when you start working in a language that has comments with #
or (*
...*)
? Add new mappings for these comment characters?
I recommend commentary.vim which does filetype-aware commenting.
The default commenting operator in commentary.vim is gc
. You can combine it with motions, and use it in Visual mode too.
Your use cases:
Comment downwards N lines (say, 3): :.,.+3normal gcc
, or gc3j
or 4gcc
.
Comment upwards 5 lines: :.,.-5normal gcc
, or simply gc5k
.
Comment until int main
: :.,/int main/-1normal gcc
, or simply gc/int main
followed by Enter.