VI search and replacement

Source: Internet
Author: User
Use vi -- use VI to find and replace

VI provides several methods to locate a specified string in a file. It also provides a powerful global replacement function.

1. Search for a string

A string contains one or several characters in a row.
To find a string, type "/" in VI command mode, followed by the string to be searched, and press Enter. VI positions the cursor on the next occurrence of the string. Type N to jump to the next occurrence of the string, and type N to jump to the previous occurrence of the string.
To look back at the file, use? Replace /. In this case, enter n to jump to the previous occurrence of the string, and enter n to jump to the next occurrence of the string.
If VI finds the required string, the cursor stays at the first place where the string appears. If this string is not found, VI will display pattern not found in the last line of the screen.
Search is usually case sensitive. If you want VI to ignore the case when searching, type: Set IC. To change it back to the default status, type: Set noic.
Some special characters (/&!. ^ * $ \?) It has special significance for the search process. Therefore, if these characters appear in the search string, they must be converted. It is a special character to be converted. You must add a backslash (\) before the character (\). For example, to search for the string anything ?, Type/anything \? Then press Enter.

2. Exact search for strings

In Vi, you can add the following special characters to the string to make the search more accurate.
Match the beginning of a line. The string must start with ^.
Match the end of a line. The string must end with $.
Match the beginning of a word, and enter the start of a string as \ <
Match the end of a word. Enter the end of a string. \>
Match any character. Enter a point (.) for the position of the string to be matched (.)
For example,
To search for a row starting with search, type/^ search. To search for a row ending with search, type/search $.

3. replace a string

String replacement is based on search. All special matching Characters Used for search can be used for search and replacement. During replacement, you must specify the replacement range (1, N), 1 and N to indicate the row number, and N to indicate the last row when n is $. S is the replacement command, and g Represents replacement throughout the process. For example,

: L, $ S/patternl/pattern2/g -- change the text from line L to the end, and change the string of patternl to the string of pattern2, if there is no G, replace only the first string matched by each line. If there is g, replace each string.

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.