ED(1) BSD ED(1)
NAME
ed - text editor
SYNOPSIS
ed [ - ] [ file ]
DESCRIPTION
ed is the standard text editor. ed operates on a copy of any file it is
editing. Changes made in the copy have no effect on the file until you
give a w (write) command. The copy of the text being edited resides in a
temporary file called the buffer.
Commands to ed have a simple and regular structure: zero or more
addresses followed by a single character command, possibly followed by
parameters to the command. These addresses specify one or more lines in
the buffer. Missing addresses are supplied by default.
In general, only one command may appear on a line. Certain commands
allow the addition of text to 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 . alone at
the beginning of a line.
OPTIONS
file If you specify a file argument, ed simulates an e command (see
below) on the named file; that is, it reads the file into the
buffer so that it can edit the file.
- Suppresses the printing of explanatory output. You should use
this option when the standard input is an editor script.
REGULAR EXPRESSIONS
ed supports a limited form of regular expression notation. A regular
expression specifies a set of strings of characters. A member of this
set of strings is said to be matched by the regular expression. In the
following specification for regular expressions the word character means
any character except newline.
1. Any character except a special character matches itself. Special
characters are the regular expression delimiter plus \[ . and
sometimes ^*$.
2. A . matches any character.
3. A \ followed by any character except a digit or () matches that
character.
4. A nonempty string s bracketed [s] (or [^s]) matches any character in
(or not in) s. In s, \ has no special meaning, and ] may only appear
as the first letter. A substring a-b, with a and b in ascending
ASCII order, stands for the inclusive range of ASCII characters.
5. A regular expression of form 1-4 followed by * matches a sequence of
0 or more matches of the regular expression.
6. A regular expression, x, of form 1-8, bracketed \(x\) matches what x
matches.
7. A \ followed by a digit n matches a copy of the string that the
bracketed regular expression beginning with the nth \( matched.
8. A regular expression of form 1-8, x, followed by a regular
expression of form 1-7, y matches a match for x followed by a match
for y, with the x match being as long as possible while still
permitting a y match.
9. A regular expression of form 1-8 preceded by ^ (or followed by $),
is constrained to matches that begin at the left (or end at the
right) end of a line.
10. A regular expression of form 1-9 picks out the longest among the
leftmost matches in a line.
11. An empty regular expression stands for a copy of the last regular
expression encountered.
Regular expressions are used in addresses to specify lines and in one
command (see s below) to specify a portion of a line which is to be
replaced. If it is desired to use one of the regular expression
metacharacters as an ordinary character, that character may be preceded
by \. This also applies to the character bounding the regular expression
(often /) and to \ itself.
ADDRESSES
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; however, the exact effect on the current line
is discussed under the description of the command. 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 name x, which must be a
lower-case letter. Lines are marked with the k command described
below.
5. A regular expression enclosed in slashes / addresses the line found
by searching forward from the current line and stopping at the first
line containing a string that matches the regular expression. If
necessary the search wraps around to the beginning of the buffer.
6. A regular expression enclosed in queries ? addresses the line found
by searching backward from the current line and stopping at the
first line containing a string that matches the regular expression.
If necessary the search wraps around to the end of the buffer.
7. An address followed by a plus sign + or a minus sign - followed by a
decimal number specifies that address plus (resp. 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; for example, -5 is
understood to mean .-5.
9. If an address ends with + or -, then 1 is added (resp. subtracted).
As a consequence of this rule and rule 8, the address - refers to
the line before the current line. Moreover, trailing + and -
characters have cumulative effect, so -- refers to the current line
less 2.
10. To maintain compatibility with earlier versions of the editor, the
character ^ in addresses is equivalent to -.
Commands may require zero, one, or two addresses. Commands that require
no addresses regard the presence of an address as an error. Commands
that accept one or two addresses assume default addresses when
insufficient are given. If more addresses are given than such a command
requires, the last one or two (depending on what is accepted) are used.
Addresses are separated from each other, typically by a comma (,). They
may also be separated by a semicolon (;). In this case the current line
. is set to the previous address before the next address is interpreted.
This feature can be used to determine the starting line for forward and
backward searches (/, ?). The second address of any two-address sequence
must correspond to a line following the line corresponding to the first
address. The special form % is an abbreviation for the 1,$ address pair.
COMMANDS
In the following list of ed commands, the default addresses are shown in
parentheses. The parentheses are not part of the address.
As mentioned, it is generally illegal for more than one command to appear
on a line. However, most commands may be suffixed by p or by l, in which
case the current line is either printed or listed respectively in the way
discussed below. Commands may also be suffixed by n, meaning the output
of the command is to be line numbered. These suffixes may be combined in
any order.
(.)a
<text>
.
The append command reads the given text and appends it to the
addressed line. " ." is left on the last line input, if there is
any; otherwise it is left at the addressed line. Address 0 is legal
for this command; text is placed at the beginning of the buffer.
(., .)c
<text>
.
The change command deletes the addressed lines, then accepts input
text that replaces these lines. "." is left at the last line input;
if there is none, it is left at the line preceding the deleted
lines.
(., .)d
The delete command deletes the addressed lines from the buffer. The
line originally after the last line deleted becomes the current
line; if the lines deleted were originally at the end, the new last
line becomes the current line.
e filename
The edit command causes the entire contents of the buffer to be
deleted, and the named file to be read in. " ." is set to the last
line of the buffer. The number of characters read is typed.
filename is remembered for possible use as a default filename in a
subsequent r or w command. If filename is missing, the remembered
name is used.
E filename
This command is the same as e, except that no diagnostic results
when no w has been given since the last buffer alteration.
f filename
The filename command prints the currently remembered filename. If
you specify filename, the currently remembered filename is changed
to filename.
(1,$)g/regular expression/command list
The global command first marks every line that matches the given
regular expression. For every such line, the given command list is
executed with "." initially set to that line. A single command or
the first of multiple commands appears on the same line with the
global command. All lines of a multiline list, except the last
line, must be ended with \. A, i, and c commands and associated
input are permitted; the "." terminating input mode may be omitted
if it would be on the last line of the command list. The commands g
and v are not permitted in the command list.
(.)i
<text>
.
The insert command inserts the given text before the addressed line;
" ." is left at the last line input, or, if there is none, at the
line before the addressed line. This command differs from the a
command only in the placement of the text.
(., .+1)j
The join command joins the addressed lines into a single line;
intermediate newlines simply disappear. " ." is left at the
resulting line.
( . )kx
The mark command marks the addressed line with name x, which must be
a lowercase letter. The address form 'x then addresses this line.
(., .)l
The list command prints the addressed lines in an unambiguous way.
Non-graphic characters are printed in two-digit octal, and long
lines are folded. The l command may be placed on the same line
after any non-I/O command.
(., .)ma
The move command repositions the addressed lines after the line
addressed by a. The last of the moved lines becomes the current
line.
(., .)p
The print command prints the addressed lines. " ." is left at the
last line printed. The p command may be placed on the same line
after any non-I/O command.
(., .)P
This command is a synonym for p.
q The quit command causes ed to exit, without doing an automatic write
of a file.
Q This command is the same as q, except that no diagnostic results
when no w has been given since the last buffer alteration.
($)r filename
The read command reads in the given file after the addressed line.
If you did not specify a filename, ed uses the remembered filename,
if any (see e and f commands). ed remembers the filename if there
was no remembered filename already. 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
left at the last line read in from the file.
( ., .)s/regular expression/replacement/ or,
( ., .)s/regular expression/replacement/g
The substitute command searches each addressed line for an
occurrence of the specified regular expression. On each line in
which a match is found, all matched strings are replaced by the
replacement specified, if the global replacement indicator g appears
after the command. If the global indicator does not appear, only
the first occurrence of the matched string is replaced. It is an
error for the substitution to fail on all addressed lines. Any
punctuation character may be used instead of / to delimit the
regular expression and the replacement. "." is left at the last
line substituted.
An ampersand (&) appearing in the replacement is replaced by the
string matching the regular expression. The special meaning of & in
this context may be suppressed by preceding it by \. The characters
`\n' where n is a digit, are replaced by the text matched by the nth
regular subexpression enclosed between \( and \) characters. When
nested, parenthesized subexpressions are present, n is determined by
counting occurrences of \( starting from the left.
Lines can be split by substituting newline characters into them.
The newline in the replacement string must be escaped by preceding
it by \.
One or two trailing delimiters may be omitted, implying the p
suffix. The special form s followed by no delimiters repeats the
most recent substitute command on the addressed lines. The s may be
followed by the letters r (use the most recent regular expression
for the left hand side, instead of the most recent left hand side of
a substitute command), p (complement the setting of the p suffix
from the previous substitution), or g (complement the setting of the
g suffix). These letters may be combined in any order.
(., .)ta
This command acts just like the m command, except that a copy of the
addressed lines is placed after address a (which may be 0). "." is
left on the last line of the copy.
(., .)u
The undo command restores the buffer to its state before the most
recent buffer-modifying command. The current line is also restored.
Buffer-modifying commands are a, c, d, g, i, k, m, r, s, t, and v.
For purposes of undo, g and v are considered to be a single buffer-
modifying command. undo is its own inverse.
When ed runs out of memory the full undo is not possible, and u can
undo only the effect of the most recent substitute on the current
line. This restricted undo also applies to editor scripts when you
invoke ed with the - option.
(1, $)v/regular expression/command list
This command is the same as the global command g except that the
command list is executed g with "." initially set to every line
except those matching the regular expression.
(1, $)w filename
The write command writes the addressed lines onto the given file.
If the file does not exist, it is created. The filename is
remembered if there was no remembered filename already. If no file
name is given, the remembered filename, if any, is used (see e and f
commands). " . is unchanged. If the command is successful, the
number of characters written is printed.
(1, $)W filename
This command is the same as w, except that the addressed lines are
appended to the file.
(1, $)wq filename
This command is the same as w except that afterwards a q command is
done, exiting the editor after the file is written.
(.+1)z or,
(.+1)zn
This command scrolls through the buffer starting at the addressed
line; 22 (or n, if given) lines are printed. The last line printed
becomes the current line. The value n is sticky, in that it becomes
the default for future z commands.
($)= The line number of the addressed line is printed. " ." is unchanged
by this command.
!<shell command>
The remainder of the line after the ! is sent to sh(1) to be
interpreted as a command. The current line " . " is unchanged.
(.+1,.+1)<newline>
An address alone on a line causes the addressed line to be printed.
A blank line alone is equivalent to .+1p and is useful for stepping
through text. If two addresses are present with no intervening
semicolon, ed prints the range of lines. If they are separated by a
semicolon, ed prints the second line.
If you send an interrupt signal ed prints the message "?interrupted" and
returns to its command level.
NOTES
Some size limitations: 512 characters per line, 256 characters per
global command list, 64 characters per file name, and, on mini computers,
128K characters in the temporary file. The limit on the number of lines
depends on the amount of memory: each line takes two words.
When reading a file, ed discards ASCII NUL characters and all characters
after the last newline. It refuses to read files containing non-ASCII
characters.
DIAGNOSTICS
"?name" for an inaccessible file; "?self-explanatory message" for other
errors.
To protect against throwing away valuable work, a q or e command is
considered to be in error, unless you have specified a w (write command)
since the last buffer change. A second q or e is obeyed regardless.
BUGS
The l command mishandles interrupts.
The undo command causes marks to be lost on affected lines.
The -x option and the x commmand cannot be supported due to U.S.
encryption protection policies.
FILES
/tmp/e*
edhup: Work is saved here if terminal hangs up
SEE ALSO
ex(1), sed(1), UNIX Text Processing.