Quick Find and Replace in vim

Source: Internet
Author: User
Tags uppercase letter

Vim is known as very efficient text editing software. But mastering and efficient use is a difficult thing to do. Fast search and Replace in Vim is an important way to improve the efficiency of vim. Here are some of the things I've collated when I read the VIM user manual:

  1. In-line search.
    1. The f command allows in-line search. Enter FX to find the next X character.
    2. The f command allows in-line search in the opposite direction, and the input FX can find the previous X character.
    3. The T command is also an inline search, but the cursor stays in front of the character that matches the condition. Enter TX to keep the cursor in front of the next X character.
    4. The T command allows in-line search in the opposite direction, but the cursor rests on the next character of the character that matches the condition. Enter TX so that the cursor rests on the character following the previous x.
    5. % can be searched for the corresponding () []{} that matches it. This feature is especially useful for writing programs.
  2. Full-Text search. After typing/, the cursor enters the command line at the bottom of vim, and you can enter/start/search commands.
    1. The simplest to find. Enter/string to find string strings.
    2. Continue to find. Entering n after the first search will continue with the last search, and if you enter 3n then you will find the third string that follows. uppercase n is searched in the opposite direction, that is, up.
    3. Search up. Enter to search up. The subsequent n command will continue to search up, n search down, and/or.
    4. Match the case.
      1. On the command line input: Set ignorecase can set the search to ignore case. Input: Set Noignorecase can set the search to match the case. By default, the match is case. This default setting can be modified in. vimrc.
      2. Casing Intelligent Matching mode. Input command: Set ignorecase smartcase can be set to smart case matching mode. In this mode, if you enter a string that contains at least one uppercase letter, the search is done in case-sensitive mode, otherwise the search is ignored in case-insensitive mode.
      3. Specify the case in the search command. Converting in several case search modes always enter a long string of instructions, which is a bit of a hassle if you need to constantly convert patterns. So you can specify the case matching pattern directly in the search command. \c indicates that case is ignored and \c indicates case sensitivity.
    5. Finds the current word. A simple way to find the next word that is the same as the current word, you do not enter/currentword, you can simply press * to find the next Currentword. #可以让你向上查找同一个单词.
    6. Adjusts the position of the cursor after the lookup.
      1. Using/STRING/3 causes the cursor to go to line 3rd below the line where the string is found. The 1th line counts from the line that contains the string. You can also use/string/-2 to keep the cursor in the first 2 rows of the line containing the string.
      2. Use/string/e to keep the cursor at the end of a string string instead of the default first character position. /string/b the position of the first character of the string, but by default, we are no more than superfluous. But/string/b+2 can keep the cursor in the position of the second character of the string, which is R. At the back of E or B + or-number, you can further adjust the position of the cursor.
    7. Special characters. In the search command, the. *[]^%/?~$ 10 characters have a special meaning, so when using these characters, precede with a backslash/. And \e said <esc>;\t said <tab>;\r said that <cr>;\b represents <bs>.
    8. Match line breaks or spaces. You can use \ n to represent a newline, and \s to match whitespace, and note that whitespace is not a space. \_s matches a newline or a space; the \_a represents a match for a newline or a letter. For example:/the\nword finds the position of the line that ends with the and the next line begins with Word. /the\_sword find the location between the and word separated by a blank or newline. /the\_s\+word indicates that there can be more than one white space between the word. The meaning of \+ can be found later.
    9. The first or final word of a qualifier. \< can be qualified to find words that begin with the specified string. For example,/\<the can find the string that begins with the string, but ignores the word that contains the text in the middle. The \> is a word that must be terminated with the specified string.
    10. The beginning and end of the line. Enter/^string to find the line starting with string, and the cursor rests on the first character of the string. Enter/string$ to find the line ending with string and rest the cursor at the first character of the string. This mode does not ignore leading or trailing spaces.
    11. Match any one character. Period. You can match any one character. For example/T.E can find the either tae or Tue.
  3. More complex pattern of matching. In fact,/search can use very complex matching patterns. Some of these complex matching patterns are listed below.
    1. A repeatable character.
      1. Use * to indicate that the characters in front of it can be repeated or 0 times. such as/be* can match b,be,bee,beee and so on. But this will also contain B, because E repeats 0 after it is empty. If you want to match strings such as Be,bebe,bebebe, you can use \ (and \) to include them, such as:/\ (be\) *.
      2. Use \+ to qualify for repetition at least once to countless times. Such as:/be\+ can match Be,bee,bee and so on.
      3. Repeat 0 or one time. \= can specify 0 repetitions or one time. The/strings\= represents a matching string or strings. This is especially useful in finding the plural form of a word.
      4. Specifies the number of repetitions. \{N,M} can be specified to repeat n to M times. For example:/be\{2,4} will match bee,beee,beeee. You can also use \{,4} to indicate a match of 0 to 4 times, \{4,} for 4 times to countless times, and \{4} to repeat 4 times.
    2. Match one of the list. Using a \| split list, you can represent any of the items in a matching list. For example,/one\|two indicates that finding one or two;/one\|two\|three means finding one or both or three.
      1. A complex example:/end\ (if\|while\|for\) will look for Endif,endwhile and endfor.
      2. A list of characters. Use [0-9] to represent a range of characters from 0 to 9. such as/string[1-5] means to find STRING1,STRING2,STRING3,STRING4,STRING5. Of course, you can also use [A-z] to represent the middle of the character A to Z.
      3. Special set of characters. Using \d can also represent numbers from 0 to 9, which can be substituted for [0-9]. \d indicates that a non-numeric substitution [^0-9];\x means a hexadecimal number instead of [0-9a-fa-f];\x denotes a non-hexadecimal number instead of [^0-9a-fa-f];\s denotes a whitespace character instead of [] (<Tab> and <Space>); s denotes non-whitespace characters instead of [^] (except <Tab> and <Space>); \l means lowercase instead of [a-z];\l for non-lowercase letters instead of [^a-z];\u for uppercase instead of [a-z];\u for non-uppercase] instead of [^a-z ]
  4. Search and Replace. Input: The [range]s/from/to/[flags] command can search for a matching string and replace it with the specified string. This is a slightly more complex command. Where [Range] is an option to specify the scope of the search substitution. [Flags] is also an optional option to specify how the search substitution is handled. So the simple form of this command is: s/from/to/.
    1. Search for a range of replacements. If no range is specified, search is replaced only in the current row.
      1. The search is replaced on all lines. The range symbol% represents a search substitution on all rows. :%s/from/to/is the full-text lookup from and replaced with to.
      2. Replaces the search on the specified line. : 1,50s/from/to/means searching and replacing between lines 1th and 50th, including rows 1 and 50. : 45s/from/to/means searching and replacing only on line 45th. The "1,$" line number range and "%" are equivalent.
    2. Processing mode. The default mode of Vim I use now is no need to confirm. However, it seems that different versions of the default processing methods are different.
      1. No confirmation is required. The way symbol g means that direct substitution does not require confirmation. :%s/from/to/g indicates that the from is found in the full text and replaced directly with to.
      2. Print. The processing mode symbol p indicates that each changed row is listed at the time of substitution. The manual says so, but the actual effect is confusing to me.

Quick Find and Replace in vim

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.