Batch comment and uncomment under Vim

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.