Name

Substitution

Synopsis

:[addr1[,addr2]]s/pattern/replacement/[cgp]

Replace the text matched by pattern with replacement on every line in the address range. If no address range is given, the current line is used. Each address may be either a line number or a regular expression. If addr1 is supplied, substitution will begin on that line number (or the first matching line) and continue until either the end of the file or the line indicated (or matched) by addr2. There are also a number of address shortcuts, which are described in the following tables.

Substitution options

Option

Meaning

c

Prompt before each substitution.

g

Replace all matches on a line.

p

Print line after substitution.

Address shortcuts

Address

Meaning

.

Current line.

$

Last line in file.

%

Entire file.

't

Position “t”.

/...[/]

Next line matched by pattern.

?...[?]

Next previous line matched by pattern.

\/

Next line matched by the last search.

\?

Next previous line matched by the last search.

\&

Next line where the last substitution pattern matched.

Get Regular Expression Pocket Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.