How to Comment Multiple Lines at Once in Vim

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 and uncomment out multiple lines of code in Vim editor. Learning how to do this will remove the need to go down each line and comment out each line.

How to Comment Multiple Lines – Vim

For this guide, we will use a simple python script called simple.py

Vim simple.py

Method #1 – Line Numbers

The simplest method to comment out multiple lines is to use line numbers. First, open your file in Vim and press ESC to enter command mode.

Enter

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

To demonstrate, let us comment out from lines 10 to 15./

If you do not want to use line numbers in the command, use the highlight method.

To do this, move to the line you want to start commenting out and press SHIFT + V. Doing this will highlight the entire line. Then, use the up and down arrow keys to highlight the other lines.

Once you have the lines to comment out highlighted, press enter; the Highlighted code should look something like this:

Press ESC to enter command mode and use the command as:

:s/^/#

Once completed, you should have all the lines you highlighted commented as:

Method # 2 – Visual Mode

The other method you can use to comment out multiple lines is to use Visual Mode.

To do this, press ESC and navigate to the lines you want to comment out.

Press CTRL + V to enable Visual Mode.

Using the up and down arrow key, highlight the lines you wish to comment out.

Once you have the lines selected, press the SHIFT + I keys to enter insert mode.

Enter your command symbol, for example, # sign, and press the ESC key. Vim will comment out all the highlighted lines.

# 3 – Regular Expression

One of our favorite methods of commenting lines of code in Vim is regular expressions. For example, using regex is a powerful method that comments outlines that contain a specific word.

For example, to comment lines that contain the word def, use the command:

:g/\while/s/^/#

Press enter to comment out the lines:

How to Uncomment Multiple Lines – Vim

The reverse is true; once you comment on outlines, you need to uncomment them. To uncomment multiple lines, use the following method.

Enter the following to uncomment lines starting with a # sign

:%s/^#/

Another method is to use Visual mode.

Navigate to the line you wish to comment out and press CTRL + V to enter Visual mode.

Use the up and down arrow keys to highlight the lines you wish to uncomment. Once selected, press x to remove the comments.

Once you press x, it automatically uncomments the lines.

To Conclude

In short, using Vim shortcuts should improve your workflow and help improve your efficiency. Please search our site for more Vim tutorials to improve your Vim skills.

Thank you for reading!



from Linux Hint https://ift.tt/36PICXH

Post a Comment

0 Comments