Vim replacement function

Source: Internet
Author: User
Tags repetition
1. Substitute)

: [Range] S/pattern/string/[C, E, G, I] 5
. 1

 

Range It refers to zookeeper. 1, 7 refers to the entire article from the first row to the seventh row, and 1, $ refers to the entire article from the first row to the last line. It can also be represented by %. Remember? % Is the current article of zookeeper, # Is the previous article of zookeeper.
Pattern It is the string to be replaced, which can be expressed by Regexp.
String Replace pattern with string.
C Confirm, which will be asked before each replacement.
E No error is reported.
G Globe. If this parameter is left blank, the entire row is replaced by a limit.
I Ignore is not large or small.

G must be added. Otherwise, it will only match the first line of each line. It can be used in combination. For example, CGI indicates that the whole line is replaced by a shard, regardless of the size. You need to ask whether to replace the shard before replacing the shard.

 

[Example]: % S/Edwin/Edward/g

In this way, the Edwin of the entire article will be replaced by Edward.

In addition, the example of searching for and replacing rules will be described again when Regexp is explained. Currently, you only need to know the most basic usage! In fact, this is very useful. :-)

 

 

 

 

2. Replacement instructions (example)

 

 

 

Summary of common replacement modes in vim.

1. Simple replacement expression

The replacement command can replace another word with one word in the full text:

: % S/four/4/g

The prefix of "%" indicates that replacement is performed in all rows. The last "G" Mark replaces all matching points in the row. If you only operate on the current row, remove %.

If you have a word like "thirtyfour", the above command will fail. In this case, the word is replaced with "thirty4 ″. To solve this problem, use "/<" to specify the start of the matching word:

: % S // <four/4/g

Obviously, errors will still occur when processing "fourty. Use "/>" to solve this problem:

: % S // <four/>/4/g

If you are coding, you may just want to replace the "four" in the comment, but keep the "four" in the code. Since this is difficult to specify, you can add a "C" mark in the replacement command, so that Vim will prompt you before each replacement:

: % S // <four/>/4/GC

2. Delete unnecessary spaces.

To delete unnecessary spaces behind each line, run the following command:

: % S/+ $ //

The command specifies that the range is "%", so this will apply to the entire file ." The matching mode of the substitute command is

"/S/+ $ ". This indicates one or more (/+) spaces (/s) before the end of the line ($ ). The "to" part of the replacement command is empty :"//". In this way, the matching spaces are deleted.

3. Matching repetition Mode

The asterisk (*) specifies that the items before it can be repeated at any time. Therefore:

/*

Horse
With "A", "AA", "AAA", and so on. But it also matches "" (Null String), because zero times are also included. The asterisk "*" is only applied to the item next to it. Therefore
"AB *" Matching
"A", "AB", "ABB", "abbb", and so on. To repeat the entire string multiple times, the string must be an item. The method to make up one is to add
"/(", Followed by "/)". Therefore, this command:

// (AB /)*

Matching: "AB", "Abab", "ababab", and so on. It also matches "".

To avoid matching null strings, use "/+ ". This indicates that the previous item can be matched once or multiple times.

/AB/+

Match "AB", "ABB", "abbb", and so on. It does not match "A" that follows "B ".

To match an option, use "/= ". For example:

/Folders/=

Matches "folder" and "Folders ".

4. specify the number of repetitions

To match a specific number of times of repetition, use the form "/{n, m. "N" and "m" are numbers. The item before it will be repeated "N" to "M" Times (| aggressive sive | contains "N" and "m "). For example:

/AB/{3, 5}

Match "abbb", "abbbb", and "abbbbb ".

When "N" is omitted, It is 0 by default. When M is omitted, It is infinitely large by default. When ", M" is omitted, it indicates that the repetition is exactly "N" times. For example:

Number of pattern matches

/{, 4} 0, 1, 2, 3, or 4

/{3,} 3, 4, 5, and so on

/{0, 1} 0 or 1, same as/=

/{0,} 0 or more, the same *

/{1,} 1 or more, same as/+

/{3} 3

5. Select one or more matches.

In a search mode, the "or" operator is "/| ". For example:

/Foo/| bar

This command matches "foo" or "bar ". More options can be connected to the following:

/One/| two/| three

Match "one", "two", or "three ".

To match multiple times, the entire decision structure must be placed between "/(" and:

// (FOO/| bar/)/+

This command matches "foo", "foobar", "foofoo", "barfoobar", and so on.

Another example:

/End/(if/| while/| /)

This command matches "endif", "endwhile", and "endfor ".

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.