Replacement and searching of VIM Functions

Source: Internet
Author: User

Vim has powerful replacement and search functions. If you can use it skillfully, You can greatly improve your work efficiency.

Replace

Syntax: [ADDR] S/source string/destination string/[Option]

[ADDR] indicates the search range, for example:

"1, N": indicates from 1st rows to N rows
"%": Indicates the entire file, which is the same as "1, $"
"., $": Indicates from the current row to the end of the file

[ADDR] indicates the current row if omitted

S: indicates the replacement operation, which is short for substitute.

[Option]: indicates the operation type, for example:

G: GLOBE, indicating global replacement of C: Confirm, indicating confirmation of P: indicates that the replacement result is displayed row by row (CTRL + l restore screen) I: Ignore, if not case-insensitive [Option] is omitted, only the first matching string in each line is replaced. If special characters such as '/', '<' appear in the source string and destination string ', '>', ',' and so on. Common commands need to be followed by a backslash \ for escape: # Replace the first a in the current line with B: s/a/B/# Replace all A in the current row with B: S/a/B/G # Replace the first a in each row with B: % S/A/B # Replace all A in the entire file with B: % S/a/B/G # Replace the first a from 1 to 3 with B: 1, 3 S/a/B/# Replace all A values in rows 1 to 3 with B: 1, 3 S/a/B/g, which is a common replacement, however, the problems we encounter on a daily basis are more than that simple. This involves some advanced replacement operations, such as escaping and regular expressions. The following are some examples: # Use # As the separator. The '/' in the middle will not be used as the separator. For example, replace the character string "A/" of the current row with "B /": s # A/# B/# Find the row containing the letter A and delete it: G/A/D # delete all empty rows
: G/^ $/D # Replace multiple spaces with one space: S/\ + // G # Use the \ (and \) symbol in the regular expression to enclose the regular expression, you can use variables such as \ 1 and \ 2 to access the content in \ (and \), as follows:
Change data1 data2 to data2 data1
: S/\ (\ W \ + \) \ s \ + \ (\ W \ + \)/\ 2 \ t \ 1
Continue to supplement later!
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.