Vi and vim Editor (8): Global replacement (1)

Source: Internet
Author: User

Vi and vim Editor (8): Global replacement (1)

This chapter describes the global replacement commands in the vi editor. The global replacement command can be used to automatically replace all words in the file. Generally, two ex commands are used for global replacement: ": g" (global), ": s" (substitute ).

Replacement command:

The syntax of the replace command is as follows: ": s/old/new/". This command replaces the first old mode in the current line with new. The diagonal lines are used to separate each part of the command (if the diagonal lines are located at the last character of the command, the diagonal lines can be omitted ).

": S/old/new/g" replaces all old modes in the current row with new. Add the line address before s to specify the line range that will be affected by the replacement command. For example, "% s/old/new/g" can replace all the old modes in the file with new, as shown in: This command replaces all lines in the file with file.


The following command replaces the first line in all lines of the file with file: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20141228/2014122809525766.png" alt = "\">

Confirm replacement:

Be careful when using search and replacement commands. You can add the c option (confirm) at the end of the replacement command to confirm each replacement, as shown in:



In this case, enter y ("yes") to replace the currently matched string, while enter n to replace the string.

Context-related replacement:

Sometimes, you need to search for a mode. When a row containing the mode is found, replace another string in the row. That is, the search mode is not a string to be replaced. You can run the following command to implement ": g/pattern/s/old/new/g ". The first g indicates that it takes effect on all rows in the file, and the last g indicates that all old rows in each row are replaced with new (if this g is not available, then, only the first old in each row is replaced. patten is the search mode.

As shown in: this command searches for all rows that contain the "Line" mode and replaces the "this" string in this row with "that ".


When the search mode is the same as the replacement mode, there is no need to repeat the input. In this case, it can be abbreviated as ": g/string/s // new/g". This command works the same as the ": % s/string/new/g" command.

Rules for pattern matching:

In addition to searching constant strings, the vi editor can also search for Variable Modes, that is, regular expressions. A regular expression is an expression that combines common characters with special metacharacters.

Metacharacters used in search mode:

. (DOT): match any single character (except line breaks );

*: Matches 0 to infinite first characters, so. * can match any number of characters;

^: When ^ appears at the beginning of a regular expression, it indicates that the content of the subsequent regular expression must appear at the beginning of a line. If ^ does not appear at the beginning of a regular expression, it has no special meaning;

$: When $ appears at the end of a regular expression, it indicates that the content of the regular expression must appear at the end of a row. If $ does not appear at the end of the regular expression, it has no special meaning;

\: It is equivalent to an escape character. special characters following the character are considered as common characters.

[]: Match any character in square brackets. For example, pat, pet, and put in p [aeu] t matching. In addition, if the target matches a character in a range, it can be expressed by the first character plus a hyphen and the last character. For example, [A-Z] matches uppercase letters between A and Z, and [0-9] matches any number from 0 to 9. In addition, square brackets can contain more than two ranges, and can be used together with a single character. When the symbol ^ is used as the first character in square brackets, it indicates that any character that is not within the character range in square brackets is matched.

\ (\): Saves the mode between \ (and \) to a special space (called a reserved buffer ). This method can save nine schemas in any row.

\ <\> Matches words that start with (\ <) or end with (\>;

~ : Match the regular expression used for the last search.

POSIX square brackets:

In addition to matching any character in square brackets, POSIX introduces other methods for comparing non-English characters. In the POSIX standard, character groups in square brackets are called square brackets expressions ". In the square brackets expression, in addition to text characters, it can also include other elements:

Character class: POSIX character class includes keywords enclosed by [: And.

Proofreaders: a proofreader is a sequence consisting of multiple characters, but must be considered as a unit. And use [. And.] to enclose the required characters.

Equivalence Class: it lists all character sets that should be considered equal and should be included in [= and =.

These three types must all appear in square brackets, for example, [[: alpha:]!]. Match any letter or exclamation point. For specific POSIX character types, see Chapter 6 of vi and vim Editor: Global replacement.

Metacharacters used to replace strings:

For global replacement, the special metacharacters mentioned above can only be used in the search part of the command, that is, the first part. In the replacement of the string, these metacharacters may lose special meaning. For example, the command in will only replace A, B, or C with "[abc]", rather than lowercase letters.


However, there are still some special metacharacters in the replacement string.

\ N: Replace the text in the nth mode stored by \ (and;

\: It has the same meaning as the \ character in search mode. It is an escape character that converts a special character to a common character and thus loses its special meaning;

&: When used in replacement characters, & will be replaced with the full text that matches the search mode, which is useful in avoiding repeated text input;

~ : ~ In the search mode ~ Metacharacters have similar meanings and are used to represent the replacement text in the last replacement command. This is useful in repeated editing;

\ U or \ l: converts the next character in the replacement string to uppercase or lowercase;

\ U or \ L or \ e or \ E: \ U or \ L will convert all subsequent characters into uppercase or lowercase letters until \ e or \ E appears.

More replacement tips:

: S: equivalent to: s //~ /, That is, the last replacement will be repeated;

: &: You can also repeat a replacement. You can think of it as "the same thing ";

:~ : Similar to the: & command, but the pattern used for searching is the previous regular expression, rather than the regular expression used in the previous replacement command.

In addition to using/characters as separators in the replacement command, you can also use any non-letter, non-numeric, non-space characters, except for the backslash, double quotation marks, and vertical lines, this is useful when replacing the path string.

Summary of the vi commands in this chapter:

: S/old/new, g option, c option,: g,
Special characters in search mode., *, ^, $, [], \ (\), \ <, \> ,~,
Special characters \ n ,\,&,~, \ U, \ l, \ U, \ L, \ e, \ E,
: S ,:&,:~.


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.