VI command usage (search and replace)

Source: Internet
Author: User
VI command usage (search and replace)

09:54:14 | category:

OS (operating system) | Tag:
| Font size
Subscription

ESC: switch between command mode and insert mode

I. VI search:
After you open a file using vi, how can you find the keyword that you want to search for because the file is too long? No menu-> Search in VI,

But it doesn't matter. When you press the slash (/) in command mode, "/" appears in the status bar (that is, the lower left corner of the screen) and enter the keyword you want to search.

Press enter. If you want to continue searching for this keyword, you can continue searching by typing n. It is worth noting that "/" is a downward search, and "?"

Is to look up, and on the keyboard definition "?" It is just the top letter.

Ii. VI replacement:In VI/vim, you can use the: s command to replace the string. In the past, only one format was used to replace the full text, today, we found that this command has many writing methods (VI is really powerful, but there are still many things 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 Vivian/in the current line with Sky/
: % S +/oradata/apras/+/user01/apras1 + (replace with +/):/oradata/apras/Replace with/user01/apras1/

1.: 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

2.: N, $ S/Vivian/sky/Replace the first Vivian from row n to row N with 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)

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

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

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

: S # Vivian/# Sky/# Replace the first Vivian/in the current line with Sky/

5. Delete ^ m from the text
Problem description: For line breaks, use the carriage return line break (0a0d) in the window, and press enter (0a) in Linux to indicate like this,

When you copy files on Windows to Unix, there will always be a ^ m. Write a line break (0d) used to filter Windows files in UNIX)

Shell or C program §

Using the command: CAT filename1 | tr-d "^ V ^ m"> newfile;
§ Run the following command: sed-E "S/^ V ^ m //" FILENAME> outputfilename. in methods 1 and 2, ^ V and ^ m indicate Ctrl + V and CTRL + M. you must enter the file manually, instead of pasting §

§ Processing in VI: First open the file using vi, Press ESC, and then enter the command: % S/^ V ^ m //.
§: % S/^ m $/g

If the above method is useless, the correct solution is:

§ Tr-d \ "\ r \" <SRC> dest
§ Tr-d \ "\ 015 \" dest
§ Strings A> B

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

: S/str1/str2/use string str2 to replace str1 that appears for the first time in the line
: S/str1/str2/g replace all the str1 strings in the row with str2
: §, $ 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 str1 with string str2
: 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,

Indicates that only the first appearance of the search string is replaced; G is placed at the beginning of the command, indicating that all the search characters in the body are

Replace string rows §

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.