Unless you are already familiar with regular expressions,
the discussion of special characters above probably looks forbiddingly
A few more examples should make things clearer.
In the examples that follow, a square
is used to mark a blank space; it is not a special character.
Let's work through how you might use some special characters in a replacement.
Suppose that you have a long file
and that you want to substitute the word
throughout that file.
You first save the edited buffer with
try the global replacement:
When you continue editing, you notice occurrences of words
You have unintentionally matched the word
Returning to the last saved buffer with
, you now try:
(Note that there is a space after
But this command misses
and so on.
After some thought, you remember that brackets
allow you to specify one character from among a list, so
you come upon the solution:
This searches for
followed by either a
space (indicated by ) or any one of
the punctuation characters
. You expect to
replace this with
followed by the corresponding
space or punctuation mark,
but you've ended up with
a bunch of punctuation marks after every occurrence of
You need to save the
space and punctuation marks inside a
Then you can "replay"
them with a
. Here's the next attempt:
When the search matches a character inside the
on the right-hand side restores the same character.
The syntax may seem awfully complicated, but this
command sequence can save you a lot of work!
Any time you spend learning regular expression syntax will be repaid
The command is still not perfect, though. You've noticed
that occurrences of
have been changed,
so you need a way to match
when it isn't part of another
As it turns out,
(but not all other programs that use regular
expressions) has a special syntax for saying "only if the pattern
is a complete word." The character sequence
requires the pattern
to match at the beginning of a word, whereas
pattern to match at the end of a word. Using both will restrict
the match to a whole word. So, in the task given above,
will find all instances of the word
whether followed by punctuation or spaces.
Here's the substitution command you should use:
Suppose you have subroutine names beginning with the
If you want to save the prefixes but want to change the name
, either of the following replacement commands will do the trick.
The first example illustrates how
can be used to save whatever
pattern was actually matched. The second example shows how you can search
for one pattern but change another.
The global replacement keeps track of whether an
is saved. In that way,
is changed to
is part of the
Has the same effect as the previous command, but it is a little less safe
since it could change other instances of
on the same
line, not just those within the routine names.
You can also move blocks of text delimited by patterns.
For example, assume you have a 150-page reference manual.
All references pages are organized into three paragraphs with
the same three headings: SYNTAX, DESCRIPTION,
A sample of one reference page follows:
.Rh 0 "Get status of named file" "STAT"
integer*4 stat, retval
retval = stat (filename, status)
Writes the fields of a system data structure into the
These fields contain (among other
things) information about the file's location, access
privileges, owner, and time of last modification.
.IP "\fBfilename\fR" 15n
A character string variable or constant containing
the UNIX pathname for the file whose status you want
You can give the ...
Suppose that it is decided to move DESCRIPTION above the SYNTAX
With pattern matching, you can move blocks of text on all 150 pages
with one command!
:g /SYNTAX/,/DESCRIPTION/-1 mo /PARAMETERS/-1
This command operates on the block of
text between the line containing the word
and the line just
before the word
The block is moved (using
to the line just before
can place text only below the line specified.
to place text above a line, you first have to
move up a line with
, and then place your text below.
In a case like this, one command saves literally hours of work.
(This is a real-life example - we once used a pattern match like this
to rearrange a reference manual containing hundreds of pages.)
Block definition by patterns can be used equally well with other
For example, if you wanted to delete all DESCRIPTION paragraphs in
the reference chapter, you could enter:
This very powerful kind of change is implicit in
addressing syntax, but it is not readily apparent even to experienced
For this reason,
whenever you are faced with a complex, repetitive editing task, take
the time to analyze the problem
and find out if you can apply
pattern-matching tools to get the job done.
Since the best way to learn pattern matching is by example,
here is a list of pattern-matching examples, with explanations.
Study the syntax carefully, so that you understand the principles at
You should then be able to adapt these examples to your own situation.
italicization codes around
Notice that two backslashes (
) are needed in the replacement,
because the backslash in the
italicization code will be
interpreted as a special character.
alone would be interpreted as
you must type
Modify a list of pathnames in a file:
A slash (used as a delimiter in the global replacement sequence) must be
escaped with a backslash when it is part of the pattern or
to get /.
An alternate way to achieve this same
effect is to use a different character as the pattern delimiter.
For example, you could make the above replacement using colons as
Change all periods to semicolons in lines 1 to 10:
A dot has special meaning in regular expression syntax and must
be escaped with a backslash (\.).
Change all occurrences of the word
changes the pattern that follows to all uppercase. The
pattern that follows is the repeated search pattern, which is
one or more
spaces with a single space:
Make sure you understand how the asterisk works as a special
An asterisk following any character (or following any regular
expression that matches a single character, such as
zero or more
instances of that character.
Therefore, you must specify
spaces followed by an asterisk
to match one or more spaces (one space, plus zero or more spaces).
Replace one or more spaces following a colon with two spaces:
Replace one or more spaces following a period
colon with two spaces:
Either of the two characters within brackets can be matched.
This character is saved into a hold buffer, using
, and restored on the right-hand side by the
Note that within brackets a special character such as a dot
does not need to be escaped.
Standardize various uses of a word or heading:
The brackets enclose three characters: a space,
a colon, and the letter
Therefore, the pattern
An asterisk is added to the pattern so that it also matches
(with zero spaces after it) and
already correct spelling). Without the asterisk,
would be missed entirely and
incorrectly changed to
Delete all blank lines:
What you are actually matching here is the beginning of the line (^)
followed by the end of the line ($), with nothing in between.
Delete all blank lines, plus any lines that contain only white space:
(In the line above, a tab is shown as
A line may appear to be blank but may in fact contain spaces or tabs.
The previous example will not delete such a line.
This example, like the one above it, searches for the beginning and end
of the line. But instead of having nothing in between, the
pattern tries to find any number of spaces or tabs.
If no spaces or tabs are matched, the line is blank.
To delete lines that contain white space but that
you would have to match lines with
one space or tab:
Delete all leading spaces on a line:
to search for one or more spaces at the beginning of a line;
to save the rest of the line into the first hold buffer.
Restore the line without spaces, using
Delete all spaces at the end of a line:
to save all the text on the line,
but only up until one or more spaces
at the end of the line.
Restore the saved text without the spaces.
The substitutions in this example and the previous one
will happen only once on any given line, so the
doesn't need to follow the replacement string.
Insert a >
at the start of every line in a file:
What we're really doing here is "replacing" the start of the line with
Of course, the start of the line (being a logical construct, not an
actual character) isn't really replaced!
This command is useful when replying to mail or USENET news postings.
Frequently, it is desirable to include part of the
original message in your reply. By convention,
the inclusion is distinguished from your reply
by setting off the included text with a right angle
bracket and a couple of spaces at the start of the line. This can be done
easily as shown above. (Typically, only part of the original message will
be included. Unneeded text can be deleted either before or after the above
replacement.) Advanced mail systems do this automatically.
However, if you're using a primitive mail program,
you may need to do it manually.
Add a period to the end of the next six lines:
The line address indicates the current line plus five lines.
indicates the end of line. As in the previous
is a logical construct. You aren't
really replacing the end of the line.
Reverse the order of all hyphen-separated items in a list:
to save text on the line into the first hold buffer, but
only until you find
to save the rest of the line into the
second hold buffer.
Restore the saved portions of the line,
reversing the order of the two hold buffers.
The effect of this command on several items is shown below.
more - display files
display files - more
lp - print files
print files - lp
Change every word in a file to uppercase:
flag at the start of the replacement string tells
to change the replacement to uppercase. The
character replays the search pattern as the replacement.
These two commands are equivalent; however, the first form is
considerably faster, since it results in only one substitution per
matches the entire line, once per line),
whereas the second form results in repeated substitutions on each line
matches only a
single character, with the replacement repeated on account of the
Reverse the order of lines in a file:
The search pattern matches all lines (a line contains zero or more
Each line is moved, one by one, to the top of the file (that
is, moved after imaginary line 0). As each matched line is
placed at the top, it pushes the previously moved lines down,
one by one, until the last line is on top.
Since all lines have a beginning, the same result can be achieved
In a database, on all lines not marked
Paid in full
append the phrase
or the equivalent:
To affect all lines
those matching your pattern,
or simply use the
For any line that doesn't begin with a number, move the line
to the end of the file:
As the first character within brackets, a caret negates the
sense, so the two commands have the same effect. The first one
says, "Don't match lines that begin with a number," and the
second one says, "Match lines that don't begin with a number."
Change manually numbered section heads
(e.g., 1.1, 1.2, etc.) to a
for an A-level heading):
The search string matches a digit other than zero, followed by a
period, followed by another nonzero digit.
Notice that the period doesn't need to be escaped in the replacement
would have no effect, either).
The command above won't find chapter numbers containing
two or more digits. To do so, modify the command like this:
Now it will match chapters
10 to 99 (digits 1 to 9, followed by a digit),
100 to 999 (digits 1 to 9, followed by two digits),
The command still finds chapters
1 to 9 (digits 1 to 9, followed by no digit).
Remove numbering from section headings in a document.
You want to change the sample lines:
10.3.8 New Functions
into the lines:
Here's the command to do this:
The search pattern resembles the one in the previous example, but now
the numbers vary in length. At a minimum, the headings contain
, so you start
with the search pattern from the previous example:
But in this example, the heading may continue with any number of
digits or periods:
Change the word
to the phrase
FORTRAN (acronym of FORmula
First, since we notice that
use portions of the
original word, we decide to save the search pattern in two
The first time we restore it,
we use both pieces together, converting all characters to uppercase:
. Next, we undo the uppercase with
remaining replacement text would all be uppercase. The
replacement continues with actual typed words,
then we restore the first hold buffer. This buffer still contains
, so again we convert to uppercase first:
Immediately after, we lowercase the rest of the word:
Finally, we restore the second hold buffer. This contains
, so we precede the "replay" with uppercase, follow it
with lowercase, and type out the rest of the word:
We conclude this chapter by presenting sample tasks that involve
complex pattern-matching concepts.
Rather than solve the problems right away, we'll work toward
the solutions step by step.
Suppose you have a few lines with this general form:
the best of times; the worst of times: moving
The coolest of times; the worst of times: moving
The lines that you're concerned with always end
, but you never know what the first two words
might be. You want to change
any line that ends with
The greatest of times; the worst of times: moving
Since the changes must occur on certain lines, you need to
specify a context-sensitive global replacement. Using
will match lines that end with
Next, you realize that your search pattern could be any number of
any character, so the metacharacters
come to mind.
But these will match the whole line unless you somehow restrict
the match. Here's your first attempt:
This search string, you decide, will match from the beginning of
the line to the first
. Since you needed to specify the
to restrict the search, you simply repeat it in the
replacement. Here's the resulting line:
The greatest of times: moving
Something went wrong. The replacement gobbled the line up to the second
instead of the first. Here's why. When given a
choice, the action of "match any number of any character" will
match as much text as possible.
In this case, since the word
your search string finds:
the best of times; the worst of
the best of
Your search pattern needs to be more restrictive:
:g/moving$/s/.*of times;/The greatest of times;/
will match all characters up to
the instance of the phrase
Since there's only one instance, it has to be the first.
There are cases, though, when it is
inconvenient, or even incorrect, to use the
For example, you might find yourself typing
many words to restrict your search pattern, or you might
be unable to restrict the pattern by specific words (if the text
in the lines varies widely). The next section presents such a
Suppose you want to switch the order of all last names
and first names in a database.
The lines look like this:
Name: Feld, Ray; Areas: PC, UNIX; Phone: 123-4567
Name: Joy, Susan S.; Areas: Graphics; Phone: 999-3333
The name of each field ends with a colon, and each field is
separated by a semicolon. Using the top line as an example, you
want to change
We'll present some commands that look promising but don't work.
After each command, we show you the line the way it looked before
the change and after the change.
:%s/: \(.*\), \(.*\);/: \2 \1;/
Feld, Ray; Areas: PC
; Phone: 123-4567
Feld, Ray; Areas: PC
; Phone: 123-4567
We've highlighted the contents of the first hold buffer in
and the contents of the second hold buffer in
Note that the first hold buffer contains more than you want.
Since it was not sufficiently restricted by the pattern that
follows it, the hold buffer was able to save up to the second comma.
Now you try to restrict the contents of the first hold buffer:
:%s/: \(....\), \(.*\);/: \2 \1;/
Ray; Areas: PC, UNIX
; Phone: 123-4567
Ray; Areas: PC, UNIX
; Phone: 123-4567
Here you've managed to save the last name in the first hold
buffer, but now the second hold buffer will save anything
up to the last semicolon on the line. Now you restrict the
second hold buffer, too:
:%s/: \(....\), \(...\);/: \2 \1;/
; Areas: PC, UNIX; Phone: 123-4567
; Areas: PC, UNIX; Phone: 123-4567
This gives you what you want, but only in the specific case of a
four-letter last name and a three-letter first name. (The
previous attempt included the same mistake.) Why not just return
to the first attempt, but this time be more selective about the
end of the search pattern?
:%s/: \(.*\), \(.*\); Area/: \2 \1;/
; Areas: PC, UNIX; Phone: 123-4567
; Areas: PC, UNIX; Phone: 123-4567
This works, but we'll continue the discussion by
introducing an additional concern. Suppose that the
field isn't always present or isn't always the second field.
The above command won't work on such lines.
We introduce this problem to make a point. Whenever you rethink
a pattern match, it's usually better to work toward refining the
variables (the metacharacters), rather than using specific text
to restrict pattterns. The more variables you use in your
the more powerful your commands will be.
In the current example,
think again about the patterns you want to switch.
Each word starts with an uppercase letter and is followed by any
number of lowercase letters, so you can match the names like this:
Ok, but a last name might also have more than one uppercase letter
, for example),
so you'd want to search for this possibility in the second and
It doesn't hurt to use this for the first name, too (you never
will turn up).
Your command now becomes:
:%s/: \([A-Z][A-Za-z]*\), \([A-Z][A-Za-z]*\);/: \2 \1;/
Quite forbidding, isn't it?
It still doesn't cover the case of a name like
Joy, Susan S.
Since the first-name field might include a middle initial, you need to
add a space and a period within the second pair of brackets.
But enough is enough.
Sometimes, specifying exactly what
you want is more difficult than specifying what you
want. In your sample database, the last names end with a comma,
so a last-name field can be thought of as a string of characters
This pattern matches characters up until the first comma.
Similarly, the first-name field is a string of characters that
Putting these more efficient patterns back into your previous
command, you get this:
:%s/: \([^,]*\), \([^;]*\);/: \2 \1;/
The same command could also be entered as a
If all lines begin with
, you can say:
:g/^Name/s/: \([^,]*\), \([^;]*\);/: \2 \1;/
You can also add an asterisk after the first space,
in order to match a colon that has extra spaces (or no spaces)
:g/^Name/s/: *\([^,]*\), \([^;]*\);/: \2 \1;/
As we've usually seen the
command used, it selects lines that
are typically then edited by subsequent commands on the same line - for
example, we select lines with
, and then make substitutions
on them, or select them and delete them:
However, in his two-part tutorial in
Walter Zintz makes an interesting point about the
command. This command selects lines - but the associated editing
commands need not actually affect the lines that are selected.
Instead, he demonstrates a technique by which you can repeat
some arbitrary number of times. For example, suppose you want to place
ten copies of lines 12 through 17 of your file at the end of your
current file. You could type:
This is a very unexpected use of
, but it works! The
selects line 1, executes the specified
command, then goes on to
line 2, to execute the next copy command. When line 10 is
will have made ten copies.
Here's another advanced
example, again building on
suggestions provided in Zintz's article.
Suppose you're editing a document that consists of several parts.
Part 2 of this file is shown below, using ellipses to
show omitted text and displaying line numbers for reference.
301 Part 2
302 Capability Reference
304 Chapter 7
305 Introduction to the Capabilities
306 This and the next three chapters ...
400 ... and a complete index at the end.
402 Chapter 8
403 Screen Dimensions
404 Before you can do anything useful
405 on the screen, you need to know ...
556 Chapter 9
557 Editing the Screen
558 This chapter discusses ...
822 Part 3:
823 Advanced Features
825 Chapter 10
The chapter numbers appear on one line, their
titles appear on the line below, and the chapter text
(highlighted for emphasis)
begins on the line below that.
The first thing you'd like to do is copy the beginning line
of each chapter, sending it to an already existing file called
Here's the command that does this:
:g /^Chapter/ .+2w >> begin
You must be at the top of your file before issuing this command.
First you search for
at the start of a line,
but then you want to run the command on the beginning line of each
chapter - the second line below
Because a line beginning with
is now selected as
the current line,
the line address
will indicate the second line below it.
The equivalent line
work as well.
You want to write these lines to an existing file named
, so you issue the
command with the append operator
Suppose you want to send the beginnings of chapters that are only
within Part 2. You need to restrict the lines selected by
so you change your command to this:
:/^Part 2/,/^Part 3/g /^Chapter/ .+2w >> begin
command selects the lines that begin with
, but it searches
only that portion of the file from a line
through a line
If you issue the above command,
the last lines of the file
will read as follows:
This and the next three chapters ...
Before you can do anything useful
This chapter discusses ...
These are the lines that begin Chapters 7, 8, and 9.
In addition to the lines you've just sent,
you'd like to copy chapter titles to the end of
the document, in preparation for making a table of contents.
You can use the vertical bar to tack a second command after
your first command, like so:
:/^Part 2/,/^Part 3/g /^Chapter/ .+2w >> begin | +t$
Remember that with any subsequent command, line addresses are
relative to the previous command. The first command has marked
(within Part 2) that start with
, and the chapter titles
appear on a line below such lines. Therefore,
to access chapter titles in the second command, the line
(or the equivalents
to copy the chapter titles to the end of the
As these examples illustrate, thought and
experimentation may lead you to some unusual editing solutions.
Don't be afraid to try things! Just be sure to back up your file