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.