Today, we will be discussing how to comment out multiple lines at once in Vim editor in Linux. The other day I was trying to comment out a paragraph in a text file. I am bit lazy to go through each line and comment them out one by one. If you're anything like me, read on. It is not that difficult. There could be many ways do to this, but here I have listed five methods. As far as I tested, all methods given below worked just fine.

Comment Out Multiple Lines At Once In Vim Editor

Method 1:

This is the easiest method ever I found.

Let s take an example text file, say ostechnix.txt. Here is the contents of this text file.

$ cat ostechnix.txt

Usually, to comment out a line, we place the cursor at the beginning of the line, press i, and type #. This is the most common way. It is easy if there are only few lines in the file. But if you need to comment out multiple lines, there is an another easy way out.

First open the file in vim editor:

$ vim ostechnix.txt

Then highlight the lines that you want to comment out. To do so, place the cursor at the beginning of a line. Press SHIFT+V to highlight the whole line. After highlighting the first line, press DOWN arrow key to highlight the remaining lines one by one. Here is how the file will look like after highlighting the lines.

After highlighting the lines that you want to comment out, type the following:

:s/^/# /

Now you will see the selected lines are commented out.

Easy, isn't it? Of course it is!

Here, "s" stands for "substitution". In our case, we substitute ^ (in the beginning of the line) with # (hash). As we all know, we put # in-front of a line to comment it out.

Let us see another method.

Method 2:

Open the file in vim editor.

$ vim ostechnix.txt

Set line numbers by typing the following in vim editor and hit enter.

:set number

And then enter the following command:

:1,3s/^/#

In this case, we are commenting out the lines from 1 to 3. Check the following screenshot. The lines from 1 to 3 have been commented out.

To uncomment those lines, run:

:1,3s/^#/

Once you're done, unset the line numbers.

:set nonumber

Let us go ahead and see third method.

Method 3:

This one is same as above but slightly different.

Open the file in vim editor.

$ vim ostechnix.txt

Set line numbers:

:set number

Then, type the following command to comment out the lines.

:1,4s/^/# /

The above command will comment out lines from 1 to 4.

Finally, unset the line numbers by typing the following.

:set nonumber

Method 4:

This method is suggested by one of our reader Mr.Anand Nande in the comment section below.

Open file in vim editor:

$ vim ostechnix.txt

Press Ctrl+V to enter into ‘Visual block’ mode and press DOWN arrow to select all the lines in your file.

Then, press Shift+i to enter INSERT mode (this will place your cursor on the first line). Press Shift+3 which will insert ‘#’ before your first line.

Finally, press ESC key, and you can now see all lines are commented out.

Method 5:

This method is suggested by one of our Twitter follower and friend Mr.Tim Chase.

We can even target lines to comment out by regex. Open the file in vim editor.

$ vim ostechnix.txt

And type the following:

:g/\Linux/s/^/# /

The above command will comment out all lines that contains the word "Linux".

And, that's all for now. I hope this helps. If you know any other easier method than the given methods here, please let me know in the comment section below. I will check and update them in the guide. Also, have a look at the comment section below. One of our visitor has shared a good guide about Vim usage.