vim editor to find and replace

Share - Reprint: How to use VI editor to find and replace

https://www.cnblogs.com/xiandedanteng/p/5229813.html

 

Reprint: How to use VI editor to find and replace

 When using the vi editor to edit long documents, often dizziness, could not find the content needs to be changed.

In this case, use the Find function is particularly important.

As follows:

1. Input "/ character string" command mode, for example, "/ Section 3".

2, if the next to find a, press "n" can be.

To search upwards from the current cursor position, use the following command:

/ pattern the Enter

where, pattern represents the specific sequence of characters to search for.

To search down from the current cursor position, use the following command:

? Pattern Enter

press the Enter key, vi searches for the specified pattern, and position the cursor at the first character of the pattern. For example, to search up the word place, type:

vi search and replace Daquan

vi / vim can use the: s command to replace the string. Before only to replace the use of a full-text format, today found that there are a variety of writing commands (vi really powerful ah, there is much to learn), recorded several here, to facilitate future inquiries.


: S / vivian / sky / replace the current line is a sky vivian


: S / vivian / sky / g in the current line is all sky vivian


: n, s / vivian / sky / replacing n-th row to the last row of the first row of each vivian sky: the first alternative n-th line n, s / vivian / sky / to the last line of each row a vivian as sky: n, s / vivian / sky / g replacing the n-th row to the last line of each row of all vivian as Sky

n is a number, if n is, starts from the current line to the last line.

:% S / vivian / sky / (equivalent to: g / vivian / s // sky /) replace the first row of each vivian Sky

:% S / vivian / Sky / G (equivalent to: g / vivian / s // sky / g) replacing each row of all vivian sky


can be used as a delimiter #, whereby the intermediate appear / not as delimiters

: s # vivian / # sky / # replace the current line a vivian / sky is /

:% s + / oradata / apras / + / user01 / apras1 + ( used to replace + /): / oradata / apras / replace / User01 / apras1 /

1.:s/vivian/sky/ the current line is the first vivian Sky

: s / vivian / sky / g in the current line is all vivian Sky

2.: n, S / vivian / Sky / replacing n-th row to the last row of the first row of each vivian sky: n, s / vivian / sky / replacement of the n-th row to the last row in the beginning of each line Vivian is a sky: n, s / vivian / sky / g alternative n-th row to the last line of each row of all vivian Sky

(n is a number, if n is, starts from the current line to the last line.)

3.:% s / vivian / sky / ( equivalent to: g / vivian / s // sky /) replace the first row of each vivian Sky

:% S / vivian / Sky / G (equivalent to: g / vivian / s // sky / g) replacing each row of all vivian Sky

4. # can be used as a delimiter, whereby the intermediate appear / not as delimiters

: s # vivian / # sky / # replace a first current line a vivian / is Sky /

^ M 5. the deleted text

description of the problem: for the wrap, the window with the transport line (0A0D) expressed under Linux is a carriage return (0A) to represent change. Thus, when copying a file on the window on Unix use, there will always be a ^ M. Please write a newline use in filtering windows unix file (0D) c of the shell or program.

• Using the command: CAT filename1 | TR -d "^ V ^ M"> newfile;

• Using the command: sed -e "s / ^ V ^ M //" filename> outputfilename.

Note that in both methods 1 and 2, ^ V ^ M, and refer to and Ctrl + V Ctrl + M.

You must be manually entered, rather than the stick.

· Processing in vi: vi is first used to open the file, and then press the ESC key, then enter the command:% s / ^ V ^ M //.

·:% S / ^ M // g useless if the method described above, the correct solution is: ⋅tr-d "\ r" <src> dest⋅tr-d "\ 015" dest⋅stringsA> B6 other use. : s command string to be replaced.

Specific uses include:

: S / str1 / str2 / replace string in str2 str1 line first appears

: S / str1 / str2 / g with the line to be replaced in str2 str1 all strings appear:.,

 

// g If the above method useless, the correct solution is: · tr-d "\ r" <src> dest · tr-d "\ 015" dest · stringsA> B6.

Other using: s command string to be replaced.

Specific uses include:

: S / str1 / str2 / string line to be replaced in str2 str1 first occurrence: s / str1 / str2 / g with the line to be replaced in str2 str1 all strings appear:., 

s / str1 / str2 / g text string str2 replace the current line to the end of all strings appear str1

: 1, $ s / str1 / str2 / g in str2 replacing the body with all the strings appear str1

: g / str1 / s // str2 / g above function

can be seen from the above replace command:

g on the end of the command, the search string represents replace each occurrence;

Without g, it represents only the first occurrence of the search string to be replaced;

g on the beginning of the command, the text representation of all the rows contain the search string replacement operation.

 



Reproduced please indicate the original address: http: //www.server110.com/linux/201404/10603.html

 

 

Published 237 original articles · won praise 167 · views 30000 +

Guess you like

Origin blog.csdn.net/frdevolcqzyxynjds/article/details/105282200