26.6 C
Jaipur
Wednesday, July 28, 2021

How To Comment Out Multiple Lines At Once In Vim Editor

Must read

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.

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.

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

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 visitor has shared a good guide about Vim usage.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -

Latest article