SYNOPSIS
ed [- | -s] [-p prompt] [name]
DESCRIPTION
Ed is the standard text editor.
If a name 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 - or -s suppresses the printing of
character counts by e, r, and w commands, and of the `!' after comple-
tion of a shell command.
With the -p option, the given prompt string is printed before each com-
mand is read.
Ed operates on a copy of any file it is editing; changes made in 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.
The editor supports format specifications as defined in fspec(5). If
the terminal is configured to expand tabulators (as enabled with stty
tab3 or stty -tabs), and the first line of the file being edited con-
tains a format specification, the t and s are interpreted, that is,
tabulators are expanded and lines are truncated when printing to the
terminal. For example,
<:t-f s72:>
selects FORTRAN format and truncates lines at 72 characters. No expan-
sion or truncation is performed by ed when input is typed to the termi-
nal.
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 recog-
nized; 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. 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 but newline. /usr/5bin/ed uses simple regular
expressions, whereas /usr/5bin/s42/ed, /usr/5bin/posix/ed, and
/usr/5bin/posix2001/ed use basic regular expressions.
Simple Regular Expressions
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 of form 1-4 followed by \{m,n\} forms an
interval expression that matches a sequence of m through n
matches, inclusive, of the regular expression. The values of m
and n must be non-negative and smaller than 256. The form \{m\}
matches exactly m occurrences, \{m,\} matches at least m occur-
rences.
7. The sequence \< forces the match to occur only at the beginning
of a ``variable'' or ``word''; that is, either at the beginning
of a line, or just before a letter, digit or underline and after
a character not one of these.
8. The sequence \> matches the end of a ``variable'' or ``word'',
i.e. either the end of the line or before character which is
neither a letter, nor a digit, nor the underline character.
9. A regular expression, x, of form 1-11, parenthesized \(x\) is
called a subexpression and matches what x matches.
10. A \ followed by a digit n forms a backreference and matches a
copy of the string that the parenthesized regular expression
beginning with the nth \( matched.
11. A regular expression of form 1-11, x, followed by a regular
expression of form 1-10, 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.
12. A regular expression of form 1-11 preceded by ^ (or followed by
$), is constrained to matches that begin at the left (or end at
the right) end of a line (anchoring).
13. A regular expression of form 1-12 picks out the longest among
the leftmost matches in a line.
14. An empty regular expression stands for a copy of the last regu-
lar expression encountered.
Basic Regular Expressions
Basic Regular Expressions add the following features to Simple Regular
Expressions:
15. The special character *, as described in 5., and the interval
expressions described in 6. can also be applied to subexpres-
sions as described in 9. For POSIX.1-2001 utilities such as
/usr/5bin/posix2001/ed, subexpressions do not match the empty
string if there is a possible longer match.
Collating symbol expressions of the form [.c.], where c is a
collating symbol in the current LC_COLLATE locale. A collating
symbol expression matches the specified collating symbol.
Equivalence class expressions of the form [=c=], where c is a
collating symbol in the current LC_COLLATE locale. An equiva-
lence class expression matches any character that has the same
collating weight as c.
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.
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 cur-
rent 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 begin-
ning 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 `-' fol-
lowed 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; e.g. `-5' is under-
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.
Omission of the first address causes the first line to be used with
`,', or the current line with `;', respectively; if the second address
is also omitted, the last line of the buffer is used. Thus a single
`,' specifies the entire contents of the buffer, and a single `;' spec-
ifies the contents ranging from the current line to the last one.
In the following list of ed commands, the default addresses are shown
in parentheses. The parentheses are not part of the address, but are
used to show that the given addresses are the default.
As mentioned, it is generally illegal for more than one command to
appear on a line. However, most commands may be suffixed by `p', `l',
or `n', in which case the current line is either printed, listed, or
numbered respectively in the way discussed below.
(.)a
<text>
.
The append command reads the given text and appends it after the
addressed line. `.' is left on the last line input, if there were
any, otherwise 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 which replaces these lines. `.' is left at the last line
input; if there were none, it is left at the line preceding the
deleted lines. For /usr/5bin/posix2001/ed, a `0' as the first
address is identical to `1'.
(.,.)d
The delete command deletes the addressed lines from the buffer.
The line originally after the last line deleted becomes the cur-
rent 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
If `filename' is given, the currently remembered file name is
changed to `filename'.
(1,$)g/regular expression/command list
In the global command, the first step is to mark every line which
matches the given regular expression. Then 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 multi-
line 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 com-
mand list.
(1,$)G/regular expression/
The interactive global command first marks every line matching the
given regular expression. Then each line is printed and a command
is read and executed for this line. A single newline character
causes the line to remain unchanged, an isolated `&' repeats the
command given for the previous line. The command can be termi-
nated by an interrupt signal.
h This command prints a verbose description for the last error
encountered.
H This command acts like the h command, but also causes verbose
descriptions to be printed on all following error conditions.
Another H turns verbose mode off.
(.)i
<text>
.
This command inserts the given text before the addressed line.
`.' is left at the last line input, or, if there were none, at the
line before the addressed line. This command differs from the a
command only in the placement of the text. For
/usr/5bin/posix2001/ed, a `0' as the first address is identical to
`1'.
(.,.+1)j
This command joins the addressed lines into a single line; inter-
mediate newlines simply disappear. `.' is left at the resulting
line.
(.)kx
The mark command marks the addressed line with name x, which must
be a lower-case letter. The address form `'x' then addresses this
line.
(.,.)l
The list command prints the addressed lines in an unambiguous way:
(.,.)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 causes a prompt to be printed before following com-
mands are read. The default prompt is a `*' character, but can be
set with the -p command line option (which also enables the
prompt). Another P disables the prompt.
q The quit command causes ed to exit. No automatic write of a file
is done.
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 no file name is given, the remembered file name, if any, is
used (see e and f commands). The file name is remembered if there
was no remembered file name 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. A `filename'
starting with a `!' causes the output of the shell command fol-
lowing this character to be read in.
(.,.)s/regular expression/replacement/ or,
(.,.)s/regular expression/replacement/g or,
(.,.)s/regular expression/replacement/number
The substitute command searches each addressed line for an occur-
rence of the specified regular expression. On each line in which
a match is found, all matched strings are replaced by the replace-
ment 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; if the
number indicator is given, the numbered occurrence 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 regular expression and the replace-
ment. `.' 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 n-th regular subexpression enclosed between `\('
and `\)'. When nested, parenthesized subexpressions are present,
n is determined by counting occurrences of `\(' starting from the
left.
the current state is restored.
(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,$)V/regular expression/
This command is the same as the interactive global command G
except that the commands are read 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 mode 666 (readable and
writable by everyone). The file name is remembered if there was
no remembered file name already. If no file name is given, the
remembered file name, if any, is used (see e and f commands). `.'
is unchanged. If the command is successful, the number of charac-
ters written is printed. A `filename' starting with a `!' causes
the string following this character to be executed as a shell com-
mand with the addressed lines as standard input.
(1,$)W filename
This command is the same as w, except that the addressed lines are
appended to the file.
($)= 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 sh(1) to be
interpreted as a command. `.' is unchanged. If the command
starts with a `!', the previous command is inserted. A `%' causes
the current file name to be inserted.
(.+1)<newline>
An address alone on a line causes the addressed line to be
printed. A blank line alone is equivalent to `.+1p'; it is useful
for stepping through text.
The following commands are extensions:
(.)b[count]
Prints a screenful of lines, starting at the addressed one, and
browses forward in the buffer by this amount. With the optional
count argument, the screen size for this and following b commands
is set to the given number of lines.
help Causes a summary of ed commands along with short descriptions to
be printed on the terminal.
An input line that consists exactly of the two characters `\.' causes
a period `.' to be inserted with the a, c, and i commands in
/usr/5bin/ed and /usr/5bin/s42/ed.
Some size limitations: The maximum number of bytes in the buffer corre-
sponds to the address size; on machines with 32-bit addressing, it is
2 G bytes, with 64-bit addressing, it is 9 E bytes. The limit on the
number of lines depends on the amount of core: each line takes 2 words.
If a line contains a NUL character, regular expressions cannot match
beyond this character. A substitute command deletes a NUL and all fol-
lowing characters on the line. NUL characters in command input are
discarded. If an input file does not end with a newline, ed prints a
message and appends one.
Omission of the `/' character following the regular expression or the
replacement string to the global and substitute commands causes the
affected lines to be printed. Thus the following commands have the
same effect:
g/pattern g/pattern/p
s/pattern/repl s/pattern/repl/p
s/pattern/ s/pattern//p
ENVIRONMENT VARIABLES
LANG, LC_ALL
See locale(7).
LC_COLLATE
Affects the collation order for range expressions, equivalence
classes, and collation symbols in basic regular expressions.
LC_CTYPE
Determines the mapping of bytes to characters in both simple and
basic regular expressions, the availability and composition of
character classes in basic regular expressions, and the set of
printable characters for the l command.
TMPDIR Determines the location of the temporary file if it contains the
name of an accessible directory.
FILES
/var/tmp/e*
/tmp/e*
ed.hup: work is saved here if terminal hangs up
SEE ALSO
B. W. Kernighan, A Tutorial Introduction to the ED Text Editor
B. W. Kernighan, Advanced editing on UNIX
bfs(1), grep(1), sed(1), sh(1)
DIAGNOSTICS
`?name' for inaccessible file; `?' for errors in commands, possibly
not available.
For portable programs, restrict textual data to the US-ASCII character
set, set the LC_CTYPE and LC_COLLATE variables to `C' or `POSIX', and
use the constructs in the second column instead of the character class
expressions as follows:
l l. [[:alnum:]] [0-9A-Za-z] [[:alpha:]] [A-Za-z]
[[:blank:]] [<tab><space>] [[:cntrl:]] [^<space>-~]
[[:digit:]] [0-9] [[:graph:]] [!-~] [[:lower:]] [a-z]
[[:print:]] [<space>-~] [[:punct:]] [!-/:-@[-`{-~]
[[:space:]] [<tab><vt><ff><cr><space>] [[:upper:]] [A-Z]
[[:xdigit:]] [0-9a-fA-F]
<tab>, <space>, <vt>, <ff>, and <cr> indicate inclusion of a literal
tabulator, space, vertical tabulator, formfeed, or carriage return
character, respectively. Do not put the <vt>, <ff>, and <cr> charac-
ters into the range expression for the space class unless you actually
want to match these characters.
Heirloom Toolchest 6/22/05 ED(1)
Man(1) output converted with
man2html