


Usage of global replacement command in vim editor
Aug 22, 2017 pm 02:11 PMThe 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!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to run SUDO commands in Windows 11/10

Essential software for C language programming: five good helpers recommended for beginners

How to check the MAC address of the network card in Win11? How to use the command to obtain the MAC address of the network card in Win11

Where is hyperv enhanced session mode? Tips for enabling or disabling Hyper-V enhanced session mode using commands in Win11

Super practical! Sar commands that will make you a Linux master

What is the correct way to restart a service in Linux?

How to use LSOF to monitor ports in real time

Detailed explanation of VSCode functions: How does it help you improve work efficiency?
