Summary of VIM replace search commands

Source: Internet
Author: User
Tags repetition
1. the simple replacement expression replacement command can replace another word with one word in the full text: % s/four/4/g "%" range prefix to execute replacement in all rows. The last "g" Mark replaces all matching points in the row. If you only operate on the current row, you only need to remove %. if you have a ticket like "thirtyfour ",

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 a matching word:

: % S/\ "to solve this problem:

: % S/\/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/\/4/gc

2. delete unnecessary spaces.

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

: % S/\ 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 "\ (", followed by "\)" before the component. 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 ".

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 a matching word:
: % S/\ "to solve this problem:
: % S/\/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/\/4/gc

2. delete unnecessary spaces.
To delete unnecessary spaces behind each line, run the following command:
: % S/\ 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 "\ (", followed by "\)" before the component. 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.