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

Usage of global replacement command in vim editor

Aug 22, 2017 pm 02:11 PM
Order usage editor

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!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to run SUDO commands in Windows 11/10 How to run SUDO commands in Windows 11/10 Mar 09, 2024 am 09:50 AM

How to run SUDO commands in Windows 11/10

Essential software for C language programming: five good helpers recommended for beginners Essential software for C language programming: five good helpers recommended for beginners Feb 20, 2024 pm 08:18 PM

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 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 Feb 29, 2024 pm 04:34 PM

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 Where is hyperv enhanced session mode? Tips for enabling or disabling Hyper-V enhanced session mode using commands in Win11 Feb 29, 2024 pm 05:52 PM

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 Super practical! Sar commands that will make you a Linux master Mar 01, 2024 am 08:01 AM

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

What is the correct way to restart a service in Linux? What is the correct way to restart a service in Linux? Mar 15, 2024 am 09:09 AM

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

How to use LSOF to monitor ports in real time How to use LSOF to monitor ports in real time Mar 20, 2024 pm 02:07 PM

How to use LSOF to monitor ports in real time

Detailed explanation of VSCode functions: How does it help you improve work efficiency? Detailed explanation of VSCode functions: How does it help you improve work efficiency? Mar 25, 2024 pm 05:27 PM

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

See all articles