How to Comment Out Multiple Lines at Once in Vim Editor?

“Vim” is an open-source well-reputed command line text editor to create and edit text files, configuration files, and scripts. It supports many key features like fancy themes, multiple tabs, git support, syntax highlighting, set numbers, and commenting out single/multiple lines. 

The comments are the additional lines known as an understandable/readable explanation that makes the source code easier for humans to understand. They also reduce the debugging effort of developers by commenting out the errors without deleting the whole source code.

This post explains all the possible methods to comment out multiple lines at once in the vim editor.

  • Method 1: Using the Line Number
  • Method 2: Using Highlight Block
  • Method 3: Using the Visual Mode
  • Method 4: Using the Regular Expressions
  • How to Uncomment Multiple Lines in Vim?

Method 1: Using the Line Number

The vim text editor supports the “set number” command used to display the line number in the text file. It can be used for commenting out multiple lines at a time in the Vim editor. Let’s see how it can be done:

Open a File/Script

An existing “” script is opened in the “vim” editor by specifying the fits name with “vim” in this format:

$ vim

Enter in the Command Mode

The “vim” editor is in normal mode by default, press the “Esc” escape key to enter into the command line mode.

Execute the Command

The general syntax to execute the “:set number” command for multiple lines at once in the vim editor is:

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

Use this syntax and comment out the line number “6, “7” and “8” of the “” script in this way:


In the above command, the “s/” substitutes the “#” as a comment character at the beginning of the specified lines:

The line numbers 6,7, and 8 have been commented denoted by the “#” symbol:

The “3 substitution on 3 lines” statement shows the occurrence s of the “#” symbol in the three lines.

Method 2: Using Highlight Block

In case the user does not want to recall the line number and comment out the multiple lines randomly then the “highlight block” method is beneficial.

Highlight Multiple Lines

Navigate to the line where the user wants to start commenting and hit the “Shift+V” key to enable “Visual Line” mode. Select the Lines using the up/down arrow keys:

The highlighted block of the above open file is selected for comment.

Enable Command Mode

Press the “Esc” key to return the “Visual Line” and enable the “command” mode. Type the following command and press the “Enter” key:


The above substitute command substitutes(/s) the “#” sign at the start of the highlighted lines in place of the “^(leading tab spaces)”:

The execution of the above command shows this result:

The highlighted block of multiple lines has been commented out.

Method 3: Using the Visual Mode

The “Visual Mode” of vim editor performs the selection of text to cut, delete, copy, and many other operations. In this method, it is used to highlight the desired number of lines and make them comment at once.

Enable the Visual Mode

Switch from “Normal” to “Visual mode” by pressing the “Ctrl+V” key from the keyboard:

Select Lines

Move the mouse pointer at the start of the line that you want to select. Press the “up” and “down” keys to navigate to the upward and downward positions. 

In addition, the letter “k(for upward)” and “j(for downward)” can also be used for this task:

Switch to Insert Mode

Press the shortcut key “Shift+i” to enter into the “Insert” mode:

Comment Highlighted Lines

Insert the “#” symbol and press the “Esc” key to pass the “#” symbol at the start of all selected lines: 

The selected multiple lines have been commented out at once.

Method 4: Using the Regular Expressions

The vim editor also allows the regular expressions support to comment on the specific lines of a file. It comments out the lines that contain the defined keyword at the start of the line. Let’s see its practical implementation:

Enable into Command Mode

Enable the “Command” mode of the vim editor by using the “Esc” key:

Type the Regular Expression

Insert the following regular expression as a command and hit the “Enter” key:


This regular expression substitutes(/s) the “#” sign globally(g/) before each line that will start from the keyword “Linux”:

All five lines that start from the “Linux” keyword have been commented out.

How to Uncomment Multiple Lines in Vim?

The “regular expression” key feature also assists the user to uncomment multiple lines. For this purpose highlight the commented lines using “Ctrl+V” “Visual Block” mode:

Switch from the command mode and type this regular expression as a command:


The command replaces all the tab spaces and the “#” sign with nothing, i.e., uncomment the commented lines:

Execute the command:

All the highlighted multiple lines have been uncommented.


The “vim” editor offers the “line number”, “highlight block, and “regular expression” feature to comment out multiple lines of a file/source code at the same time. This operation can also be done using the “Visual Mode” of the vim editor by pressing the “Ctrl+V” shortcut key. 

This post has listed all the possible aspects to comment out the multiple lines at once in the vim editor.