[Linux] Vim's highlight lookup operation

Source: Internet
Author: User

Reference:http://blog.chinaunix.net/uid-20732478-id-763411.html

The use of vim for so long, but has been unable to remember some basic operating instructions. Today when looking for a keyword, can't think of how to find "next", so Google and solve, by the way to paste the useful all over.

Find directives:
/xxx Down Search
? XXX go up
N Next
: Set HLS Open Highlight
: Set Nohls off highlighting

Here is the find and replace, although I have not used this feature yet--
Because there are so many online, I don't know who the original is.

1, simple substitution expression

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

:%s/four/4/g

The "%" range prefix indicates that substitutions are performed on all rows. The last "G" tag represents all the matching points in the replacement row. If you are only working on the current line, just remove the%

If you have a word like "thirtyfour", the above command will go wrong. In this case, the word will be replaced with "Thirty4″." To solve this problem, use "\<" to specify the beginning of the matching word:

:%s/

Obviously, this will still make a mistake when dealing with "fourty". Use "\>" to solve this problem:

:%s/\/4/g

If you are coding, you may just want to replace "four" in the comment, while preserving the code. Since this is difficult to specify, you can add a "C" tag to the replacement command so that Vim prompts you before each replacement:

:%S/\/4/GC

2. Remove extra spaces

To remove the extra space behind each line, you can execute the following command:

:%s/\s\+$//

The command indicates that the range is "%", so this works on the entire file. The match pattern for the "substitute" command is

"\s\+$". This represents one or more (\+) spaces (\s) before the end of the line ($). The "to" part of the Replace command is empty: "//". This will delete those matching whitespace characters.

3, matching repeatability mode

The asterisk item "*" Specifies that items in front of it can be repeated any time. So:

/a*

Match "A", "AA", "AAA", and so on. But also matches "" (empty string), because 0 times is also included. The asterisk "*" is applied only to the item that is immediately in front of it. So "ab*" matches "a", "AB", "ABB", "abbb", and so on. To repeat the entire string more than once, the string must be made up of an item. The way to form an item is to precede it with "\ (", followed by "\"). So this command:

/\ (ab\) *

Matches: "AB", "Abab", "Ababab", and so on. and also match "".

To avoid matching empty strings, use "\+". This means that the preceding item can be matched one or more times.

/ab\+

Match "AB", "ABB", "abbb", etc. It does not match "a" followed by "B".

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

/folders\=

Match "folder" and "Folders".

4, specify the number of repetitions

To match a specific number of times for an item, use the form "\{n,m}". where "n" and "M" are numbers. The item in front of it will be repeated "n" to "M" Times (|inclusive| contains "n" and "M"). For example:

/ab\{3,5}

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

When "n" is omitted, it is defaulted to zero. When "M" is omitted, the default is infinity. When ", M" is omitted, it means repeating exactly "n" Times. For example:

Pattern Match times

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

\{3,} 3,4,5, etc.

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

\{0,} 0 or more, same as *

\{1,} 1 or more, with \+

\{3} 3

5, multiple Select a match

In a lookup mode, the OR operator is "\|". For example:

/foo\|bar

This command matches "foo" or "bar". More choices can be attached to the back:

/one\|two\|three

Matches "One", "one", or "three".

To match its multiple repetitions, the entire decision structure must be placed between "\ (" and "\)":

/\ (foo\|bar\) \+

This command matches "foo", "Foobar", "Foofoo", "Barfoobar", and so on.

Let me give you an example:

/end\ (if\|while\|for\)

This command matches "endif", "Endwhile" and "ENDfor".

[Linux] Vim's highlight lookup operation

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.