How To Comment Out Multiple Lines At Once In Vim Editor

Comment Out Multiple Lines At Once In Vim Editor

Howdy! Today, we will be discussing how to comment out multiple lines at once in Vim editor in Linux. The other day I had a situation where I need 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 only three 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 online.

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

$ cat ostechnix.txt

To comment out a line, place the cursor at the beginning of the line, press i, and type #. This is the usual 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 easy way do it.

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 and final 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 my 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.

If you find our guides useful, please share them on your social, professional networks and support OSTechNix. More good stuffs to come. Stay tuned!

Cheers!

Thanks for stopping by!

Help us to help you:

Have a Good day!!

You may also like...

  • anand nande

    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.

    • SK

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

  • nuny3

    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

    • SK

      Thank you. This is really a good read.