Vim is an excellent command-line text editor, especially if you are comfortable with its shortcuts, modes, and bindings. However, when working with it.
We can encounter instances where we need to comment on code blocks while editing code and configuration files.
This article describes quick and easy ways to comment out multiple lines of code in Vim editor.
- Open the file using vim editor
- Highlight the lines that you want to comment out by going to the line you want to comment and press SHIFT+V then move the cursor up or down keys or k or j according to your cursor position to highlight lines one by one as shown below.
- Type the following and press ENTER
s: stands for substitution.
So we substitute the caret symbol (^) (in the beginning of the line) with (#) (hash).
- You will see the selected lines are commented then type :wq to save the file and exit.
- Open the file with vim
- Set line numbers by typing the below then press ENTER.
- Enter the following command then press ENTER.
Replace (7) with the number of lines you want to hash.
- Unset the line numbers.
- Type :wq to save the file and exit.
- Type the following and press ENTER key:
The above command will comment out all lines that contains the word line.
Replace line with a word of your choice.
In this article, we showed you how to comment multiple lines at once in vim editor using 3 methods.