ed(1) DG/UX 4.30 ed(1)
NAME
ed, red - text editor
SYNOPSIS
ed [ - ] [ -s ] [ -p string ] [ -x ] [ -C ] [ file ]
red [ - ] [ -p string ] [ -x ] [ -C ] [ file ]
DESCRIPTION
Ed is a standard text editor. If the file argument is
given, ed simulates an e command (see below) on the named
file; that is to say, the file is read into ed's buffer so
that it can be edited. The optional - suppresses the
printing of character counts by e, r, and w commands, of
diagnostics from e and q commands, and of the ! prompt after
a !shell command. The -s option suppresses the printing of
character counts by e, r, and w commands, of diagnostics
from e and q commands, and of the ! prompt after a !shell
command.
NOTE:
The - option, supported in this release for upward
compatibility, will not be supported in the next major
release of the system. Convert shell scripts that use
the - option to use the -s option instead.
The -p option lets the user specify a prompt string. If -x
is present, an X command is simulated first to handle an
encrypted file (U.S. versions of the DG/UX software only;
encryption mechanisms are not included in international
distributions of DG/UX software). The -C option is the same
as the -x option except that ed simulates a C command. The
C command is like X except that all text read in is assumed
to be encrypted.
Ed operates on a copy of the file it is editing; changes
made to the copy have no effect on the file until a w
(write) command is given. The copy of the text being edited
resides in a temporary file called the buffer. There is
only one buffer.
Red is a restricted version of ed. It will allow only
editing of files in the current directory. It prohibits
executing shell commands via !shell command. Attempts to
bypass these restrictions result in a ? diagnostic (and
optionally the error message restricted shell; see the h and
H commands below).
Both ed and red support the fspec(4) formatting capability.
After including a format specification as the first line of
file and invoking ed with your terminal in stty -tabs or
stty tab3 mode (see stty(1)), the specified tab stops will
Licensed material--property of copyright holder(s) Page 1
ed(1) DG/UX 4.30 ed(1)
automatically be used when scanning file. For example, if
the first line of a file contained:
:t5,10,15 s72:
tab stops would be set at columns 5, 10, and 15, and a
maximum line length of 72 would be imposed. NOTE: while
inputting text, tab characters when typed are expanded to
every eighth column as is the default.
Commands to ed have a simple and regular structure: zero,
one, or two addresses followed by a single-character
command, possibly followed by parameters to that command.
These addresses specify one or more lines in the buffer.
Every command that requires addresses has default addresses,
so that the addresses can very often be omitted.
In general, only one command may appear on a line. Certain
commands allow the input of text. This text is placed in
the appropriate place in the buffer. While ed is accepting
text, it is said to be in input mode. In this mode, no
commands are recognized; all input is merely collected.
Input mode is left by typing a period (.) alone at the
beginning of a line.
Ed supports a limited form of regular expression notation;
regular expressions are used in addresses to specify lines
and in some commands (e.g., s) to specify portions of a line
that are to be substituted. A regular expression (RE)
specifies a set of character strings. A member of this set
of strings is said to be matched by the RE. The REs allowed
by ed are constructed as follows:
I. The following one-character REs match a single
character:
1. An ordinary character (not one of those discussed in
this subsection I.) is a one-character RE that
matches itself.
2. A backslash (\) followed by any special character is
a one-character RE that matches the special character
itself. The special characters are:
a. ., *, [, and \ (period, asterisk, left square
bracket, and backslash, respectively), which
are always special, except when they appear
within square brackets ([]; see I.4 below).
b. ^ (caret or circumflex), which is special at
the beginning of an entire RE (see III.1 and
III.2 below), or when it immediately follows
the left of a pair of square brackets ([]) (see
I.4 below).
Licensed material--property of copyright holder(s) Page 2
ed(1) DG/UX 4.30 ed(1)
c. $ (currency symbol), which is special at the
end of an entire RE (see III.2 below).
d. The character used to bound (i.e., delimit) an
entire RE, which is special for that RE (for
example, see how slash (/) is used in the g
command, below.)
3. A period (.) is a one-character RE that matches any
character except new-line.
4. A non-empty string of characters enclosed in square
brackets ([]) is a one-character RE that matches any
one character in that string. If, however, the first
character of the string is a circumflex (^), the
one-character RE matches any character except new-
line and the remaining characters in the string. The
^ has this special meaning only if it occurs first in
the string. The minus (-) can indicate a range of
consecutive ASCII characters; for example, [0-9] is
equivalent to [0123456789]. The - loses this special
meaning if it occurs first (after an initial ^, if
any) or last in the string. The right square bracket
(]) does not terminate such a string when it is the
first character within it (after an initial ^, if
any); e.g., []a-f] matches either a right square
bracket (]) or one of the letters a through f
inclusive. The four characters listed in I.2.a above
stand for themselves within such a string of
characters.
II. The following rules can construct REs from one-
character REs:
1. A one-character RE is a RE that matches whatever the
one-character RE matches.
2. A one-character RE followed by an asterisk (*) is a
RE that matches zero or more occurrences of the one-
character RE. If there is any choice, the longest
leftmost string that permits a match is chosen.
3. A one-character RE followed by \{m\}, \{m,\}, or
\{m,n\} is a RE that matches a range of occurrences
of the one-character RE. The values of m and n must
be non-negative integers less than 256; \{m\} matches
exactly m occurrences; \{m,\} matches at least m
occurrences; \{m,n\} matches any number of
occurrences between m and n inclusive. Whenever a
choice exists, the RE matches as many occurrences as
possible.
Licensed material--property of copyright holder(s) Page 3
ed(1) DG/UX 4.30 ed(1)
4. The concatenation of REs is a RE that matches the
concatenation of the strings matched by each
component of the RE.
5. A RE enclosed between the character sequences \( and
\) is a RE that matches whatever the unadorned RE
matches.
6. The expression \n matches the same string of
characters as was matched by an expression enclosed
between \( and \) earlier in the same RE. Here n is
a digit; the sub-expression specified begins with the
n-th occurrence of \( counting from the left. For
example, the expression ^\(.*\)\1$ matches a line
consisting of two repeated appearances of the same
string.
III. Finally, an entire RE may be constrained to match only
an initial segment or final segment of a line (or both).
1. A circumflex (^) at the beginning of an entire RE
constrains that RE to match an initial segment of a
line.
2. A currency symbol ($) at the end of an entire RE
constrains that RE to match a final segment of a
line.
The construction ^entire RE$ constrains the entire RE to
match the entire line.
The null RE (e.g., //) is equivalent to the last RE
encountered. See also the last paragraph before FILES,
below.
To understand addressing in ed it is necessary to know that
at any time there is a current line. Generally speaking,
the current line is the last line affected by a command; the
exact effect on the current line is discussed under the
description of each command. IV. Addresses are constructed
as follows:
1. The character . addresses the current line.
2. The character $ addresses the last line of the
buffer.
3. A decimal number n addresses the n-th line of the
buffer.
4. 'x addresses the line marked with the mark name
character x, which must be a lowercase letter. Lines
Licensed material--property of copyright holder(s) Page 4
ed(1) DG/UX 4.30 ed(1)
are marked with the k command described below.
5. A RE enclosed by slashes (/) addresses the first line
found by searching forward from the line following
the current line toward the end of the buffer and
stopping at the first line containing a string
matching the RE. If necessary, the search wraps
around to the beginning of the buffer and continues
up to and including the current line, so that the
entire buffer is searched. See also the last
paragraph before FILES below.
6. A RE enclosed in question marks (?) addresses the
first line found by searching backward from the line
preceding the current line toward the beginning of
the buffer and stopping at the first line containing
a string matching the RE. If necessary, the search
wraps around to the end of the buffer and continues
up to and including the current line. See also the
last paragraph before FILES below.
7. An address followed by a plus sign (+) or a minus
sign (-) followed by a decimal number specifies that
address plus (or minus) the indicated number of
lines. The plus sign may be omitted.
8. If an address begins with + or -, the addition or
subtraction is taken with respect to the current
line; e.g, -5 is understood to mean .-5.
9. If an address ends with + or -, then 1 is added to or
subtracted from the address. As a consequence of
this rule and of rule 8 immediately above, the
address - refers to the line preceding the current
line. (To maintain compatibility with earlier
versions of the editor, the character ^ in addresses
is entirely equivalent to -.) Moreover, trailing +
and - characters have a cumulative effect, so --
refers to the current line less 2.
10. A comma (,) stands for the address pair 1,$, while a
semicolon (;) stands for the pair .,$.
Commands may require zero, one, or two addresses. Commands
that require no addresses treat an address as an error.
Commands that accept one or two addresses assume default
addresses when an insufficient number of addresses is given;
if more addresses are given than such a command requires,
the last one(s) are used.
Typically, addresses are separated from each other by a
comma (,). They may also be separated by a semicolon (;).
Licensed material--property of copyright holder(s) Page 5
ed(1) DG/UX 4.30 ed(1)
In the latter case, the current line (.) is set to the first
address, and only then is the second address calculated.
This feature can determine the starting line for forward and
backward searches (see rules 5. and 6. above). The second
address of any two-address sequence must correspond to a
line that follows, in the buffer, the line corresponding to
the first address.
In the following list of ed commands, the default addresses
are shown in parentheses. The parentheses are not part of
the address; they show that the given addresses are the
default.
Usually, no more than one command may appear on a line.
However, any command (except e, f, r, or w) may be suffixed
by l, n, or p. Then, the current line is either listed,
numbered or printed as discussed below under the l, n, and p
commands.
(.)a
<text>
.
The a(append) command reads the given text and appends
it after the addressed line; . is left at the last
inserted line, or, if there was none, at the addressed
line. Address 0 is legal for this command: it puts the
appended text at the beginning of the buffer. The
maximum number of characters that may be entered from a
terminal is 256 per line (including the new-line
character).
(.)c
<text>
.
The c(change) command deletes the addressed lines, then
accepts input text that replaces these lines; . is left
at the last line input, or, if there were none, at the
first line that was not deleted.
C
The C (change) command is the same as the X command
except that ed assumes that all text read in for the e
and r commands is encrypted unless a null key has been
typed in.
(.,.)d
The d(delete) command deletes the addressed lines from
the buffer. The line after the last line deleted
becomes the current line; if the lines deleted were
originally at the end of the buffer, the new last line
becomes the current line.
Licensed material--property of copyright holder(s) Page 6
ed(1) DG/UX 4.30 ed(1)
e file
The e(edit) command deletes the entire contents of the
buffer, and reads in the named file; . is set to the
last line of the buffer. If no file name is given, the
currently-remembered file name, if any, is used (see
the f command). The number of characters read is
typed; file is remembered for possible use as a default
file name in subsequent e, r, and w commands. If file
is replaced by !, the rest of the line is taken to be a
shell (sh(1)) command whose output is to be read. Such
a shell command is not remembered as the current file
name. See also DIAGNOSTICS below.
E file
The E(edit) command is like e, but the editor does not
check to see if any changes have been made to the
buffer since the last w command.
f file
If file is given, this command changes the currently-
remembered file name to file; otherwise, it prints the
currently-remembered file name.
(1,$)g/RE/command list
In the g(global) command, the first step is to mark
every line that matches the given RE. Then, for every
such line, the given command list is executed with .
initially set to that line. A single command or the
first of a list of commands appears on the same line as
the global command. All lines of a multi-line list
except the last line must be ended with a \; a, i, and
c commands and associated input are permitted. The .
terminating input mode may be omitted if it would be
the last line of the command list. An empty command
list is equivalent to the p command. The g, G, v, and
V commands are not permitted in the command list. See
also BUGS and the last paragraph before FILES , below.
(1,$)G/RE/
The interactive G(global) command marks every line that
matches the given RE. Then, for every such line, that
line is printed, . is changed to that line, and any one
command (other than one of the a, c, i, g, G, v, and V
commands) may be input and is executed. After the
execution of that command, the next marked line is
printed, and so on; a new-line acts as a null command;
an & causes the re-execution of the most recent command
executed within the current invocation of G. Note that
the commands input as part of the execution of the G
command may address and affect any lines in the buffer.
The G command can be terminated by an interrupt signal
(ASCII DEL or BREAK).
Licensed material--property of copyright holder(s) Page 7
ed(1) DG/UX 4.30 ed(1)
h
The h(help)
command gives a short error message that explains the
most recent ? diagnostic.
H
The H(help) command places ed in a mode in which error
messages are printed for all subsequent ? diagnostics.
It will also explain the previous ? if there was one.
The H command alternately turns this mode on and off;
it is initially off.
(.)i
<text>
.
The i(insert) command inserts the given text before the
addressed line; . is left at the last inserted line,
or, if there was none, at the addressed line. This
command differs from the a command only in the
placement of the input text. Address 0 is not legal
for this command. The maximum number of characters
that may be entered from a terminal is 256 per line
(including the new-line character).
(.,.+1)j
The j(join) command joins contiguous lines by removing
the appropriate new-line characters. If exactly one
address is given, this command does nothing.
(.)kx
The k (mark) command marks the addressed line with name
x, which must be a lowercase letter. The address 'x
then addresses this line; . is unchanged.
(.,.)l
The l(list) command prints the addressed lines in an
unambiguous way: a few non-printing characters (e.g.,
tab and backspace) are represented by (hopefully)
mnemonic overstrikes. All other non-printing
characters are printed in octal, and long lines are
folded. An l command may be appended to any other
command other than e, f, r, or w.
(.,.)ma
The m(move) command repositions the addressed line(s)
after the line addressed by a. Address 0 is legal for
a and moves the addressed line(s) to the beginning of
the file. It is an error if address a falls within the
range of moved lines; . is left at the last line moved.
(.,.)n
Licensed material--property of copyright holder(s) Page 8
ed(1) DG/UX 4.30 ed(1)
The n(number) command prints the addressed lines,
preceding each line by its line number and a tab
character; . is left at the last line printed. The n
command may be appended to any other command other than
e, f, r, or w.
(.,.)p
The p(print) command prints the addressed lines; . is
left at the last line printed. The p command may be
appended to any other command other than e, f, r, or w.
For example, dp deletes the current line and prints the
new current line.
P
The editor will prompt with a * for all subsequent
commands. The P command alternately turns this mode on
and off; it is initially off.
q
The q(quit) command causes ed to exit. No automatic
write of a file is done (but see DIAGNOSTICS below).
Q
The editor exits without checking if changes have been
made in the buffer since the last w command.
($)r file
The r(read) command reads in the given file after the
addressed line. If no file name is given, the
currently-remembered file name, if any, is used (see
the e and f commands). The currently-remembered file
name is not changed unless file is the very first file
name mentioned since ed was invoked. Address 0 is
legal for r and causes the file to be read at the
beginning of the buffer. If the read is successful,
the number of characters read is typed; . is set to the
last line read in. If file is replaced by !, the rest
of the line is taken to be a shell (sh(1)) command
whose output is to be read. For example, $r !ls
appends the current directory to the end of the file
being edited. Such a shell command is not remembered
as the current file name.
(.,.)s/RE/replacement/ or
(.,.)s/RE/replacement/g or
(.,.)s/RE/replacement/n n = 1-512
The s(substitute) command searches each addressed line
for an occurrence of the specified RE. In each line in
which a match is found, all (non-overlapped) matched
strings are replaced by the replacement if the global
replacement indicator g appears after the command. If
the global indicator does not appear, only the first
Licensed material--property of copyright holder(s) Page 9
ed(1) DG/UX 4.30 ed(1)
occurrence of the matched string is replaced. If a
number n appears after the command, only the nth
occurrence of the matched string on each addressed line
is replaced. It is an error for the substitution to
fail on all addressed lines. Any character other than
space or new-line may be used instead of / to delimit
the RE and the replacement; . is left at the last line
on which a substitution occurred. See also the last
paragraph before FILES below.
An ampersand (&) appearing in the replacement is
replaced by the string matching the RE on the current
line. You can suppress the special meaning of & in
this context by preceding it by \. As a more general
feature, the characters \n, where n is a digit, are
replaced by the text matched by the n-th regular
subexpression of the specified RE enclosed between \(
and \). When nested parenthesized subexpressions are
present, n is determined by counting occurrences of \(
starting from the left. When the character % is the
only character in the replacement, the replacement used
in the most recent substitute command is used as the
replacement in the current substitute command. The %
loses its special meaning when it is in a replacement
string of more than one character or is preceded by a
\.
You can split a line by substituting a new-line
character into it. The new-line in the replacement
must be escaped by preceding it by \. Such
substitution cannot be done as part of a g or v command
list.
(.,.)ta
This command acts just like the m command, but a copy
of the addressed lines is placed after address a (which
may be 0); . is left at the last line of the copy.
u
The u(undo) command nullifies the effect of the most
recent command that modified anything in the buffer,
namely the most recent a, c, d, g, i, j, m, r, s, t, v,
G, or V command.
(1,$)v/RE/command list
This command is the same as the global command g , but
the command list is executed with . initially set to
every line that does not match the RE.
(1,$)V/RE/
This command is the same as the interactive global
command G but the lines that are marked during the
Licensed material--property of copyright holder(s) Page 10
ed(1) DG/UX 4.30 ed(1)
first step are those that do not match the RE.
(1,$)w file
The w(write) command writes the addressed lines into
the named file. If the file does not exist, it is
created with mode 666 (readable and writable by
everyone), unless your umask setting (see sh(1))
dictates otherwise. The currently-remembered file name
is not changed unless file is the very first file name
mentioned since ed was invoked. If no file name is
given, the currently-remembered file name, if any, is
used (see the e and f commands); . is unchanged. If
the command is successful, the number of characters
written is typed. If file is replaced by !, the rest
of the line is taken to be a shell (sh(1)) command
whose standard input is the addressed lines. Such a
shell command is not remembered as the current file
name.
X
(not available in international distributions of the
DG/UX system; U.S. versions only) A key string is
demanded from the standard input. Subsequent e, r, and
w commands will encrypt and decrypt the text with this
key by the algorithm of crypt(1). The X command makes
an educated guess to determine whether text read in is
encrypted or not. The temporary buffer file is
encrypted also, using a transformed version of the key
typed in for the -x option. An explicitly empty key
turns off encryption.
($)=
The line number of the addressed line is typed; . is
unchanged by this command.
!shell command
The remainder of the line after the ! is sent to the
UNIX system shell (sh(1)) to be interpreted as a
command. Within the text of that command, the
unescaped character % is replaced with the remembered
file name; if a ! appears as the first character of the
shell command, it is replaced with the text of the
previous shell command. Thus, !! will repeat the last
shell command. If any expansion is performed, the
expanded line is echoed; . is unchanged.
(.+1)<new-line>
An address alone on a line prints the addressed line.
A new-line alone is equivalent to .+1p; it is useful
for stepping forward through the buffer.
If an interrupt signal (ASCII DEL or BREAK) is sent, ed
Licensed material--property of copyright holder(s) Page 11
ed(1) DG/UX 4.30 ed(1)
prints a ? and returns to its command level.
Some size limitations: 512 characters per line, 256
characters per global command list, 64 characters per file
name (including slashes), and 128K characters in the buffer.
The limit on the number of lines depends on the amount of
user memory: each line takes 1 word.
When reading a file, ed discards ASCII NUL characters and
all characters after the last new-line.
If the closing delimiter of an RE or of a replacement string
(e.g., /) would be the last character before a new-line,
that delimiter may be omitted. The addressed line is
printed. The following pairs of commands are equivalent:
s/s1/s2 s/s1/s2/p
g/s1 g/s1/p
?s1 ?s1?
FILES
$TMPDIR If this environmental variable exists and is not
null, its value is used in place of /usr/tmp as
the directory name for the temporary work file.
/usr/tmp If the $TMPDIR variable does not exist or is
null, this directory is used for the temporary
work file.
/tmp If $TMPDIR does not exist or is null and
/usr/tmp does not exist, /tmp is used as the
temporary work directory.
ed.hup Work is saved here if the terminal is hung up.
DIAGNOSTICS
? For command errors.
?file For an inaccessible file.
(use the h(help) and H(help) commands for detailed
explanations).
If changes have been made in the buffer since the last w
command that wrote the entire buffer, ed warns the user if
an attempt is made to destroy ed's buffer via the e or q
commands. It prints ? and lets you continue editing. A
second e or q command at this point will take effect. The -
command-line option inhibits this feature.
SEE ALSO
crypt(1), grep(1), sed(1), sh(1), stty(1).
fspec(4), regexp(5).
CAVEATS AND BUGS
A ! command cannot be subject to a g or a v command.
The ! command and the ! escape from the e, r, and w commands
cannot be used if the editor is invoked from a restricted
shell (see sh(1)).
The sequence \n in a RE does not match a new-line character.
Licensed material--property of copyright holder(s) Page 12
ed(1) DG/UX 4.30 ed(1)
The l command mishandles DEL.
Files encrypted directly with the crypt(1) command with the
null key cannot be edited.
Characters are masked to 7 bits on input.
If the editor input is coming from a command file (i.e., ed
file < ed-cmd-file), the editor will exit at the first
failure of a command in that file.
Licensed material--property of copyright holder(s) Page 13