:[range]s/from/to/[flags]
range
: The search scope, if no range is specified, acts on but forward.
:1,10s/from/to/
Indicates a search substitution between lines 1th through 10th (containing 1th, 10th lines);
:10s/from/to/
Indicates that the search is replaced only on the 10th line;
:%s/from/to/
Represents a search substitution in all rows;
1,$s/from/to/
Ditto.
flags
The following four options are available:
c
Confirm, ask before each replacement;
e
Error, do not display errors;
g
Globle, do not ask, the whole line is replaced. If no option is added g
, only the first matching string of each row is replaced;
i
Ignore, ignoring case.
These options can be used in combination, such as representing case insensitive cgi
, full line substitution, and asking before replacing.
Regular expressions
Metacharacters
Metacharacters
Meta character |
Description |
. |
Match any character |
[ABC] |
Matches any one of the characters in the square brackets, which can be used to - represent a range of characters. such as [a-z0-9] matches lowercase letters and numbers |
[^ABC] |
Match any character except the character in square brackets |
\d |
Matching Arabic numerals, equivalent to [0-9] |
\d |
Matches any character other than the Arabic numeral, equivalent to [^0-9] |
\x |
Match hexadecimal digits, equivalent to [0-9a-fa-f] |
\x |
Matches any character other than a hexadecimal number, equivalent to [^0-9a-fa-f] |
\l |
Match [A-z] |
\l |
Match [^a-z] |
\u |
Match [A-z] |
\u |
Match [^a-z] |
\w |
Match word letter, equivalent to [0-9a-za-z_] |
\w |
Matches any character other than the word letter, equivalent to [^0-9a-za-z_] |
\ t |
Match <TAB> character |
\s |
Match white space character, equivalent to [\ t] |
\s |
Match non-whitespace characters, equivalent to [^\t] |
-
Some ordinary characters need to be transferred
|
|
\* |
match * character |
|
. character |
\/ |
match / character |
\ |
match \ character |
\[ |
match [ characters |
\] |
match ] character |
-
Represents the number of metacharacters
|
|
* |
match 0-any one |
\+ |
match 1-any of |
\? |
|
\{n,m} |
match n-m |
\{n} |
match n |
\{n,} |
matches N-any |
\{,m} |
match 0-m |
Meta-character representing position
Meta character |
Description |
$ |
Match end of Line |
^ |
Match beginning |
\< |
Match the first words of the word |
\> |
Match word endings |
Substitution variables
Regular expressions in and around regular \(
expressions \)
, which can be used in the subsequent use \1
, \2
such as variables to access \(
and \)
in the content.
Example
- Delete line trailing spaces:
:%s/\s+$//g
- Remove extra spaces at the beginning of the line:
%s/^\s*//
or%s/^ *//
- Delete empty lines that are not in the content:
%s/^$//
org/^$/d
- Delete empty lines that consist of spaces:
%s/^\s*$//
org/^\s*$/d
- Delete blank lines beginning with space or tab to the end:
%s/^[ |\t]*$//
org/^[ |\t]*$/d
Replace all strings in the text "abc......xyz" with "XYZ......ABC" to have the following wording
:%s/abc\(.*\)xyz/xyz\1abc/g:%s/\(abc\)\(.*\)\(xyz\)/\3\2\1/g
SED: Specify file to replace
Sed-i "s/from/to/g" file name
Vim replaces "Turn"