VIM Regular Expression Search string __ Regular expression

Source: Internet
Author: User


In insert mode, to reduce repetitive keystroke input, VIM provides a number of shortcuts, and when you enter a string that has been entered in a context, you just enter the first few characters, use the shortcut key, VIM will search the context, find the matching string, and fill the remaining characters, you don't have to knock. So, it doesn't matter how long you play the variable names when you're coding,:-). You can also reduce input errors. I think, insert complement is the most outstanding function of VIM.

I<c-p> search up, fill a word. For example, the word filename appears above, and when you want to enter filename again, just press f<c-p>. If VIM goes up to search, find the first match that starts with F is not filename, you can continue to press <C-P> search for the next match to complete the completion. Of course, if you want to <C-P> success, you can enter a few more characters such as Filen and then press <C-P> fill

I<c-n> down search, fill a word

I<c-x><c-l> a full line. For example, you wrote a line for (int i = 0; i < i++), you want to write exactly the same line, just enter the for<c-x><c-l>. If the completion is not the line you want, you can press <C-P> or <C-N> Select the previous or next matching row
I<c-x><c-f> Search in file system, complete a filename

If you press <C-P> or <C-N> to complete a word and no match is found in the current file, VIM searches for the file in the #include statement, and the location of the file is searched in path.

The search string is in a regular expression (Regular expression), many of which have special meanings:
\        The special meaning of the following characters to be canceled. For example, the \[vim\] match string "[Vim]"
[]       matches one of them. For example, [vim] matches the letter "V", "I", or "M", [a-za-z] matches any letter
[^]      match is not one of them. For example, [^vim] matches all characters except the letter "V", "I" and "M"
.        matches any character
*         match the previous character is greater than or equal to 0 times. For example vi*m match "VM", "Vim", "Viim" ... The
\+       match is greater than or equal to the previous character. For example vi\+m match "Vim", "Viim", "Viiim" ... The
\?       matches the previous character 0 or more times. For example, vi\?m matches the "VM" or "vim"
^        matches the beginning of the line. For example,/^hello finds the word Hello
$        that appears at the beginning of the line matches the end of the row. For example/hello$ find words that appear at the end of the line Hello
\      Surround a regular expression
\ number     Repeat the expression enclosed in the previous paragraph. For example \ (hello\). *\1 matches a start and end are "Hello", and the middle is a string of any string

For a replacement string, you can use "&" to represent the entire search string, or use "\ Number" to represent an expression enclosed in a paragraph in the search string.

To give a complex example, replacing all the string "abc......xyz" in the text with "Xyz......abc" can be written in the following notation:
:%s/abc\ (. *\) xyz/xyz\1abc/g
:%s/\ (abc\) \ (. *\) \ (xyz\)/\3\2\1/g
Other more detailed and accurate descriptions of regular expression search substitutions: Help pattern

For example, search the text for all rows containing amount greater than 0 with a [] enclosed string , such as "amount[123", "amount[200]", and so on:

First press: Enter command mode , then enter the following string and return to start looking:/amount\[[1-9]\ ([0-9]*\) \+\]

Explained as follows:

/means serial search, other words Fu Weizheng the content of the expression

Amount indicates that the matching string contains amount

\[escape character, representing the matching left bracket [

[1-9] indicates a match between a 1 to 9 of any number

\ (\) escapes the left and right brackets, indicating that a section of regular expressions is enclosed,

\+ escape character +, which indicates that the preceding character or a regular string is repeated 1 or more times, so \ ([0-9]*\) represents a number between any 0-9

\] Escape character]

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.