How to Comment Multiple Lines in Vim Editor

Vim Featured

Vim is one of the most popular and influential command-line text editors. It comes packaged with a lot of functionality and features for any terminal geek. However, Vim can be discouraging to new users – not because it is complex to learn and master, but because it has an unconventional way of doing things. For example, Vim uses shortcuts, modes, and bindings that often take some getting used to, and you need a full article to show you how to exit Vim.

This guide discusses how to get comfortable with Vim by learning quick and easy steps to comment on multiple code blocks.

First, before you can comment on multiple lines in Vim, you need to know how to comment a single line, so let’s focus on that as our starting point.

How to Comment a Single Line in Vim

To comment out a single line in Vim, enter Visual Mode by pressing Ctrl + V. Next, navigate to the line you wish to comment out and press the C key.

Depending on your Vim configuration, this should comment out the selected line. That’s all there is to it – easy, isn’t it?

Now let’s dive a bit deeper.

How to Comment Multiple Lines

To learn how to comment out multiple lines in Vim, we are using a simple python script.

Vim Python

Method 1 – Using Line Numbers

One method to comment out multiple lines in Vim is to use line numbers. Vim allows you to specify the start and finish line and comment them out.

1. Start by opening the file you wish to edit in Vim.

2. Next, press the ESC key to launch Vim Command Mode.

3. Enter the command:

The start is the line number to start from, and the end is the line number on which to end the comment block.

The following screenshot illustrates how to use this command to comment from line 15 to 25.

Vim Comment Line Number

Method 2 – Highlight Block

Suppose you do not know the line numbers to comment out. In that case, you can use the highlight method to highlight the target block and comment it out.

To use the highlight method, jump to the line you want to start commenting on and press the Shift + V keys.

Doing this will highlight the entire line where the cursor is located.

Next, use the up and down arrow keys to select the lines you wish to comment out.

Note: you can also use your mouse scroll wheel to select the lines you wish to comment on.

Vim Highlight Block

Once you have the target block highlighted, press the RETURN key to exit Visual Line.

Next, press the ESC key to enter command mode and enter the command:

Press Enter to run the command and comment on the highlighted lines.

Vim Highlight Block Comment

Method 3 – Regular Expressions

Vim also supports regular expressions to comment out lines containing specific words. The command to comment out specific words is:

In this command, the “keyword” represents the specific words you wish to comment out.

The example below comments out all the lines containing the keyword True:

Vim Comment Regex

Vim automatically highlights the target word before executing the command.

Finally, press the ENTER keyword to execute the command.

Method 4 – Visual Mode

You can also use Vim visual mode to comment out a block of code.

Start by navigating to the line you wish to start commenting out. Next, press the Ctrl + V keys to enable visual mode.

Using the up and down arrow keys, select all the lines you wish to comment out. Vim will insert a highlight at the beginning of each line to indicate the line is selected.

Vim Comment Visual Mode

With the target lines selected, press Shift + I to enter insert mode.

Next, enter the comment symbol for the target file. In this case, we need to insert the pound (#) symbol to comment Python code.

Finally, press the ESC key, and Vim will comment out all the selected lines.

Vim Comment Visual Set

How to Uncomment Multiple Lines

You may also need to uncomment out multiple lines. To do this, you can use the visual mode.

Start by navigating to the line you wish to uncomment, then press Ctrl + V to activate highlight mode.

Using the up and down arrow keys, select all the lines you want to uncomment. Once you have the region selected, press the X key to remove the comments.

Another method is to use a command to remove all lines starting with the comment symbol.

The command for that is:

Vim Ucomment
Vim Uncomment True

Remember to replace comment symbol with the actual comment symbol to be removed.

Wrapping Up

Vim is a great text editor that allows you to work with text files in the terminal easily and comfortably. Although this tutorial shows various ways to comment on multiple lines in Vim editor, it barely scratched the surface of Vim’s capabilities, like turning Vim into a Word processor. If you are an experienced Vim user, don’t forget to check out these Vim tips and tricks, too.


John Wachira

John is a technical writer at MTE, when is not busy writing tech tutorials, he is staring at the screen trying to debug code.

Leave a Comment

Yeah! You've decided to leave a comment. That's fantastic! Check out our comment policy here. Let's have a personal and meaningful conversation.