Vi and vim Editor (9): Global replacement (2)

Source: Internet
Author: User

Vi and vim Editor (9): Global replacement (2)

The previous article has learned how to use the ": s" command in the vi editor to perform global replacement. In addition, you can use regular expressions in the replacement command to perform powerful pattern matching. This article will learn more examples of pattern matching to become more familiar with regular expressions.

Example of pattern matching:

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

However, this command will replace the two words childrendish and Fabrichild, which are not what we want. The key to replacement is that if child is a separate word, the following methods may be taken into consideration:


This command searches for the child ending with a space, comma, and semicolon, replaces the child with children, and retains the symbols after the child. However, this command will still fail because Fabrichild will also be replaced.

In fact, it is very simple. Combined with \ <and \>, we can match a single complete word. The following command can complete the replacement work we want:

(2) Search for Common Word classes:As shown in, you need to replace all the boxes in mgibox, mgrbox, and mgabox with square. The following method can be used: <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20141231/2014123110070846.png" alt = "\">

The special metacharacters used in this command are all explained in the previous article. Here we will not explain them one by one.

(3) Replace the file path:As shown in, you need to replace/home/test/with/usr/local. Because "/" has a special meaning in the replacement command (as a separator for each part of the replacement command ), therefore, you must replace it as follows:


As mentioned in the previous article, apart from using "/" as the separator, you can also use other characters as the separator, as shown in, using ";" as the separator to replace each part of the command, the entire replacement command can be simpler:

(4) change all periods between 1st and 10th to semicolons:": 1, 10 s /\. //;/g ", because ". "It has a special meaning in the replacement string, so you need to use the" \ "Escape Character to convert it to a normal period.

(5) convert the displayed help or Help into HELP:": % S/[hH] elp/\ U &/g ".

(6) replace one or more spaces with one space:": % S/* // g". Note that there are two spaces before "*" in the search mode to match one or more spaces.

(7) delete all blank rows:": G/^ $/d", "^ $" can match rows with no content in the middle, that is, blank rows. Note that lines that only contain spaces or tab keys do not delete these lines, even though they are blank. To delete blank rows (rows without any content) and all rows that only contain spaces or tabs, run the following command :": g/^ [tab] * $/d ".

(8) delete all spaces starting with each line:": % S/^ * \ (. * \)/\ 1/". Note that there are two spaces in front of the first "*" to match one or more spaces.
(9) delete all spaces at the end of each row:": % S/\ (. *) \ * $/\ 1/", also note that there are two spaces in front of the last.

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


Note that the preceding command uses ";" as the separator and uses ".., + 3" to specify the comment range, that is, the current line and the subsequent three lines of code. In fact, there is a simpler way:


This command replaces the beginning of a line (^) with "//", "^" indicates the beginning of a line (this is a logical concept and is not an actual character, therefore, the actual first character of the line will not be replaced ).

(11) add a full stop at the end of all lines of the file:": % S/$ /./".

(12) reverse the order of all parts separated by hyphens:


(13) convert all words in the file into uppercase letters:": % S/. */\ U &/" or ": % s/./\ U &/g", but the first command is faster.

(14) reverse the order of rows in the file:": G/. */mo 0 ". This command moves each line to the beginning of the file in sequence according to the order of the file lines, so as to reverse the order of the file lines. Among them, ". *" will match the complete line. mo is the mobile command in the ex editor. Refer to learning vi and vim Editor (6): ex editor Overview (1). You can also use ": g/^/mo 0 ".

(15) add end:": G! /Line/s/$/end /". To affect all lines except the matching mode, you can add an exclamation point after the g command or directly use the v command. Therefore, the following command can also be implemented :": v/line/s/$/end /".

(16) Delete the numbers starting with each line:

Move text blocks in Mode:

As we can see earlier, the ": g" command can be used together with the ": d", "mo", "co" and other ex commands. You can also use the mode as the dividing text block. As shown in, move the content of chapter one to the end of the file:


This command is used to search for the row where chapter one is located (that is, the header line in chapter 1 ), and move all rows between the row and the last row of the row where chapter two is located (marking the entire text block in chapter 1) to the end of the file.

The last rule of pattern matching:

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

(2) When we rethink a matching pattern, it is usually better to modify the variable (metacharacters) more accurately than to limit the pattern with specific words. The more variables are used in the mode, the more powerful the command is;

(3) it is more difficult to specify an ideal result accurately than to specify an unwanted result.

Repeat the command with: g:

You can select certain lines for the g command, but the editing command does not have to affect the selected lines. As shown in, this command can copy lines 11th to 12th of the file to the end of the file, and repeat 10 such copies.


Explain how the command works: "1st GB" indicates that the search will be performed in lines 10th to of the file. Because the search mode is only a "^ ", therefore, each row matches the search mode. When the first line is selected, run the edit command to copy lines 11th to 12th of the file to the end of the file (the edit command has nothing to do with the first line selected currently ), then select the second line and run the edit command again. Therefore, a total of 10 edit commands will be executed to achieve the effect of repeating the command with: g.

Collection row:

As shown in, in a source file, if you want to extract the function prototype and import it to a header file based on the definition of all functions, you can run the following command:

This command first performs a line-by-line search on the entire file, searches for each row starting with "{", and writes the previous row to the collect_line.h file, this allows you to write the function prototype to the header file. This example was created by myself. It is not found in the original book.

Summary of the vi commands in this chapter:

This chapter describes the replacement commands and regular expressions learned in the previous article. Regular Expressions are very skillful. Only by constantly thinking and trying can you master regular expressions in a solid manner, so as to find a faster way for text editing.


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.