When you use the replace command in the linux vi editing tool, data that matches the search mode is displayed, but the "mode not found" error is reported.
The reason is that the vi s // replacement operation is for the row by default. to take effect, move the cursor to the specified row and then use the s // command.
To Perform Batch replacement, run the % s // command, for example:
: % S/192.168.1.8/192.168.20.240/g
G indicates global replacement.
Vi replacement command reference:
Http://hi.baidu.com/our_poll/item/bb87c33233af5bbe633afff4
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 (useful for directory replacement)
: 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/
To replace the string of a specified row, you only need to add n, m (n to m rows) before s)
*************************************
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
: 1, $/^ M //
Problem description: For line breaks, use the carriage return line break (0A0D) in the window, and the carriage return (0A) in linux. In this way, when you copy files on windows to unix, there will always be a ^ M. Please write a shell or c program that is used in unix to filter windows files (0D.
· Use the command: cat filename1 | tr-d "^ V ^ M"> newfile;
· Run the following command: sed-e "s/^ V ^ M/" filename> outputfilename. Note that in methods 1 and 2, ^ V and ^ M indicate Ctrl + V and Ctrl + M. You must enter the file manually instead of pasting it.
· 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 "5" 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, 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.
1. Global replacement
(1) v + G + $ select all, and then enter: s/original string/Target string/
(2): % s/original string/Target string/
2. Clear the blank characters at the end of all rows on the page:
: % S/\ s \ + $ //
3. Clear all white spaces
: % S/\ (\ s * \ n \) \ +/\ r/
4. Remove all // annotations.
: % S! \ S *//.*!!
5. Remove all/**/comments.
: % S! \ S */\ * \ _. \ {-} \ */\ s *! !
6. Repeat some internal data
Sometimes we need to organize some batch data for command line execution. For example, we need to use the previous data to generate the subsequent data, such as the data:
/Var/database/aaa.txt
/Var/database/bbb.txt
/Var/database/ccc.txt
I want to change to commands one by one, for example, to copy commands one by one, but to the former directory, You can execute:
: % S/\ (. * base \/\) \ (. * \. txt \)/cp \ 1 \ 2 \. \/\ 2/
Changed:
Cp/var/database/aaa.txt./aaa.txt
Cp/var/database/bbb.txt./bbb.txt
Cp/var/database/ccc.txt./ccc.txt
The key here is the use of the submode: () in the middle, \ 1 represents the first child mode found, \ 2 represents the second, and so on, the () in vim must be escaped, different from other languages
7. Add comments (one operation is applied on multiple rows)
For example, you need to add the # Or // annotation:
Ctrl + v locate the start line, select the desired line, then execute the I command, enter # Or //, and press Esc twice, you can apply the annotation operation to all selected rows. Remember that the v command cannot be used for selection, but Ctrl + v should be used. (To clear the annotation, refer to the above method.) (v is selected by row, ctrl + v is selected by column)
8. Align
V select the rows to be neatly organized and enter = to normalize
9. open multiple windows
In vim, use: sp file name to open the row window (horizontal), use: vsp file name to open the column window (vertical), use: q to close the current window, and use: qa to close all windows, use Ctrl + w to switch between windows.
Maximize window: Ctrl + w to activate a window, and then Shitf +-to maximize
Change the window to the specified Row Height: Ctrl + w to activate the window, 10 (number of the row size to be changed), Shift +-, and change
Increase the height of several rows: Ctrl + w activation window, 10 (number of rows to be added), Shift ++, and change
10. String/variable prompt
In the input content status, press Ctrl + P to display all strings defined on this page. If you enter part of the string, press Ctrl + p to display all the content starting with the string you entered. In the input state, press Ctrl + x to view all valid deletion operations.
: % S/r // g press enter to delete DOS ^ M
: % S = * $ = Delete the blank space at the end of the row
: % S/^ (. *) n1/1 $/delete duplicate rows
: % S/^. {-} pdf/newusers/only deletes the first pdf
: % S/<! -- _. {-} --> // Delete the multi-line comment (comment? Why do we say "again ?)
: G/s * ^ $/d delete all empty rows: Is this easy to use? Has anyone used any other methods?
: G! /^ Dd/d Delete the row without the string 'dd'
: V/^ dd/d same as above (translated as v = g !, Is not matched !)
: G/str1/,/str2/d delete all the rows from the first entry containing str1 to the first entry containing str2.
: V/./,/./-1 join compressed empty rows
: G/^ $/,/./-j compressed empty rows
Ndw or ndW deletes n-1 characters starting at and following the cursor.
Delete d0 to the beginning of the row.
D $ Delete to the end of the row.
Ndd deletes the current row and the next n-1 row.
Delete a character from x or X.
Ctrl + u Delete the text entered in the input mode.
^ R restore u
J. Merge the next row to the end of the current row
V select a row
^ V press ^ V to select a rectangle.
Aw select word
Iw internal words (no space)
As Select sentence
Is Select sentence (no space)
Ap selection Section
Ip selection section (no space)
D. Delete it to the end of the row.
X, y Delete and copy contain highlight Area
Dl deletes the current character (same as the x command)
D0 is deleted to the starting position of a row.
D ^ Delete the first character (excluding spaces or TAB characters) to a row)
Dw is deleted to the end of a word.
D3w is deleted to the end of the third word.
Db is deleted to the start position of a word.
DW is deleted to the end of a word with a space as the separator.
DB is deleted to the start position of a word with space as the separator.
D7B is deleted to the start position of the first seven words with spaces as separators.
D) Delete to the end of a statement
D4) to the end of the fourth statement.
D (delete to the start position of a statement)
D) delete it to the end of a paragraph.
D {Delete to the beginning of a paragraph
D7 {Delete the first 7th paragraph positions before the start position of the current paragraph
Dd deletes the current row
D/text: Delete the position specified by "text" from the text,
Forward until the content of the next occurrence (but not including the word)
Dfc deletes the position where the character "c" appears from the text and continues until the next occurrence of the character (including the character ).
Dtc deletes the content from the current row until the next character "c" appears
D. Delete it to the end of a row.
D $ Delete to the end of a row
5dd Delete the contents of the five rows starting from the current row
Delete dL content until the last line on the screen
DH Delete until the first line of content on the screen
Delete content from the dG until the end of the work Cache
D1G deleted until the content in the work cache is used today.
4. Remove all // annotations.
: % S! \ S *//.*!!
5. Remove all/**/comments.
: % S! \ S */\ * \ _. \ {-} \ */\ s *! !