Home Vim How To Comment Out Multiple Lines At Once In Vim Editor

How To Comment Out Multiple Lines At Once In Vim Editor

By sk
Published: Last Updated on 68.5K views

Today let us learn one of the useful Vim tip - how to comment out multiple lines at once in Vim editor in Linux. This will come in handy when you want to comment out a paragraph or a block in a text file.

Introduction

Usually, we put a # (hash) or // (double slash) symbol at the beginning of a line to comment out that line. What if you want to comment out many lines? Go to each line and add # in front of each the line? Not necessary.

You don't need to comment one line after another. Vi and Vim editors allows you to quickly comment and uncomment multiple lines in one go.

In this guide, I've listed a few different ways to comment out multiple lines in Vim editor. All the steps provided here are tested on Fedora and Ubuntu Linux distributions.

Comment out multiple lines at once in Vim editor

For the purpose of this guide, I will be using a text file called ostechnix.txt. Here is the contents of the file.

$ cat ostechnix.txt
View contents of a text file using cat command
View contents of a text file using cat command

There could be many ways do to comment multiple lines in Vim editor. Here, I have listed five methods. Let us see the first one.

Method 1:

Step 1: Open the file using vim editor with command:

$ vim ostechnix.txt

Step 2: Highlight the lines that you want to comment out. To do so, go to the line you want to comment and move the cursor to the beginning of a line.

Press SHIFT+V to highlight the whole line after the cursor. After highlighting the first line, press UP or DOWN arrow keys or k or j to highlight the other lines one by one.

Here is how the lines will look like after highlighting them.

Highlight lines in Vim editor
Highlight lines in Vim editor

Step 3: After highlighting the lines that you want to comment out, type the following and hit ENTER key:

:s/^/# /

Please mind the space between # and the last forward slash (/).

Now you will see the selected lines are commented out i.e. # symbol is added at the beginning of all lines.

Method 1 - comment out multiple lines at once in Vim editor
Comment out multiple lines at once in Vim editor

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

Step 4: After commenting the lines, you can type :w to save the changes or type :wq to save the file and exit.

Let us move on to the next method.

Method 2:

Step 1: Open the file in vim editor.

$ vim ostechnix.txt

Step 2: Set line numbers by typing the following in vim editor and hit ENTER.

:set number
Set line numbers in Vim
Set line numbers in Vim

Step 3: Then enter the following command:

:1,4s/^/#

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

Method 2 - comment out multiple lines at once in Vim editor
Comment out multiple lines at once in Vim editor

Step 4: Finally, unset the line numbers.

:set nonumber

Step 5: To save the changes type :w or :wq to save the file and exit.

The same procedure can be used for uncommenting the lines in a file. Open the file and set the line numbers as shown in Step 2. Finally type the following command and hit ENTER at the Step 3:

:1,3s/^#/

After uncommenting the lines, simply remove the line numbers by entering the following command:

:set nonumber

Let us go ahead and see the third method.

Method 3:

This one is similar to Method 2 but slightly different.

Step 1: Open the file in vim editor.

$ vim ostechnix.txt

Step 2: Set line numbers by typing:

:set number

Step 3: Type the following to comment out the lines.

:1,4s/^/# /

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

Method 3 - comment out multiple lines at once in Vim editor
Comment out multiple lines at once in Vim editor

Step 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.

Step 1: Open file in vim editor:

$ vim ostechnix.txt

Step 2: Go to the line you want to comment. Press Ctrl+V to enter into ‘Visual block’ mode.

Enter into Visual block mode in Vim editor
Enter into Visual block mode in Vim editor

Step 3: Press UP or DOWN arrow or the letter k or j in your keyboard to select all the lines that you want to be commented in your file.

Select the lines to comment in Vim
Select the lines to comment in Vim

Step 4: Press Shift+i to enter into INSERT mode. This will place your cursor on the first line.

Step 5: And then insert # (press Shift+3) before your first line.

Insert hash symbol before a line in Vim
Insert hash symbol before a line in Vim

Step 6: Finally, press ESC key. This will insert # on all other selected lines.

Method 4 - comment out multiple lines at once in Vim editor
Comment out multiple lines at once in Vim editor

As you see in the above screenshot, all other selected lines including the first line 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. In other words, we can comment all the lines that contains a specific word.

Step 1: Open the file in vim editor.

