Somtimes you want to comment out multiple lines in Vim. There’s an easy way to do this out of the box without any
rc magic or plugin.
- Navigate with the cursor to either the first or last line you want to comment out.
vto select a new visual block.
- Move your cursor down (or up respectively) to the last (or first) line you want to comment out. You just formed a visual block of your comment.
Ito insert text before all selected lines.
- Type your comment code (for example
Escape. Your comment code will be inserted before every line of the visual block.
To uncomment, do exactly the same, but instead of executing step 5 apply a regular expression. The regular expression should look something like this:
:s/^#/. This will replace every
# at the beginning of the line with nothing. If your comment code has only one character, you can also press
d to delete the first character of every line.
Now tell me how to do this in Emacs. Christine graciously commented on how to do this in Emacs. Looks similar, except that Emacs seems to be a bit smarter about what comment code to use.
Shameless plug: If this post was useful to you, please consider buying yourself something from one of my Amazon stores: US store, UK store, FR store, DE store, CA store. If you're not into Amazon, why not donate something to GNOME, Mozilla or Wikipedia? Thank you!