Last Updated on 2021-09-15 by Clay
In many IDEs, we can quickly comment the code of a region, and then quickly test different code, after the test, we can also quickly cancel the comment. Usually, it can be done by a shortcut key in the IDE.
In the famous Vim text editor, of course we can also do this, the following is a simple record of how to complete it!
Quick Comment Code
First at all, let's briefly list the steps to quickly comment the code!
- Press
CTRL + v
to enter-- VISUAL BLOCK --
mode - Select the block to be commented
- Press
Shift + i
to enter input mode - Enter the comment symbol (assuming it is
//
) - Press the
Esc
key (it is written on the Internet that pressing once is fine, but it needs to be pressed twice on my computer)
Step 1: Press CTRL + v
to enter -- VISUAL BLOCK --
mode

Step 2: Select the block to be commented

Step 3: Press Shift + i
to enter input mode

Step 5: Enter the comment symbol (assuming it is //
)

Step 6: Press the Esc
key

As you can see, we have successfully comment a large number of blocks quickly.
Quickly Uncomment
Quickly uncommenting is easier than adding comments:
- Press
CTRL + v
to select the area we want to uncomment - Press
d
to uncomment
Step 1: Press CTRL + v
to select the area we want to uncomment
This is almost the first and second steps of adding comment. The only thing to note is that since the comment symbol I demo is //
, you will need to press the right button when selecting a block to directly eliminate the two rows.

Like this.
Step 2: Press d
to uncomment

Successfully accomplished.
References
- https://stackoverflow.com/questions/1676632/whats-a-quick-way-to-comment-uncomment-lines-in-vim
- https://unix.stackexchange.com/questions/120615/how-to-comment-multiple-lines-at-once