Home > Operation and Maintenance > Linux Operation and Maintenance > Usage of global replacement command in vim editor

Usage of global replacement command in vim editor

巴扎黑
Release: 2017-08-22 14:11:53
Original
3394 people have browsed it

The syntax is:[addr]s/source string/destination string/[option]

The global replacement command is::%s/source string/destination string/g

[addr] indicates the search range, and when omitted, it indicates the current line.

For example: "1,20": indicates from line 1 to line 20;

"%": indicates the entire file, the same as "1,$";

".,$": from the current line to the end of the file;

s: represents the replacement operation

[option]: represents the operation type

For example: g represents global replacement;

c means to confirm

p means that the substitution results are displayed line by line (Ctrl + L restores the screen);

When option is omitted, only the first matching string in each line is Replacement;

If special characters appear in the source string and destination string, they need to be escaped with "\"

The following are some examples:

#Replace That or Replace this with This or that

:%s/\(That\) or \(this\)/\u\2 or \l\1/

—-

#Replace child at the end of the sentence with children

:%s/child\([ ,.;!:?]\)/children\1/g

—-

#Replace mgi/r/abox with mgi/r/asquare

:g/mg\([ira]\)box/s//mg//my\1square/g <= > :g/mg[ira]box/s/box/square/g

—-

#Replace multiple spaces into one space

:%s/ */ /g

—-

#Use spaces to replace one or more spaces after a period or colon

:%s/\([:.]\) * /\1 /g

—-

#Delete all blank lines

:g/^$/d

—-

# Delete all blank lines and blank lines

:g/^[ ][ ]*$/d

—-

#Insert two blanks at the beginning of each line

:%s/^/> /

—-

#Add at the end of the next 6 lines.

:.,5/$ /./

—-

#Reverse the line order of the file

:g/.*/m0O <=> :g/^/m0O

—--

#Find the starting line that is not a number and move it to the end of the file

:g!/^[0-9]/m$ <=> g/^[^0-9]/m$

—-

#Copy 10 words from lines 12 to 17 of the file and put them at the end of the current file

:1,10g/^/12,17t$

~~~~The role of the number of repetitions

——-

#Place the second line below the chapter start line The content is written in the begin file

:g/^chapter/.+2w>>begin

—-

:/^part2/,/^part3/ g/^chapter/.+2w>>begin

—-

:/^part2/,/^part3/g/^chapter/.+2w>>begin|+ t$

The above is the detailed content of Usage of global replacement command in vim editor. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template