Skip to main content
Vim Text Editor:

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 #

  1. Position the cursor at the first column of the first line you want to comment.
  2. Press Ctrl+v to enter visual block mode.
  3. Use the j/k keys to extend the selection to the last (included) line of the block.
  4. Press Shift+i to begin inserting text.
  5. Type #.
  6. 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 #

  1. Position the cursor at the first # character of the first line in the block.
  2. Press Ctrl+v to enter visual block mode.
  3. Use the j/k keys to extend the selection to the last (included) line of the block.
  4. Press x to 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:

Author

Jonas Jared Jacek • J15k

Jonas Jared Jacek (J15k)

Jonas works as project manager, web designer, and web developer since 2001. On top of that, he is a Linux system administrator with a broad interest in things related to programming, architecture, and design. See: https://www.j15k.com/

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.

All Topics

Random Quote

“Did we imagine that people would do bad things on the web? Absolutely.”

Tim Berners-Lee English computer scientist, inventor of the World Wide WebTIME, - IT quotes