Replace Vim text in Linux
Linux Command: vi Global replacement method
Syntax: [addr] s/source string/destination string/[option]
Global replacement command: % s/source string/destination string/g
[Addr]: indicates the search range. If it is omitted, it indicates the current row.
For example, "1st" indicates the number of rows from to 20;
"%": Indicates the entire file, which is the same as "1, $ ";
"., $": From the current row to the end of the file;
S: indicates the replacement operation.
[Option]: indicates the operation type.
For example, g indicates global replacement;
C Indicates confirmation
P indicates that the replacement result is displayed row by row (Ctrl + L restore the screen );
If option is omitted, only the first matching string in each line is replaced;
If special characters appear in the source string and target string, escape them "\"
1. Basic replacement
: 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
2. 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/
3. Delete ^ M from the text
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.
Run the command: cat filename1 | tr-d "^ V ^ M"> newfile;
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.
Process in vi: first use vi to open the file, then 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 includest
Strings A> B
4. other usage
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 Replace the first appearance of the search string; g is placed at the beginning of the command to replace all rows that contain search strings in the body.
Build VIM into a simple and practical IDE
Vim Learning Guide
Quick learn Vi Editor
Powerful Vim Editor
Build a Vim Development Environment on CentOS 6.2
Install the highlighted Vim editing tool in CentOS 5.4
Vim tips: C language settings
Set the Vim row number in Ubuntu
Vim editor basic tutorial
This article permanently updates the link address: