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!