Learning VI and VIM editor (8): Global substitution (1)

Source: Internet
Author: User
Tags posix uppercase letter

This chapter learns the global substitution commands in the VI editor. With the global Replace command, you can automatically replace all occurrences of a word in a file. a global substitution typically uses two ex commands: ": G" (Global), ": S" (substitute).

Replace command:

The syntax for the Replace command is the following: ": s/old/new/", which replaces the first existing old mode in this line with the new one. where slashes are used to separate portions of the command (if the slash is at the last character of the command, the slash can be omitted).

": s/old/new/g" replaces all old modes in the current row with new. With the line address in front of S, you can specify the range of rows that the replacement command will affect. For example, "%s/old/new/g" can replace all the old modes in the file with new, as shown in: This command replaces all the line in the file with files.


The following command will only replace the first line in all lines of the file with files:


Confirm Replacement:

You should be very careful when using the search and replace commands. You can add the C option (for confirm) at the end of the replacement command to confirm it before each substitution, as shown in:



Enter Y (for "yes") to replace the currently matched string, and input n will not be replaced.

Context-related substitutions:

Sometimes, you need to search for a pattern, and when you find a row that contains the pattern, replace another string in that row. that is, the search pattern is not a string that needs to be replaced. You can now implement ": G/pattern/s/old/new/g" with the following command. Where the first G represents the effect on all rows in the file, and the last G represents replacing all old in each row with new (if this g is not the first old in each row is replaced), Patten is the search pattern.

As shown: This command searches all lines that contain "line" mode and replaces the "This" string in that row with "that".


When the pattern used for searching is the same as the pattern used for substitution, there is no need to repeat the input. This can be abbreviated as " : g/string/s//new/g", which has the same effect as the ":%s/string/new/g" command.

Rules for pattern matching:

In addition to the ability to search for constant strings, the VI editor can also search for mutable patterns, which are regular expressions. A regular expression is an expression that combines ordinary characters with special meta-characters.

Metacharacters used in search mode:

. (dot): matches any single character (except line break);

*: Matches 0 to infinitely many previous characters, so. * Can match any number of characters;

^: When ^ appears at the beginning of a regular expression, the contents of the regular expression representing the following must appear at the beginning of a line. If ^ is not the beginning of the regular expression, there is no special meaning;

$: When $ appears at the end of a regular expression, the contents of the preceding regular expression must appear at the end of a line. If $ is not present at the end of the regular expression, there is no special meaning;

\: Equivalent to an escape character, the following special characters are treated as normal characters.

[]: Matches any one of the characters in the square brackets. For example p[aeu]t matches in Pat,pet,put. And if the match target is a range of characters, the first character can be added with a hyphen, plus the last character. For example, [A-z] matches an uppercase letter between A and Z, [0-9] matches any number within 0 to 9. and the square brackets can contain more than two ranges, you can also mix the range and a single character. when you insert a symbol ^ as the first character in square brackets, it means that any character that is within the range of characters that is not in square brackets is matched.

\ (\): The pattern between \ (and \) is saved to a special space (called a reserved buffer). This method can save 9 patterns in any row.

\< \> matches words that begin with certain characters (\<) or end (\>);

~: Matches the regular expression that was used in the previous search.

POSIX square Bracket expression:

In addition to the square brackets that can be used to match any one of the characters in square brackets, POSIX introduces another method for comparing characters that are not English letters. In the POSIX standard, a group of characters within square brackets is called a "square bracket expression." In a square bracket expression, you can include other elements in addition to literal characters:

Character class: The POSIX character class includes keywords enclosed in [: and:].

Proofing symbols: A collation symbol is a sequence of multiple characters, but must be treated as a unit. and use [. and.] Enclose the required characters.

Equivalence class: The equivalence class lists all the character sets that should be treated as equal, enclosed in [= and =].

All three classes must appear in a square bracket expression, such as [[: alpha:]!] matches any one of the alphabetic characters or exclamation marks. Specific POSIX character types can be see the VI and Vim editor, chapter Sixth: Global substitution.

Metacharacters used in the replacement string:

As a global replacement, the previously mentioned metacharacters with special meaning can only be used in the search portion of the command, the first part. In the replacement string section, these metacharacters may lose their special meaning. For example, the command in will only replace a or B or C with "[ABC]" instead of the corresponding lowercase letter.


However, in the substitution string, there are still some metacharacters that have special meanings.

\ n: use the nth pattern stored by \ (and \) to do the text substitution;

\: The meaning of the \ character in the search pattern is the same as the escape character, which causes a special character to be changed to a normal character, thus losing its special meaning;

&: When used in a replacement character,,& is replaced with the full text that the search pattern matches, which is useful for avoiding repetition of the input text;

~: Similar to the ~ metacharacters in the search pattern, used to represent alternate text in the last substitution command. This is useful for repeating edits;

\u or \l: makes the next character in the replacement string uppercase or lowercase;

\u or \l or \e or \e: \u or \l causes all subsequent characters to be converted to uppercase or lowercase until a \e or \e appears.

More Replacement tips:

: s: Equivalent to: s//~/, which will repeat the last substitution;

:&: Can also repeat the last substitution, can think of & as "the same thing";

: ~: Similar to the:& command, but the pattern used to search is the previous regular expression, rather than the regular expression used in the previous replace command.

In addition to using the/character as a delimiter in the Replace command, you can also use any non-alphabetic, non-numeric, non-whitespace character, except backslashes, double quotes, and vertical bars, which are useful when replacing a path string.


This chapter VI command summarizes:

: s/old/new,g option, c option,: G,
Special characters in the search pattern., *, ^, $, [], \ (\), \<, \>, ~,
Special characters in replacement mode \n,\, &,~, \u, \l,\u,\l, \e,\e,
: s,: &,: ~.


Learning VI and VIM editor (8): Global substitution (1)

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.