Vim (5): Global substitution

Source: Internet
Author: User

Vim Global Replacement file:

The syntax is: [addr]s/source string/destination string/[option]the Global Substitution command is::%s/source string/destination string/g
[addr] Indicates the scope of the search, which represents the current row when omitted. such as: "1,20": the line from line 1th to 20, "%": represents the entire file, with "1,$"; "., $": from the current line to the end of the file; s: Represents the Replace Operation [ Option]: Indicates the type of operation , such as: g means global substitution; c means to confirm p indicates that the substitution results are displayed line by row (Ctrl + L recovery screen); when option is omitted, only the first matching string of each row is replaced; if special characters appear in the source string and the destination string, you need to escape with "\"
Here are some examples: #将That or this is replaced with this or so
:%s/\ (that\) or \ (this\)/\u\2 or \l\1/
-- < br> #将句尾的child换成children
:%s/child\ ([,.;!:?] \)/children\1/g
--
#将mgi/r/abox change to Mgi/r/asquare
: g/mg\ ([ira]\) box/s//mg//my\1square/g     <=> : g/mg[ira]box/s/box/square/g
--
#将多个空格换成一个空格
:%s/  *//g
--
# Use a space to replace the period or one or more spaces following the colon
:%s/\ ([:.] \)   */\1/g
--
#删除所有空行
: g/^$/d
--
#删除所有的空白行和空行
: g/^[ ][ ]*$/d
--
# Insert two blanks at the beginning of each line
:%s/^/> /
--
#在接下来的6行末尾加入.
:., 5/$/./
--
#颠倒文件的行序
:g/.*/m0o  < + =: g/^/m0o
--
#寻找不是数字的开始行 and move it to the end of the file
: g!/^[0-9]/m$ <=> g/^[^0-9]/m$
--
# Copy the 12th to 17th line of the file 10 words to the end of the current file
: 1,10g/^/12,17t$
~ ~ ~ ~ ~ ~ ~ ~
#将chapter开始行下面的第二行的内容写道begin文件中
: g/ ^chapter/.+2w>>begin
--
:/^part2/,/^part3/g/^chapter/.+2w>>begin
--
:/^part2/,/^part3/ g/^chapter/.+2w>>begin|+t$

Vim (5): Global substitution

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.