Batch comments and uncomments are implemented in vim, and vim uncomments
When debugging code, you can easily comment out or cancel code comments in batches. Many ides use shortcuts to comment out or uncomment selected code blocks in batches, how can this function be completed in Vim?
Method 1 selection mode
Batch comment:
Ctrl + v enter the block selection mode, move the cursor to select the row you want to comment on, and then press the uppercase I to enter the first line insertion mode and enter the annotation symbol such as // or #, after the input is complete, Vim automatically adds comments to the capitals of all selected rows.
Uncomment:
Press Ctrl + v to enter the block selection mode. Select the comment symbol at the beginning of the row you want to delete. Note: // select two. After the selection, press d to delete the comment.
Method 2 replacement command
Batch comment:
Use the following command to add a comment at the beginning of the specified line:
: Start line number, end line number s/^/annotator/g
Uncomment:
: Start line number, end line number s/^ annotator/g
Example:
Add // comment in 10-20 rows
: 10, 50 s # ^ # // # g
Delete from 10 to 20 rows // comment
: 10, 20 s # ^ // # g
Add # comment in lines 10-20
: 10, 20 s/^/#/g
Delete in 10-20 rows # comment
: 10, 20 s/^/#/g
Note that the regular delimiter In the example uses the opposite delimiter. If the delimiter matches //, use # As the separator, and do not need to escape/to save the number of inputs.
How does vim generate comments in batches?
CTRL + v or ctrl + Q.
What is the shortcut key for vim multi-line comment?
I use my own customization. Share it with you. Hope to help you.
Add the following statement to. vimrc:
Vmap <C-S-P> dO # endif <Esc> PO # if 0 <Esc>
In normal mode, press "V" (uppercase) to enter the <visual line> mode, press the top and bottom keys, select the row to be annotated, and press CTRL + SHIFT + P, in this way, # if 0 and # endif will be added up and down the selected row. It serves as a comment.
In the C language, it is not safe to use/**/to annotate multiple rows, so the "# if 0" method is used. If you need other methods, you can also refer to this idea.