$ vim ostechnix.txt

Step 2: Type the following and press ENTER key:

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

The above command will comment out all lines that contains the word "Linux". Replace "Linux" with a word of your choice.

Comment out all lines that contains a specific word in Vim editor
Comment out all lines that contains a specific word in Vim editor

As you see in the above output, all the lines have the word "Linux", hence all of them are commented out.

And, that's all for now. I hope this was useful. If you know any other method than the given methods here, please let me know in the comment section below. I will check and add them in the guide.

Also, have a look at the comment section below. One of our readers has shared a good guide about Vim usage.

You May Also Like

8 comments

anand nande November 23, 2017 - 9:40 am

Method 4:

– Press Ctrl+V to enter into ‘Visual block’ mode
– Press down arrow to select all the lines in your file
– Press Shift+i to enter INSERT mode (this will place your cursor on the first line)
– Press Shift+3 (which will insert ‘#’ without the quotes) before your first line
– Press ESC key now, and you can see all lines are commented out.

Reply
SK November 23, 2017 - 10:47 am

You’re genius. It’s working. Thanks brother.

Reply
nuny3 November 23, 2017 - 8:46 pm

If you want to be productive in Vim you need to talk with Vim with *language* Vim is using. Every solution that gets out of “normal
mode” is most probably not the most effective.

METHOD 1
Using “normal mode”. For example comment first three lines with: I#j.j.
This is strange isn’t it, but:
I –> capital I jumps to the beginning of row and gets into insert mode
# –> type actual comment character
–> exit insert mode and gets back to normal mode
j –> move down a line
. –> repeat last command. Last command was: I#
j –> move down a line
. –> repeat last command. Last command was: I#
You get it: After you execute a command, you just repeat j. cobination for the lines you would like to comment out.

METHOD 2
There is “command line mode” command to execute “normal mode” command.
Example: :%norm I#
Explanation:
% –> whole file (you can also use range if you like: 1,3 to do only for first three lines).
norm –> (short for normal)
I –> is normal command I that is, jump to the first character in line and execute insert
# –> insert actual character
You get it, for each range you select, for each of the line normal mode command is executed

METHOD 3
This is the method I love the most, because it uses Vim in the “I am talking to Vim” with Vim language principle.
This is by using extension (plug-in, add-in): https://github.com/tomtom/tcomment_vim extension.
How to use it? In NORMAL MODE of course to be efficient. Use: gc+action.

Examples:
gcap –> comment a paragraph
gcj –> comment current line and line bellow
gc3j –> comment current line and 3 lines bellow
gcgg –> comment current line and all the lines including first line in file
gcG –> comment current line and all the lines including last line in file
gcc –> shortcut for comment a current line

You name it it has all sort of combinations. Remember, you have to talk with Vim, to properly efficially use it.
Yes sure it also works with “visual mode”, so you use it like: V select the lines you would like to mark and execute: gc

You see if I want to impress a friend I am using gc+action combination. Because I always get: What? How did you do it? My answer it is Vim, you need to talk with the text editor, not using dummy mouse and repeat actions.

NOTE: Please stop telling people to use DOWN arrow key. Start using h, j, k and l keys to move around. This keys are on home row of typist. DOWN, UP, LEFT and RIGHT key are bed habit used by beginners. It is very inefficient. You have to move your hand from home row to arrow keys.

VERY IMPORTANT: Do you want to get one million dollar tip for using Vim? Start using Vim like it was designed for use normal mode. Use its language: verbs, nouns, adverbs and adjectives. Interested what I am talking about? You should be, if you are serious about using Vim. Read this one million dollar answer on forum: https://stackoverflow.com/questions/1218390/what-is-your-most-productive-shortcut-with-vim/1220118#1220118

Reply
SK November 24, 2017 - 12:46 pm

Thank you. This is really a good read.

Reply
mdebusk November 26, 2019 - 7:07 am

I’ve tried the “boxes” utility with vim and it can be a lot of fun.

https://boxes.thomasjensen.com/

Reply
Sérgio Araújo December 17, 2020 - 4:43 am

Method 6
:%norm I#

Reply
Branden Burrola August 4, 2022 - 9:07 am

I am a great fan of your site. Amazing content.

Reply
sk August 4, 2022 - 10:41 am

Pleased to know. Thanks for your positive feedback!

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More