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:
- Press Ctrl + V to enter Visual Mode.
- Navigate to the line you wish to comment out and press the C key.
- 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 comments.py
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.
- Assign a number to each line by the following command:
:set number
- Press the ESC key to launch Vim Command Mode.
- Enter the command:
:[start],[end]/s/^/#
The start is the line number to start from, and the end is the line number on which to end the comment block.
- 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.
:15,25s/^/#
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.
- 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.
- 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.
- Once you have the target block highlighted, press the RETURN key to exit Visual Line.
- Press the ESC key to enter command mode and enter the command:
:s/^/#
- Press Enter to run the command and comment on the highlighted lines.
3. Regular Expressions
Vim also supports regular expressions (regex) to comment out lines containing specific words. To comment out lines containing a particular term:
- Open your file in the vim editor and type the following command:
:g/\keyword/s/^/#
The “keyword” in this command represents the specific words you wish to comment out.
- 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:
:g/\True/s/^/#
4. Visual Mode
You can also use Vim visual mode to comment out a block of code.
- Navigate to the line you wish to start commenting out. Press the Ctrl + V keys to enable visual mode.
- 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.
- With the target lines selected, press Shift + I to enter insert mode.
- Enter the comment symbol for the target file. In this case, must insert the pound (
#
) symbol to comment Python code. - Press the ESC key, and Vim will comment out all the selected lines.
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
- Start by navigating to the line you wish to uncomment, then press Ctrl + V to activate the visual block mode.
- Select all the lines you want to uncomment using the up and down arrow keys or the letters k and j.
- 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.
- 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>/
- 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.
Our latest tutorials delivered straight to your inbox