Regular expressions, and their use with the substitute command, are what give vi most of its significant editing power.
The general form of the substitute command is:
:[addr1
[,addr2
]]s/old
/new
/[flags
]
Omitting the search pattern
(:s//
replacement
/
)
uses the last search or substitution regular expression.
An empty replacement part
(:s/
pattern
//
)
“replaces” the matched text with nothing, effectively deleting it from
the line.
.
(period) Matches any single character except a newline. Remember that spaces are treated as characters.
*
Matches zero or more (as many as there are) of the single character that immediately precedes it.
The *
can follow a metacharacter, such as
.
, or a range in brackets.
^
When used at the start of a regular expression,
^
requires that the following regular
expression be found at the beginning of the line. When not at the
beginning of a regular expression, ^
stands for
itself.
$
When used at the end of a regular expression,
$
requires that the preceding regular
expression be found at the end of the line. When not at the end of
a regular expression, $
stands for
itself.
Treats the following special character as an ordinary
character. Use \
to get a literal
backslash.
~
Matches whatever regular expression was used in the last search.
[ ]
Matches any one of the characters enclosed between the brackets. A range of consecutive characters can be specified by separating the first and last characters in the range with a hyphen.
You can include more than one range inside brackets and specify a mix of ranges and separate characters.
Most metacharacters lose their special meaning inside
brackets, so you don’t need to escape them if you want to use them
as ordinary characters. Within brackets, the three metacharacters
you still need to escape are
-
]
. The hyphen
(-
) acquires meaning as a range specifier; to
use an actual hyphen, you can also place it as the first character
inside the brackets.
A caret (^
) has special meaning only when
it’s the first character inside the brackets, but in this case,
the meaning differs from that of the normal ^
metacharacter. As the first character within brackets, a
^
reverses their sense: the brackets match any
one character not in the list. For example, [^a-z]
matches any character that’s not a lowercase letter.
On modern systems, the locale can affect the interpretation of ranges within brackets, causing vi to match letters in a surprising fashion. It is better to use POSIX bracket expressions (see POSIX Bracket Expressions) to match specific kinds of characters, such as all lowercase or all uppercase characters.
(...)
Saves the pattern enclosed between (
and
)
into a special holding space or “hold
buffer.” You can save up to nine patterns in this way on a single
line.
You can also use the
n
notation within a
search or substitute string:
:s/(abcd)1/alphabet-soup/
changes abcdabcd
into
alphabet-soup
.[3]
< >
Matches characters at the beginning
(<
) or end (>
) of a
word. The end or beginning of a word is determined either by a
punctuation mark or by a space. Unlike (...)
,
these don’t have to be used in matched pairs.
POSIX bracket expressions may contain the following:
A POSIX character class consists of keywords bracketed by
[:
and :]
. The keywords
describe different classes of characters, such as alphabetic
characters, control characters, and so on (see the following
table).
A collating symbol is a multicharacter sequence that
should be treated as a unit. It consists of the characters
bracketed by [.
and
.]
.
An equivalence class lists a set of characters that
should be considered equivalent, such as e
and
è
. It consists of a named element from the
locale, bracketed by [=
and
=]
.
All three constructs must appear inside the square brackets of a bracket expression.
Class | Matching characters |
---|---|
| Alphanumeric characters |
| Alphabetic characters |
| Space and tab characters |
| Control characters |
| Numeric characters |
| Printable and visible (nonspace) characters |
| Lowercase characters |
| Printable characters (includes whitespace) |
| Punctuation characters |
| Whitespace characters |
| Uppercase characters |
| Hexadecimal digits |
n
Is replaced with the text matched by the
nth pattern previously saved by
(
and )
, where
n is a number from one to nine, and
previously saved patterns (kept in hold buffers) are counted from
the left on the line.
Treats the following special character as an ordinary
character. To specify a real backslash, type two in a row
(\
).
&
Is replaced with the entire text matched by the search pattern when used in a replacement string. This is useful when you want to avoid retyping text.
~
The string found is replaced with the replacement text specified in the last substitute command. This is useful for repeating an edit.
u
or l
Changes the next character in the replacement string to uppercase or lowercase, respectively.
U
or L
and e
or E
U
and L
are similar
to u
or l
, but all
following characters are converted to uppercase or lowercase until
the end of the replacement string or until e
or E
is reached. If there is no
e
or E
, all characters of
the replacement text are affected by the U
or
L
.
A simple :s
is the same as
:s//~/
.
:&
is the same as
:s
. You can follow the &
with g
to make the substitution globally on the
line, and even use it with a line range.
You can use the & key as a
vi command to perform the
:&
command, i.e., to repeat the last
substitution.
The :~
command is similar to the
:&
command, but with a subtle difference. The
search pattern used is the last regular expression used in
any command, not necessarily the one used in
the last substitute command.
Besides the /
character, you may use any
nonalphanumeric, nonwhitespace character as your delimiter, except
backslash, double quote, and the vertical bar (,
"
, and |
).
When the edcompatible
option is enabled,
vi remembers the flags (g
for
global and c
for confirmation) used on the last
substitution and applies them to the next one.
13.59.38.41