Open In App

How to comment multiple lines in Vim Editor in Linux

Last Updated : 25 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Vim Editor in Linux is one of the most used text editors for writing scripts, codes, and textual data. There are different types of features offered by the Vim Editor through which we can create the text more comfortably. Various types of shortcuts, modes, and bindings are available in Vim Editor. While developing a code, we usually need to comment out the code for better understanding and readability, Also the configuration files need comments so that they can be easily understandable. To add multiple lined comments in Vim Editor we can use the below specified steps and comment out the multiple lines in Vim Editor in Linux.

How to comment on multiple lines in Vim Editor in Linux?

For commenting on multiple lines in Vim Editor in Linux, we have various methods through which it will be easy for us to comment on multiple lines in one go. Below are the methods that can help us to comment on multiple lines:

So, we will see each of the methods in a step-by-step manner.

Method 1: Commenting Using Line Numbers

Follow the below steps to comment on multiple lines in Vim Editor using Line Numbers:

Step 1: Firstly, we need to open the file in which we need to comment out the multiple lines. We have the basic example python script which we will be using for the demonstration. So open the file in Vim editor using the below command:

vim file.py
Opening File in Vim Editor

Opening File in Vim Editor

Step 2: After opening the file in Vim Editor, we need to press the ESC key on our keyboard so that Vim will open in the command mode and we can enter the Vim commands into it.

Opening Command Mode in Vim

Opening Command Mode in Vim

Step 3: Once the command mode gets opened we need to enter the below command. This will comment out the multiple lines in the one go.

Syntax:

:[start line],[end line]s/^/#

Command:

:5,10s/^/#
Entering Command

Entering Command

This will comment out the lines from 5 to 10 at the same time. You can change the start and end lines as per your need.

Lines Commented Successfully

Lines Commented Successfully

Method 2: Commenting Using Visual Mode

In this method, we will be commenting on the multiple lines using the Visual Mode. So follow the below steps to comment out multiple lines using Visual Mode:

Step 1: We need to switch from the “Normal” Mode to “Visual Mode” by using the “Ctrl+V” key on our Keyboard. Visual Mode allows us with different operations like delete, copy, cut, etc.

Switching from Normal to Visual Mode

Switching from Normal to Visual Mode

Step 2: After switching to the Visual Mode, we need to select the lines from the start to the end by using the “Up” and “Down” keys on our keyboard. Along with this, we can also use the alphabet letter “k” for upward selection and “j” for downward selection.

Selecting Lines to be commented

Selecting Lines to be commented

Step 3: Now, we need to switch from the Visual Mode to the Insert Mode by pressing the shortcut “Shift+i“.

Switching to Insert Mode

Switching to Insert Mode

Step 4: After switching to the Insert Mode, we need to enter the command symbol as “#” and then press the ESC key. This will comment on the lines that were selected in Step 2. The lines that we have selected will get commented on in a single one rather than commenting line by line in Vim Editor.

Entering # to Comment Selected Lines

Entering # to Comment Selected Lines

Method 3: Commenting Using Regular Expression

In this method, we will be using the Regular Expressions to comment out multiple lines. As Vim Editor has the support for Regular Expressions, this can be used to comment out the multiple lines. Follow the below steps to comment out multiple lines using Regular Expressions.

Step 1: First, we need to use the “Command” Mode in the Vim editor by using the “ESC” key on our keyboard.

Opening Command Mode

Opening Command Mode

Step 2: After switching to the Command Mode, we need to enter the below regex expression and then press the “Enter” key on the keyboard.

:g/\count1/s/^/#

The regex entered will comment out all the lines that contain the word count1 into it. We can customize it as per our needs by changing the word.

Entering Regex Pattern

Entering Regex Pattern

Step 3: After pressing the Enter key, the lines containing the “count1” word will get commented out from the entire code.

Lines Commented according to Regex

Lines Commented according to Regex

Frequently Asked Questions (FAQs)

Q 1: How to comment or uncomment multiple lines in Vim without entering the Insert Mode?

To command lines in Vim without Insert Mode can be done with the command (:normal ^V}I#). For uncommenting the commented lines, we can use the command (:normal ^V}x).

Q 2: Is it possible to comment or uncomment complete blocks of code in Vim?

Yes, we can comment or uncomment the entire blocks of code in Vim just by selecting the block in Visual mode and applying the comment characters.

Q 3: Can we comment or uncomment the lines in Vim with a specific file extension automatically?

Yes, we can set up the auto commands in our Vim configuration to automatically apply commenting or uncommenting when we open or save the files with the specific extension. For example, we can define the auto commands for (.py) files to use ‘#’ for comments.

Q 4: Is it possible to customize the commenting behavior in Vim?

We can customize the character behavior by creating or modifying the existing type-specific Vim configuration files like ‘.vimrc‘ or ‘ftplugin‘ and also by defining the custom mapping and settings.

Q 5: Can we use a macro to comment or uncomment the lines in Vim Editor

Yes, we cause the macro by recording a macro with ‘q‘ and then performing the comment/uncomment steps and replaying it with the ‘@’.

Conclusion

As Vim Editor is a terminal-based editor, but still there are many approaches and methods through which we can add comments to multiple lines in Vim Editor. Using the custom patterns, we can comment on the lines that follow the custom pattern. Also, by using the specific range command, we can comment on the multiple lines with he starting and ending line numbers. This makes the task of commenting easy in Vim Editor.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads