How to Comment Multiple Lines in Vim Editor

Vim Featured

Vim is one of the most popular and influential command-line text editors. It comes packaged with a lot of functionality and features for any terminal geek. However, Vim can be discouraging to new users – not because it is complex to learn and master, but because it has an unconventional way of doing things. For example, Vim uses shortcuts, modes, and bindings that often take some getting used to, and you need a full article to show you how to exit Vim.

This guide discusses how to get comfortable with Vim by learning quick and easy steps to comment on multiple code blocks.

First, before you can comment on multiple lines in Vim, you need to know how to comment a single line, so let’s focus on that as our starting point.

How to Comment a Single Line in Vim

To comment out a single line in Vim:

  1. Press Ctrl + V to enter Visual Mode.
  2. Navigate to the line you wish to comment out and press the C key.
  3. Depending on your Vim configuration, this should comment out the selected line.

That’s all there is to it – easy, isn’t it? Now let’s dive a bit deeper.

How to Comment Multiple Lines

We use a simple Python script to learn how to comment out multiple lines in Vim.

Vim Python

1. Using Line Numbers

Using line numbers is one method to comment out multiple lines in Vim. Vim allows you to specify the start and finish line and comment them out.

  1. Assign a number to each line by the following command:
:set number
  1. Press the ESC key to launch Vim Command Mode.
  2. Enter the command:

The start is the line number to start from, and the end is the line number on which to end the comment block.

  1. Now that you have commented on the selected lines, you can unassign the line numbers using the following command:
:set nonumber

The following screenshot illustrates how to use this command to comment from lines 15 to 25.

Vim Comment Line Number

Tip: If you love Vim, you can also turn it into a Word processor.

2. Highlight Block

Suppose you do not know the line numbers to comment out. In that case, you can use the highlight method to highlight the target block and comment it out.

  1. Put your cursor on the line you want to comment on and press the Shift + V keys. Doing this will highlight the entire line where the cursor is located.
  2. Use the up and down arrow keys (or the letters k and j) to select the lines you wish to comment out. Pressing up or k highlights the lines above your chosen line, while hitting down or j highlights the ones below.

Note: You can also use your mouse scroll wheel to select the lines you wish to comment on.

Vim Highlight Block
  1. Once you have the target block highlighted, press the RETURN key to exit Visual Line.
  1. Press the ESC key to enter command mode and enter the command:
  1. Press Enter to run the command and comment on the highlighted lines.
Vim Highlight Block Comment

3. Regular Expressions

Vim also supports regular expressions (regex) to comment out lines containing specific words. To comment out lines containing a particular term:

  1. Open your file in the vim editor and type the following command:

The “keyword” in this command represents the specific words you wish to comment out.

  1. Press Enter to run the command. Vim automatically highlights the target word before executing the command.

The example below comments out all the lines containing the keyword True:

Vim Comment Regex

4. Visual Mode

You can also use Vim visual mode to comment out a block of code.

  1. Navigate to the line you wish to start commenting out. Press the Ctrl + V keys to enable visual mode.
  2. Select all the lines you wish to comment out using the up and down arrow keys or the letters k and j. Vim will insert a highlight at each line’s beginning to indicate the selected line.
Vim Comment Visual Mode
  1. With the target lines selected, press Shift + I to enter insert mode.
  2. Enter the comment symbol for the target file. In this case, must insert the pound (#) symbol to comment Python code.
  3. Press the ESC key, and Vim will comment out all the selected lines.
Vim Comment Visual Set

Tip: If you are an experienced Vim user, don’t forget to check out these Vim tips and tricks, too.

How to Uncomment Multiple Lines

You may also need to uncomment multiple lines. To do this, you can use the visual mode or a command.

1. Visual Mode

  1. Start by navigating to the line you wish to uncomment, then press Ctrl + V to activate the visual block mode.
  2. Select all the lines you want to uncomment using the up and down arrow keys or the letters k and j.
  3. Once your lines are selected, press the X key to remove the comments.

2. Using a Command

Another method is to use a command to remove all lines starting with the comment symbol.

  1. Open your file in the vim editor and type the following command. Replace <comment symbol> with the actual comment symbol you want to remove.:
:%s/^<comment symbol>/
Vim Ucomment
Vim Uncomment True
  1. Press Enter to run the command.

You can also highlight the lines you want to uncomment following the steps in method 1 and then use the command in step 1 to remove comments.

Frequently Asked Questions

How to merge multi line to one line in Vim?

You can merge two lines in the Vim editor by placing your cursor on the first line and pressing Shift + j (capital J). This way, the line with the cursor will be combined with the line immediately below it. Keep pressing Shift + j to add more lines.

How to write text in Vim?

You can enter text in Vim in Insert Mode. Press Shift + i to enable it. You can also press Enter to go to a new line or navigate using the arrow keys in Insert Mode. Press Esc to return to Command Mode again.

How to save a file in Vim editor and quit?

To save a file with or without exiting Vim, enter the Command Mode by pressing Esc. Then, press Shift + ; (to type :, the colon sign). In front of the colon, type x, press Enter to save your file, and quit Vim. You can also type w and press Enter to save your file without exiting Vim or q! to close Vim without saving your file.

Farhad Pashaei
Farhad Pashaei

As a technophile, Farhad has spent the last decade getting hands-on experience with a variety of electronic devices, including smartphones, laptops, accessories, wearables, printers, and so on. When he isn’t writing, you can bet he’s devouring information on products making their market foray, demonstrating his unquenchable thirst for technology.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox