Comment and uncomment multiple lines in Vim
Summary
This article explains two methods to comment and uncomment multiple lines in Vim: using visual blocks and line ranges. Command examples for commenting or uncommenting specified lines with and without visual selection.
Introduction #
When working in Vim, you often need to comment or uncomment multiple lines, especially when editing configuration files, code, or scripts. Vim provides powerful ways to handle this task using either
- visual block mode or
- line range substitution.
Commenting and uncommenting using visual blocks #
You can use Vim’s visual block mode to prepend or remove characters from a column of text. This method is useful for commenting or uncommenting consecutive lines with a specific character, such as a hash (#).
Commenting blocks #
- Position the cursor at the first column of the first line you want to comment.
- Press
Ctrl+vto enter visual block mode. - Use the
j/kkeys to extend the selection to the last (included) line of the block. - Press
Shift+ito begin inserting text. - Type
#. - Press
ESC.
The comment character will only appear on all selected lines after you press ESC. Until then, Vim only displays the typed character on the top line.
Uncommenting blocks #
- Position the cursor at the first
#character of the first line in the block. - Press
Ctrl+vto enter visual block mode. - Use the
j/kkeys to extend the selection to the last (included) line of the block. - Press
xto delete the selected character on each line.
Commenting and uncommenting using line ranges #
If you know the line numbers, you can use substitution commands in command-line mode to comment or uncomment several lines.
Commenting ranges #
To add a hash (#) at the beginning of lines 12 to 16, use the following command:
:12,16s/^/#/
This adds a # character to the start of each line in the specified range.
To comment with two slashes (//) instead of a #, you need to use the following code, for example::12,16s/^/\/\/.
Uncommenting ranges #
To remove the hash character (#) from the beginning of lines 12 to 16, use:
:12,16s/^#//
This removes the # only if it is the first character in the line. No other text will be affected.
To uncomment lines beginning with two slashes (//), you need to use the following code, for example::12,16s/^\/\//.
FAQ's #
Most common questions and brief, easy-to-understand answers on the topic:
How do I enter visual block mode in Vim?
Press Ctrl+v to enter visual block mode in Vim.
What is the shortcut to add a comment character in visual mode?
After selecting the block, press Shift+i, type the comment character (e.g., #), and press ESC.
Can I comment lines in Vim without entering visual mode?
Yes, you can use line range substitutions like :10,15s/^/#/ to comment lines directly.
Do I need a plugin to comment multiple lines in Vim?
No, Vim supports commenting and uncommenting multiple lines using built-in commands and visual block mode.
What happens if you forget to press Esc after commenting in visual block mode?
The changes will not apply to all selected lines until you press Esc.
Further readings #
Sources and recommended, further resources on the topic:
License
Comment and uncomment multiple lines in Vim by Jonas Jared Jacek is licensed under CC BY-SA 4.0.
This license requires that reusers give credit to the creator. It allows reusers to distribute, remix, adapt, and build upon the material in any medium or format, for noncommercial purposes only. To give credit, provide a link back to the original source, the author, and the license e.g. like this:
<p xmlns:cc="http://creativecommons.org/ns#" xmlns:dct="http://purl.org/dc/terms/"><a property="dct:title" rel="cc:attributionURL" href="https://www.ditig.com/comment-uncomment-multiple-lines-in-vim">Comment and uncomment multiple lines in Vim</a> by <a rel="cc:attributionURL dct:creator" property="cc:attributionName" href="https://www.j15k.com/">Jonas Jared Jacek</a> is licensed under <a href="https://creativecommons.org/licenses/by-sa/4.0/" target="_blank" rel="license noopener noreferrer">CC BY-SA 4.0</a>.</p>For more information see the Ditig legal page.