Vim global replacement command

Source: Internet
Author: User
Syntax: [ADDR] S/source string/destination string/[Option] Global replacement command: % S/source string/destination string/g
[ADDR] indicates the search range. If it is omitted, it indicates the current row. For example, "1st" indicates the number of rows from to 20; "%": Indicates the entire file, which is the same as "1, $ "; "., $": From the current row to the end of the file; S: indicates the replacement operation. [Option]: indicates the operation type. For example, G indicates global replacement; C Indicates confirmation P indicates that the replacement result is displayed row by row (CTRL + l restore the screen ); If option is omitted, only the first matching string in each line is replaced; If special characters appear in the source string and target string, escape them "\"
The following are some examples: # Replace that or this with this or that.
: % S/\ (that \) or \ (this \) /\ U \ 2 or \ L \ 1/
--
# Replace child at the end of a sentence with children
: % S/child \([,.;! :?] \)/Children \ 1/g
--
# Change MGI/R/abox to MGI/R/asquare
: g/Mg \ ([Ira] \) box/S // mg // my \ 1 square/g <=>: g/mg [Ira] box/S/box/square/g
--
# Replace multiple spaces with one space
: % S/* // G
--
# use a space to replace one or more spaces after a period or a colon
: % S/\ ([:.] \) */\ 1/g
--
# delete all empty rows
: g/^ $/d
--
# delete all blank and empty rows
: g/^ [] [] * $/d
--
# insert two blank spaces at the beginning of each line
: % S/^/>/
--
# Add at the end of the next six rows.
:., 5/$ /. /
--
# reverse the object's row order
: G /. */m0o <=>: G/^/ M0o
--
# search for 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 $
--
# 12th to 17 lines of the file copy 10 words in the content to the end of the current file
: g/^/T $
~~~~ Function of repeated Times
--
# Write the content of the second row in the beginning row of chapter in the begin file
: G/^ chapter /. + 2 W> begin
--
:/^ Part2/,/^ Part3/G/^ chapter /. + 2 W> begin
--
:/^ Part2/,/^ Part3/G/^ chapter /. + 2 W> begin | + T $

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.