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 confirming P indicates that the replacement result is displayed row by row (CTRL + l restore 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:  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 spaces to replace one or more spaces after the ending periods or colons
: % S/\ ([:.] \) */\ 1/g
--
# Deleting 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/$ /./
--
# Reversing the object's line 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 $
--
# Copy 10 words from lines 12th to 17 of the file to the end of the current file
: G/^/T $
~~~~ Function of repeated times
--
# Write the content of the second row below the beginning of chapter to 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.