How To 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
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:
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:
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.
Open the file in vim editor.
Set line numbers by typing the following in vim editor and hit enter.
And then enter the following command:
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:
Once you’re done, unset the line numbers.
Let us go ahead and see third and final method.
This one is same as above but slightly different.
Open the file in vim editor.
Set line numbers:
Then, type the following command to comment out the lines.
The above command will comment out lines from 1 to 4.
Finally, unset the line numbers by typing the following.
This method is suggested by one of our reader Mr.Anand Nande in the comment section below.
Open file in vim editor:
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.
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.
And type the following:
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!
Thanks for stopping by!
Help us to help you:
- Subscribe to our Email Newsletter : Sign Up Now
- Support OSTechNix : Donate Via PayPal
- Download free E-Books and Videos : OSTechNix on TradePub
- Connect with us: Facebook | Twitter | Google Plus | LinkedIn | RSS feeds
Have a Good day!!