V6/usr/doc/ed/e6
.H1
Special Characters
.H2
.PG
You may have noticed that things just don't work right when you used
some characters like ``.'', ``*'', ``$'', and others in
context searches and the substitute command.
The reason is rather complex, although the cure is simple.
Basically,
.ul
ed
treats these characters as special, with special meanings.
For instance,
.ul
in a context search or the first string of the substitute command only,
\*. means ``any character,'' not a period, so
.X1
/x\*.y/
.X2
means ``a line with an x,
.ul
any character,
and a y,''
.ul
not
just ``a line with an x, a period, and a y.''~
A complete list of the special characters
that can cause trouble is the following:
.X1
^ \*. $ [ * \\
.X2
.ul
Warning:
The backslash character \\ is special to
.ul
ed.
For safety's sake,
avoid it where possible.
If you have to use one of the special characters
in a substitute command,
you can turn off its magic meaning temporarily
by preceding it with the backslash.
Thus
.X1
s/\\\\\\\*.\\*/backslash dot star/
.X2
will change ``\\\*.*'' into ``backslash dot star''.
.PG
Here is a hurried synopsis of the other special characters.
First, the circumflex `` ^ ''
signifies
the beginning of a line.
Thus
.X1
/^string/
.X2
finds ``string'' only if it is at the beginning of a line:
it will find
.X1
string
.X2
but not
.X1
the string...
.X2
The dollar-sign ``$'' is just the opposite of the circumflex;
it means the end of a line:
.X1
/string$/
.X2
will only find an occurrence of ``string''
that is at the end of some line.
This implies, of course,
that
.X1
/^string$/
.X2
will find only a line that contains just ``string'',
and
.X1
/^\*.$/
.X2
finds a line containing exactly one character.
.PG
The character ``\*.'', as we mentioned above,
matches anything;
.X1
/x\*.y/
.X2
matches any of
.X1
x+y
x-y
x y
x\*.y
.X2
This is useful in conjunction with ``*'',
which is a repetition character;
``a*'' is a shorthand for ``any number of a's,''
so ``\*.*'' matches any number of anythings.
This is used like this:
.X1
s/\*.*/stuff/
.X2
which changes an entire line,
or
.X1
s/\*.*,//
.X2
which deletes all characters in the line up to and
including the last comma.
(Since
``\*.*'' finds the longest possible match,
this goes up to the last comma.)
.PG
``['' is used with ``]'' to form ``character classes'';
for example,
.X1
/[1234567890]/
.X2
matches any single digit _
any one of the characters inside the braces
will cause a match.
.PG
Finally, the ``&'' is another shorthand character -
it is used only on the right-hand part of a substitute command
where it means ``whatever was matched on the left-hand side''.
It is used to save typing.
Suppose the current line contained
.X1
Now is the time
.X2
and we wanted to put parentheses around it.
We could just retype the line, but
this is tedious.
Or we could say
.X1
s/^/(/
s/$/)/
.X2
using our knowledge of ``^'' and ``$''.
But the easiest way uses the ``&'':
.X1
s/\*.*/(&)/
.X2
This says ``match the whole line, and replace it
by itself surrounded by parens.''~
The ``&'' can be used several times in a line;
consider
using
.X1
s/\*.*/&? &!!/
.X2
to produce
.X1
Now is the time? Now is the time!!
.X2
.PG
We don't have to match the whole line, of course:
if the buffer contains
.X1
the end of the world
.X2
we could type
.X1
/world/s//& is at hand/
.X2
to produce
.X1
the end of the world is at hand
.X2
Observe this expression carefully,
for it illustrates how to take advantage of
.ul
ed
to save typing.
The string ``/world/''
found the desired line;
the shorthand ``//'' found the same
word in the line;
and the ``&'' saved us from typing it again.
.PG
The ``&'' is a special character only within
the replacement text of a substitute command,
and has no special meaning elsewhere.
We can turn off the special meaning of ``&''
by preceding it with a ``\\'':
.X1
s/ampersand/\\&/
.X2
will convert the word ``ampersand'' into the literal symbol
``&''
in the current line.