Adding/removing/toggling comments is a common request. As such there are a few approaches
Commenting Plugins
Using a plugin like commentary (which I use), Nerd Commenter, EnhCommentify, tComment, ..., etc make managing comments for all sorts of filetypes much easier as well has giving a consistent interface.
For example commentary use gcc
to toggle an entire line or use gc
followed by a motion to toggle out an entire motion. e.g. gcip
to toggle a paragraph.
Surround.vim
Using Tim Pope's surround.vim plugin can make surrounding with quotes, parens, braces, and tags pairs very simple. It can also be customized so you can add your own "surroundings".
Add the following to ~/.vim/after/ftplugin/markdown.vim
:
let b:surround_{char2nr('#')} = "[//]: # (\r)"
Now you can select your text via v
and then surround via S#
.
For more help with surround see:
:h surround
:h surround-customizing
Change and paste
Example assuming you have text selected via v
:
c[//]: # (<c-r>")<esc>
This works because c
by default will "cut" the text to the default/unnamed register. Using <c-r>
we can recall a register while in insert mode. The register in the example above is "
aka unnamed register.
If you do this often enough it might be worth creating a mapping:
xnoremap gc c[//]: # (<c-r>")<esc>
Thoughts
Personally I would install a commenting plugin because it can be such a common operation and creating new mappings for all the filetypes you come across can get annoying quickly.
I would also recommend looking into surround.vim which feels like a very natural extension of Vim. Install repeat.vim to bring back the glory of .
when using certain plugins.