Learning VI and VIM editor (9): Global Substitution (2)

Source: Internet
Author: User
Tags function prototype

The previous article has learned how to replace globally with the ": S" command in the VI editor, and you can use regular expressions in replacement commands for strong pattern matching. This article will learn more examples of pattern matching to further familiarize yourself with regular expressions.

Example of pattern matching:

(1) match a complete word: as shown, if you need to replace all the child words in the text with children, you might first think of the replacement command shown:

but It is this command that will replace the child in the two words of Childrendish,fabrichild, which is not what we want. The key to the substitution is that if a child is a single word, it might think of the following method:


This command searches for a child that ends with a space, comma, semicolon, and replaces the child with children, preserving the symbol after the original child. However, the command will still fail because Fabrichild will also be replaced.

In fact it is very simple, combined with \< and \> can match a single complete word, such as the following command to complete the replacement we want to work:

(2) search for general word classes: as shown, all boxes in the Mgibox,mgrbox,mgabox need to be replaced with square, which can be done in the following ways:

The special meta-characters used in this command are explained in the previous article, which is no longer explained in one by one.

(3) Replace the file path: as shown, you need to replace/home/test/with/usr/local, because "/" has a special meaning in the Replace command (as a delimiter for each part of the replacement command), so you need to replace it in the following way:


However, as already mentioned in the previous article, in addition to using "/" as a delimiter, you can also use other characters as delimiters, as shown in, with "; "As a delimiter for each part of the replacement command, you can make the entire replacement command more concise:

(4) Change all periods between lines 1th to 10th into semicolons:": 1,10s/\./;/g", because ". "has a special meaning in the replacement string, so it needs to be converted to a normal period by the" \ "escape character.

(5) Convert the Help or the offer that appears to: ":%s/[hh]elp/\u&/g".

(6) Replace one or more spaces with a single space: " :%s/*//g ", note that in search mode," * "is preceded by two spaces, so that one or more spaces can be matched.

(7) Delete all blank lines:": g/^$/d", "^$" can match the middle of the row without content, that is, blank lines. Note that a line that contains only a space or TAB key, although it appears to be blank, does not delete such a line. If you need to delete a blank row (a row that does not contain any content) and all lines that include only spaces or tab keys, you can use the following command:": g/^[tab]*$/d".

(8) Delete all spaces at the beginning of each line:":%s/^ *\ (. *\)/\1/", notice that the first "*" is preceded by two spaces so that one or more spaces can be matched.
(9) Delete all spaces at the end of each line:":%s/\ (. *) \ *$/\1/", also note that the last "*" is preceded by two spaces.

(10) Add "//" at the beginning of each line: quickly comment a piece of code by adding "//" at the beginning of each line:


Note that the above commands are used "; "As a delimiter, and adopt." , +3 "Specifies the scope of the annotation, that is, the current line and 3 lines of code after it. In fact, there is a simpler way of writing:


This command replaces the line start (^) with a "//", "^" for the beginning of a line (this is a logical concept, not the actual character, so the actual beginning of the character is not actually replaced).

(11) Add a period to the end of all lines of the file:":%s/$/./".

(12) Reverse the order of all hyphens separated parts:


(13) All the words in the file are capitalized:":%s/.*/\u&/" or ":%s/./\u&/g", but the first command will be faster.

(14) Reverse the order of the lines in the file:": G/.*/mo 0". The command moves each row to the beginning of the file in the order of the file rows, in turn, to reverse the order of the file rows. where " . *" will match the complete line,mo is the move command in the ex editor, you can refer to the Learning VI and VIM editor (6): Ex Editor overview (1). Alternatively, ": G/^/mo 0" can also be used .

(15) Add end to the end of all lines that do not contain line words: ": g!/line/s/$/end/". To affect all rows except the matching pattern, you can add an exclamation point directly after the G command, or use the V command directly, so the following command can also be implemented: ": v/line/s/$/end/".

(16) Delete the number at the beginning of each line:

To move a block of text in a pattern:

as previously seen, the ": G" command can be used in conjunction with ":d", "Mo", "Co" and other ex commands. You can also use patterns as boundaries of text blocks. Move the file contents of chapter one to the end of the file as shown:


This command searches for the row where chapter one is located (that is, the header row of the first chapter) and moves all rows between the row and the previous row of chapter two rows (thus marking the entire first chapter of the text block) to the end of the file.

The pattern matches the last exhortation:

(1) When matching any number of arbitrary strings, the regular expression always matches as much text as possible;

(2) When we rethink a matching pattern, it is usually better to modify the variable (metacharacters) more precisely than to constrain the pattern with specific text. The more variables are used in the pattern, the stronger the command power is.

(3) Sometimes it is more difficult to specify the ideal result precisely than the specified result.

With: G Repeat command:

The G command can select some rows, but the accompanying editing commands do not necessarily affect the rows of the selections. as shown, the command can copy lines 11th through 12th of the file to the end of the file and repeat the copy behavior 10 times.


Explain the behavior of the command: the "1,10g" representative will perform the search behavior on lines 1th through 10th of the file, because the search pattern is just a "^", so each row conforms to the search pattern. When the first row is selected, the following editing commands are executed, the 11th to 12th lines of the file are copied to the end of the file (the edit command has no relation to the currently selected first row), and then the second row is selected, then the subsequent editing commands are executed, so a total of 10 edits are performed to achieve the effect of repeating the command with:

Collect rows:

As shown, in a source file, if you want to extract the function prototype directly from the definition of all functions and import it into a header file, you can do so with the following command:

This command works by first searching through the entire file, searching for each line that begins with "{", writing the previous line of the row to the Collect_line.h file, thus achieving the effect of writing the function prototype to the header file. This example is my own, and there is no such example in the original book.


This chapter VI command summarizes:

This chapter focuses on the substitution commands and regular expressions learned from the previous article. Regular expressions have a strong skill, and only by constantly thinking and trying, can we grasp the regular expression in a solid way, and find a quicker way to edit text.



Learning VI and VIM editor (9): Global Substitution (2)

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.