Find and replace Vim/VI

Source: Internet
Author: User

Source: http://blog.sina.com.cn/s/blog_40cccb2f0100nwi3.html


SEARCH command:

To search up from the current cursor position, run the following command:
/Pattern enter
Where, pattern indicates the specific character sequence to be searched.
To search down from the current cursor position, run the following command:
? Pattern enter
After pressing enter, VI searches for the specified pattern and positions the cursor at the first character of pattern. For example, to search for the word "place", type:
/Place enter
If VI finds the place, it positions the cursor at p. To search for other matches of place, press N or N:
N, continue to search for place in the same direction.
N. Search in the opposite direction.

4.5.1 search for special matches
In the preceding example, VI finds any sequence containing place, including displace, placement, and replaced.
To find a single place, type the word and add a space before and after it:
/Place enter
To find the place that appears only at the beginning of the row, add a hyphen (^) before the word ):
/^ Place enter
To find the place that appears only at the end of the row, add a currency symbol ($) after the word ):
/Place $ enter
Use ^
To search for a character with a hyphen (^) or a currency sign ($), add a backslash (\) before the character (\). Use the backslash command VI to search for special characters.
Use $
Special characters refer to characters with special functions in VI (such as ^, $, *,/, and .). For example, $ usually indicates "going to the end of a line", but if $ followed by a \, $ is just a common character.
Use \
For example,/(NO \ $ money) searches for the Character Sequence (no $ money) up ). Follow the Escape Character (\) command Vi before $ to search for currency symbols by word.

Replacement command:

In VI/vim, you can use the: s command to replace the string. In the past, we used only one format to replace the full text. Today we found that there are many ways to write this command (VI is really powerful, and there are still a lot to learn). Record several methods here to facilitate future queries.

: S/Vivian/sky/Replace the first Vivian in the current row with sky

: S/Vivian/sky/g replace all Vivian in the current row with sky

: N, $ S/Vivian/sky/Replace the first Vivian from row n to row N as sky

: N, $ S/Vivian/sky/g replace all Vivian values from row n to row n

N is a number. If n is., it indicates starting from the current row to the last row.

: % S/Vivian/sky/(equivalent to: G/Vivian/S // sky/) Replace the first Vivian of each row with sky

: % S/Vivian/sky/g (equivalent to: G/Vivian/S // sky/g) replace all

You can use # As the separator. The/in the middle will not be used as the separator.

: S # Vivian/# Sky/# Replace the first entry in the current rowVivian/IsSky/

: % S+/Oradata/apras/+/User01/apras1 + (replace with + /):/Oradata/apras/Replace with/user01/apras1/

 

6. Others

Use the S command to replace strings. The specific usage includes:

: S/str1/str2/replace string str2Row in progressString str1 that appears for the first time ---- only for the search row, that is, the current row

: S/str1/str2/g replace all the strings str1 in the row with string str2 ---- only for the search row, that is, the current row

:., $ S/str1/str2/g replace string str1 from the current row to the end of the body with string str2

: 1, $ S/str1/str2/g replace string str2All str1 strings in the body

: G/str1/S // str2/g functions are the same as above

From the above replacement command, we can see that G is placed at the end of the command, which means to replace each appearance of the search string; without g, it means to only search

String is replaced for the first time. G is placed at the beginning of the command to replace all rows containing the search string in the body.

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.