[Shell] Batch Replace command vim practice

Batch Replace command vim practice

 

The syntax is 

: [Addr] s / String source / destination string / [Option]

Global Replace command is:

:% S / String source / destination string / g

 

[Addr] representing the search range, indicating the current row is omitted.

"1,20": indicates the line 1 to line 20;

"%": Represents the entire file, with "1, $";

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

 

s: indicates the replace operation

 

[Option]: indicates the type of operation

g represents a global replacement; 

c represents confirmation

p represents alternative results Progressive (Ctrl + L restore the screen);

Only for each line when a match string replacement option is omitted;

If the special characters in the source string and a string object, it is necessary with the "\" escape as \ t

 

Here are some examples:

==================================================

#将That or this 换成 This or that
:%s/\(That\) or \(this\)/\u\2 or \l\1/


================================================== =====
# end of the sentence the child into Children
:% S / child \ ([,;:.!?] \) / Children \ 1 / G


================================================== =====
# the mgi / r / abox replaced MGI / R & lt / asquare
: G / mg \ ([IRA] \) Box / S // // My mg \ 1square / <=> G: G / mg [ira] box / s / box / square / g


================================================== =====
# plurality of spaces into one space
:% s / * / / g


================================================== =====
# spaces replacing one or more spaces of a period or the colon
:% s / \ ([: .] \) * / \ 1 / g


================================================== =====
# deletes all blank lines
: g / ^ $ / d


================================================== =====
# deletes all blank lines and blank lines
: g / ^ [] [] * $ / d


================================================== =====
# inserted at the beginning of each row of the two gaps
:% s / ^ /> /


================================================== =====
# 6 added to the end of the next line.
:., 5 /$/./


================================================== =====
line reverse order # file
: g /.*/ m0O <=>: g / ^ / m0O


================================================== =====
# seek is not a number of the start line, and moved to the end of the file
:! g / ^ [0-9] / m $ <=> g / ^ [^ 0-9] / m $


================================================== =====
# 12-17 will be the first line of the file to copy the contents of 10 words into the current end of the file
: 1,10g / ^ / 12,17t $
role of repetitions ~ ~ ~ ~


================================================== =====
# the contents of the second row of the start line following chapter wrote begin file
: g / ^ chapter /.+ 2w >> begin


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


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

 

Reprinted from: https://www.cnblogs.com/beenoisy/p/4046074.html

Guess you like

Origin blog.csdn.net/zkq_1986/article/details/90231710