@(#)e6 6.2 (Berkeley) 04/17/91
Special Characters
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
1 /x\*.y/
2 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:
1 ^ \*. $ [ * \e
2 .ul Warning: The backslash character \e 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
1 s/\e\e\e\*.\e*/backslash dot star/
2 will change \e.* into ``backslash dot star''.
Here is a hurried synopsis of the other special characters. First, the circumflex ^ signifies the beginning of a line. Thus
1 /^string/
2 finds string only if it is at the beginning of a line: it will find
1 string
2 but not
1 the string...
2 The dollar-sign $ is just the opposite of the circumflex; it means the end of a line:
1 /string$/
2 will only find an occurrence of string that is at the end of some line. This implies, of course, that
1 /^string$/
2 will find only a line that contains just string , and
1 /^\*.$/
2 finds a line containing exactly one character.
The character . , as we mentioned above, matches anything;
1 /x\*.y/
2 matches any of
1 x+y x-y x y x\*.y
2 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:
1 s/\*.*/stuff/
2 which changes an entire line, or
1 s/\*.*,//
2 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.)
[ is used with ] to form ``character classes''; for example,
1 /[0123456789]/
2 matches any single digit - any one of the characters inside the braces will cause a match. This can be abbreviated to [0-9] .
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
1 Now is the time
2 and you wanted to put parentheses around it. You could just retype the line, but this is tedious. Or you could say
1 s/^/(/ s/$/)/
2 using your knowledge of ^ and $ . But the easiest way uses the & :
1 s/\*.*/(&)/
2 This says ``match the whole line, and replace it by itself surrounded by parentheses.'' The & can be used several times in a line; consider using
1 s/\*.*/&? &!!/
2 to produce
1 Now is the time? Now is the time!!
2
You don't have to match the whole line, of course: if the buffer contains
1 the end of the world
2 you could type
1 /world/s//& is at hand/
2 to produce
1 the end of the world is at hand
2 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 & saves you from typing it again.
The & is a special character only within the replacement text of a substitute command, and has no special meaning elsewhere. You can turn off the special meaning of & by preceding it with a \e :
2 will convert the word ``ampersand'' into the literal symbol & in the current line.