Vim is an excellent command-line text editor, especially if you are comfortable with its shortcuts, modes, and bindings. However, when working with it, we can encounter instances where we need to comment on code blocks while editing code and configuration files.

This article describes quick and easy ways to comment and uncomment out multiple lines of code in Vim editor. Learning how to do this will remove the need to go down each line and comment out each line.

How to Comment Multiple Lines – Vim

For this guide, we will use a simple python script called simple.py

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-254.png" data-lazy- height="559" src="data:image/svg xml,” width=”1089″>

Method #1 – Line Numbers

The simplest method to comment out multiple lines is to use line numbers. First, open your file in Vim and press ESC to enter command mode.

Enter

:[start line],[end line]s/^/#

To demonstrate, let us comment out from lines 10 to 15./

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-255.png" data-lazy- height="850" src="data:image/svg xml,” width=”787″>

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-256.png" data-lazy- height="238" src="data:image/svg xml,” width=”782″>

If you do not want to use line numbers in the command, use the highlight method.

To do this, move to the line you want to start commenting out and press SHIFT V. Doing this will highlight the entire line. Then, use the up and down arrow keys to highlight the other lines.

Once you have the lines to comment out highlighted, press enter; the Highlighted code should look something like this:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-257.png" data-lazy- height="434" src="data:image/svg xml,” width=”985″>

Press ESC to enter command mode and use the command as:

Once completed, you should have all the lines you highlighted commented as:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-258.png" data-lazy- height="427" src="data:image/svg xml,” width=”989″>

Method # 2 – Visual Mode

The other method you can use to comment out multiple lines is to use Visual Mode.

To do this, press ESC and navigate to the lines you want to comment out.

Press CTRL V to enable Visual Mode.

Using the up and down arrow key, highlight the lines you wish to comment out.

Once you have the lines selected, press the SHIFT I keys to enter insert mode.

Enter your command symbol, for example, # sign, and press the ESC key. Vim will comment out all the highlighted lines.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-259.png" data-lazy- height="413" src="data:image/svg xml,” width=”974″>

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-260.png" data-lazy- height="412" src="data:image/svg xml,” width=”900″>

# 3 – Regular Expression

One of our favorite methods of commenting lines of code in Vim is regular expressions. For example, using regex is a powerful method that comments outlines that contain a specific word.

For example, to comment lines that contain the word def, use the command:

Press enter to comment out the lines:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-261.png" data-lazy- height="398" src="data:image/svg xml,” width=”878″>

How to Uncomment Multiple Lines – Vim

The reverse is true; once you comment on outlines, you need to uncomment them. To uncomment multiple lines, use the following method.

Enter the following to uncomment lines starting with a # sign

Another method is to use Visual mode.

Navigate to the line you wish to comment out and press CTRL V to enter Visual mode.

Use the up and down arrow keys to highlight the lines you wish to uncomment. Once selected, press x to remove the comments.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-262.png" data-lazy- height="526" src="data:image/svg xml,” width=”1069″>

Once you press x, it automatically uncomments the lines.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/word-image-263.png" data-lazy- height="505" src="data:image/svg xml,” width=”899″>

To Conclude

In short, using Vim shortcuts should improve your workflow and help improve your efficiency. Please search our site for more Vim tutorials to improve your Vim skills.

Thank you for reading!

About the author

<img alt="" data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/07/echo/john-150×150.png60f5ade06d672.jpg" height="112" src="data:image/svg xml,” width=”112″>

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list