Vim search and replace summary

Source: Internet
Author: User
Tags repetition

Summary of common replacement modes in vim.

0,: G/null/d

Locate the null row and delete it.

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:

/*

Match "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 *" matches "A", "AB", "ABB", and "abbb. To repeat the entire string multiple times, the string must be an item. Add "/(" before it and add "/)" after it. 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.