Name

Substitution

:[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 a line number, or a regular expression. If addr1 is supplied, substitution begins on that line number (or the first matching line), and continues until 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.

?...[?]

Previous line matched by pattern.

\/

Next line matched by the last search.

\?

Previous line matched by the last search.

\&

Next line where the last substitution pattern matched.

Get Regular Expression Pocket Reference, 2nd Edition 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.