BSD 4_3_Tahoe development
[unix-history] / usr / man / cat1 / ed.0
ED(1) UNIX Programmer's Manual ED(1)
N\bNA\bAM\bME\bE
ed - text editor
S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS
e\bed\bd [ -\b- ] [ -\b-x\bx ] [ name ]
D\bDE\bES\bSC\bCR\bRI\bIP\bPT\bTI\bIO\bON\bN
_\bE_\bd is the standard text editor.
If a _\bn_\ba_\bm_\be argument is given, _\be_\bd simulates an _\be command (see
below) on the named file; that is to say, the file is read
into _\be_\bd'_\bs buffer so that it can be edited. If -\b-x\bx is
present, an _\bx command is simulated first to handle an
encrypted file. The optional -\b- suppresses the printing of
explanatory output and should be used when the standard
input is an editor script.
_\bE_\bd operates on a copy of any file it is editing; changes
made in the copy have no effect on the file until a _\bw
(write) command is given. The copy of the text being edited
resides in a temporary file called the _\bb_\bu_\bf_\bf_\be_\br.
Commands to _\be_\bd have a simple and regular structure: zero or
more _\ba_\bd_\bd_\br_\be_\bs_\bs_\be_\bs followed by a single character _\bc_\bo_\bm_\bm_\ba_\bn_\bd, pos-
sibly 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 _\be_\bd
is accepting text, it is said to be in _\bi_\bn_\bp_\bu_\bt _\bm_\bo_\bd_\be. In this
mode, no commands are recognized; all input is merely col-
lected. Input mode is left by typing a period `.\b.' alone at
the beginning of a line.
_\bE_\bd supports a limited form of _\br_\be_\bg_\bu_\bl_\ba_\br _\be_\bx_\bp_\br_\be_\bs_\bs_\bi_\bo_\bn notation.
A regular expression specifies a set of strings of charac-
ters. A member of this set of strings is said to be _\bm_\ba_\bt_\bc_\bh_\be_\bd
by the regular expression. In the following specification
for regular expressions the word `character' means any char-
acter but newline.
1. Any character except a special character matches
itself. Special characters are the regular expression
delimiter plus \[.\b. and sometimes ^*$.
2. A .\b. matches any character.
3. A \ followed by any character except a digit or ()
matches that character.
4. A nonempty string _\bs bracketed [_\bs] (or [^_\bs]) matches any
Printed 7/9/88 April 29, 1985 1
ED(1) UNIX Programmer's Manual ED(1)
character in (or not in) _\bs. In _\bs, \ has no special
meaning, and ] may only appear as the first letter. A
substring _\ba-_\bb, with _\ba and _\bb 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 expres-
sion.
6. A regular expression, _\bx, of form 1-8, bracketed \(_\bx\)
matches what _\bx matches.
7. A \ followed by a digit _\bn matches a copy of the string
that the bracketed regular expression beginning with
the _\bnth \( matched.
8. A regular expression of form 1-8, _\bx, followed by a reg-
ular expression of form 1-7, _\by matches a match for _\bx
followed by a match for _\by, with the _\bx match being as
long as possible while still permitting a _\by match.
9. A regular expression of form 1-8 preceded by ^ (or fol-
lowed 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 _\bs 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 _\be_\bd it is necessary to know that
at any time there is a _\bc_\bu_\br_\br_\be_\bn_\bt _\bl_\bi_\bn_\be. Generally speaking, the
current line is the last line affected by a command; how-
ever, the exact effect on the current line is discussed
under the description of the command. Addresses are con-
structed as follows.
1. The character `.\b.' addresses the current line.
2. The character `$' addresses the last line of the
buffer.
3. A decimal number _\bn addresses the _\bn-th line of the
Printed 7/9/88 April 29, 1985 2
ED(1) UNIX Programmer's Manual ED(1)
buffer.
4. `'_\bx' addresses the line marked with the name _\bx, which
must be a lower-case letter. Lines are marked with the
_\bk 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;
e.g. `-5' is understood to mean `.\b.-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 `-' charac-
ters 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
which require no addresses regard the presence of an address
as an error. Commands which 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
`,\b,'. They may also be separated by a semicolon `;\b;'. In
this case the current line `.\b.' is set to the previous
address before the next address is interpreted. This
feature can be used to determine the starting line for for-
ward and backward searches (`/', `?'). The second address
of any two-address sequence must correspond to a line
Printed 7/9/88 April 29, 1985 3
ED(1) UNIX Programmer's Manual ED(1)
following the line corresponding to the first address. The
special form `%' is an abbreviation for the address pair
`1,$'.
In the following list of _\be_\bd 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 com-
mand 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 suf-
fixes may be combined in any order.
(.\b.)a
<text>
.\b.
The append command reads the given text and appends it
after the addressed line. `.\b.' 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.
(.\b., .\b.)c
<text>
.\b.
The change command deletes the addressed lines, then
accepts input text which replaces these lines. `.\b.' is
left at the last line input; if there were none, it is
left at the line preceding the deleted lines.
(.\b., .\b.)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 then the named file to be
read in. `.\b.' 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 file name in a
subsequent _\br or _\bw command. If `filename' is missing,
the remembered name is used.
E filename
This command is the same as _\be, except that no
Printed 7/9/88 April 29, 1985 4
ED(1) UNIX Programmer's Manual ED(1)
diagnostic results when no _\bw has been given since the
last buffer alteration.
f filename
The filename command prints the currently remembered
file name. 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 `.\b.' 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 `\'.
_\bA, _\bi, and _\bc commands and associated input are permit-
ted; the `.\b.' terminating input mode may be omitted if
it would be on the last line of the command list. The
commands _\bg and _\bv are not permitted in the command list.
(.\b.)i
<text>
.\b.
This command inserts the given text before the
addressed line. `.\b.' is left at the last line input,
or, if there were none, at the line before the
addressed line. This command differs from the _\ba com-
mand only in the placement of the text.
(.\b., .\b.+1)j
This command joins the addressed lines into a single
line; intermediate newlines simply disappear. `.\b.' is
left at the resulting line.
( .\b. )k_\bx
The mark command marks the addressed line with name _\bx,
which must be a lower-case letter. The address form
`'_\bx' then addresses this line.
(.\b., .\b.)l
The list command prints the addressed lines in an unam-
biguous way: non-graphic characters are printed in
two-digit octal, and long lines are folded. The _\bl com-
mand may be placed on the same line after any non-i/o
command.
(.\b., .\b.)m_\ba
The move command repositions the addressed lines after
the line addressed by _\ba. The last of the moved lines
becomes the current line.
Printed 7/9/88 April 29, 1985 5
ED(1) UNIX Programmer's Manual ED(1)
(.\b., .\b.)p
The print command prints the addressed lines. `.\b.' is
left at the last line printed. The _\bp command may be
placed on the same line after any non-i/o command.
(.\b., .\b.)P
This command is a synonym for _\bp.
q The quit command causes _\be_\bd to exit. No automatic write
of a file is done.
Q This command is the same as _\bq, except that no diagnos-
tic results when no _\bw 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 remem-
bered file name, if any, is used (see _\be and _\bf com-
mands). The file name is remembered if there was no
remembered file name already. Address `0' is legal for
_\br 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. `.\b.' is left at the last line
read in from the file.
( .\b., .\b.)s/regular expression/replacement/ or,
( .\b., .\b.)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. `.\b.' is left at the last line substi-
tuted.
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 `_\b\_\bn'
where _\bn is a digit, are replaced by the text matched by
the _\bn-th regular subexpression enclosed between `\('
and `\)'. When nested, parenthesized subexpressions
are present, _\bn is determined by counting occurrences of
`\(' starting from the left.
Lines may be split by substituting new-line characters
Printed 7/9/88 April 29, 1985 6
ED(1) UNIX Programmer's Manual ED(1)
into them. The new-line 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 _\bn_\bo
delimiters repeats the most recent substitute command
on the addressed lines. The `s' may be followed by the
letters r\br (use the most recent regular expression for
the left hand side, instead of the most recent left
hand side of a substitute command), p\bp (complement the
setting of the _\bp suffix from the previous substitu-
tion), or g\bg (complement the setting of the _\bg suffix).
These letters may be combined in any order.
(.\b., .\b.)t_\ba
This command acts just like the _\bm command, except that
a copy of the addressed lines is placed after address _\ba
(which may be 0). `.\b.' is left on the last line of the
copy.
(.\b., .\b.)u
The undo command restores the buffer to it's state
before the most recent buffer modifying command. The
current line is also restored. Buffer modifying com-
mands are _\ba, _\bc, _\bd, _\bg, _\bi, _\bk, and _\bv. For purposes of
undo, _\bg and _\bv are considered to be a single buffer
modifying command. Undo is its own inverse.
When _\be_\bd runs out of memory (at about 8000 lines on any
16 bit mini-computer such as the PDP-11) This full undo
is not possible, and _\bu can only undo the effect of the
most recent substitute on the current line. This res-
tricted undo also applies to editor scripts when _\be_\bd is
invoked with the -\b- option.
(1, $)v/regular expression/command list
This command is the same as the global command _\bg except
that the command list is executed _\bg with `.\b.' initially
set to every line _\be_\bx_\bc_\be_\bp_\bt 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 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 _\be and _\bf com-
mands). `.\b.' is unchanged. If the command is success-
ful, the number of characters written is printed.
(1, $)W filename
This command is the same as _\bw, except that the
Printed 7/9/88 April 29, 1985 7
ED(1) UNIX Programmer's Manual ED(1)
addressed lines are appended to the file.
(1, $)wq filename
This command is the same as _\bw except that afterwards a
_\bq command is done, exiting the editor after the file is
written.
x A key string is demanded from the standard input.
Later _\br, _\be and _\bw commands will encrypt and decrypt the
text with this key by the algorithm of _\bc_\br_\by_\bp_\bt(1). An
explicitly empty key turns off encryption.
(.\b.+1)z or,
(.\b.+1)z_\bn
This command scrolls through the buffer starting at the
addressed line. 22 (or _\bn, if given) lines are printed.
The last line printed becomes the current line. The
value _\bn is sticky, in that it becomes the default for
future _\bz commands.
($)= The line number of the addressed line is typed. `.\b.' is
unchanged by this command.
!<shell command>
The remainder of the line after the `!' is sent to
_\bs_\bh(1) to be interpreted as a command. `.\b.' is
unchanged.
(.\b.+1,.\b.+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. If two
addresses are present with no intervening semicolon, _\be_\bd
prints the range of lines. If they are separated by a
semicolon, the second line is printed.
If an interrupt signal (ASCII DEL) is sent, _\be_\bd prints
`?interrupted' and returns to its command level.
Some size limitations: 512 characters per line, 256 charac-
ters 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 core: each line takes 2 words.
When reading a file, _\be_\bd discards ASCII NUL characters and
all characters after the last newline. It refuses to read
files containing non-ASCII characters.
F\bFI\bIL\bLE\bES\bS
/tmp/e*
edhup: work is saved here if terminal hangs up
Printed 7/9/88 April 29, 1985 8
ED(1) UNIX Programmer's Manual ED(1)
S\bSE\bEE\bE A\bAL\bLS\bSO\bO
B. W. Kernighan, _\bA _\bT_\bu_\bt_\bo_\br_\bi_\ba_\bl _\bI_\bn_\bt_\br_\bo_\bd_\bu_\bc_\bt_\bi_\bo_\bn _\bt_\bo _\bt_\bh_\be _\bE_\bD _\bT_\be_\bx_\bt _\bE_\bd_\bi_\b-
_\bt_\bo_\br
B. W. Kernighan, _\bA_\bd_\bv_\ba_\bn_\bc_\be_\bd _\be_\bd_\bi_\bt_\bi_\bn_\bg _\bo_\bn _\bU_\bN_\bI_\bX
ex(1), sed(1), crypt(1)
D\bDI\bIA\bAG\bGN\bNO\bOS\bST\bTI\bIC\bCS\bS
`?name' for inaccessible file; `?self-explanatory message'
for other errors.
To protect against throwing away valuable work, a _\bq or _\be
command is considered to be in error, unless a _\bw has
occurred since the last buffer change. A second _\bq or _\be will
be obeyed regardless.
B\bBU\bUG\bGS\bS
The _\bl command mishandles DEL.
The _\bu_\bn_\bd_\bo command causes marks to be lost on affected lines.
The _\bx command, -\b-x\bx option, and special treatment of hangups
only work on UNIX.
Printed 7/9/88 April 29, 1985 9