'\"macro stdmacro
.if n .pH g1.ed @(#)ed	40.16 of 10/10/89
.\" Copyright 1989 AT&T
.nr X
.if \nX=0 .ds x} ed 1 "Essential Utilities" "\&"
.if \nX=1 .ds x} ed 1 "Essential Utilities"
.if \nX=2 .ds x} ed 1 "" "\&"
.if \nX=3 .ds x} ed "" "" "\&"
.TH \*(x}
.if t .ds ^ \^\s+4\v@.3m@^\v@-.3m@\s-4\^
.if n .ds ^ ^
.\" ed command in running text
.de Ec
\f4\\$1\fP\\$2
..
.\" Copyright (c) 1988 Sun Microsystems, Inc - All Rights Reserved.
.SH NAME
\f4ed\f1, \f4red\f1 \- text editor
.SH SYNOPSIS
\f4ed\f1
\f1[\f4\-s\f1]
\f1[\f4\-p\f1
.IR string " ]"
\f1[\f4\-x\f1]
\f1[\f4\-C\f1]
.RI [ file ]
.PP
\f4red\f1
\f1[\f4\-s\f1]
\f1[\f4\-p\f1
.IR string " ]"
\f1[\f4\-x\f1]
\f1[\f4\-C\f1]
.RI [ file ]
.SH DESCRIPTION
\f4ed\fP
is the standard text editor.
If the
.I file\^
argument is given,
\f4ed\fP
simulates an
.Ec e
command (see below) on the named file; that is to say,
the file is read into
\f4ed\fP's
buffer so that it can be edited.
.TP
\f4\-s\f1
Suppresses the printing
of character counts by
.Ec e ,
.Ec r ,
and
.Ec w
commands,
of diagnostics from
.Ec e
and
.Ec q
commands,
and of the
\f4!\f1
prompt after a
\f4!\fP\f2shell\ command\^\fP.
.TP
\f4\-p\f1
Allows the user to specify a prompt string.
.TP
\f4\-x\f1
Encryption option; when used,
\f4ed\fP
simulates an
\f4X\f1
command and prompts the user for a key.
This key is used to encrypt and decrypt
text using the algorithm of
\f4crypt\fP(1).
The
\f4X\f1
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
\f4\-x\f1
option.
See
\f4crypt\fP(1).
Also, see the
.SM NOTES
section at the end
of this manual page.
.TP
\f4\-C\f1
Encryption option; the same
as the
\f4\-x\f1
option, except that
\f4ed\fP
simulates a
\f4C\f1
command.
The
\f4C\f1
command is like the
\f4X\f1
command,
except that
all text read in is assumed to have been encrypted.
.PP
\f4ed\fP
operates on a copy of the file it is editing; changes made
to the copy have no effect on the file until a
.Ec w
(write)
command is given.
The copy of the text being edited resides
in a temporary file called the
.IR buffer .
There is only
one buffer.
.PP
\f4red\fP
is a restricted version of
\f4ed\fP.
It will only allow editing of files in the current directory.
It prohibits executing shell commands via
\f4!\fP\f2shell\ command\^\fP.
Attempts to bypass these restrictions result
in an error message (\f2restricted shell\fP).
.PP
Both
\f4ed\fP
and
\f4red\fP
support the
\f4fspec\fP(4)
formatting capability.
After including a
format specification
as the first line of
.I file
and invoking
\f4ed\fP
with
your terminal in
\f4stty\ \-tabs \f1
or
\f4stty\ tab3\f1
mode [see
\f4stty\fP(1)],
the specified tab stops will automatically be used when
scanning
.IR file .
For example, if the first line of a file contained:
.RS
.ft 4
\&<:t5,10,15\ s72:>
.ft 1
.RE
tab stops would be set at columns 5, 10, and 15, and a maximum line
length of 72 would be imposed.
.SM NOTE\*S:
when you are entering text into the file,
this format is not in effect;
instead, because of being in
\f4stty\ \-tabs \f1
or
\f4stty\ tab3\f1
mode,
tabs are expanded to every eighth column.
.PP
Commands to
\f4ed\fP
have a simple and regular structure: zero, one, or two
.I addresses\^
followed by a single-character
.IR 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.
.PP
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
\f4ed\fP
is accepting text, it is said
to be in
.IR "input mode" .
In this mode,
.I no\^
commands are recognized;
all input is merely collected.
Leave input mode by typing a period (\^\f4.\fP\^) at the
beginning of a line,
followed immediately by a carriage return.
.PP
\f4ed\fP
supports a limited form of
.I "regular expression\^"
notation;
regular expressions are used in addresses to specify
lines and in some commands
(e.g.,
.Ec s )
to specify portions of a line that are to be substituted.
A regular expression (\s-1RE\s+1) specifies
a set of character strings.
A member of this set of strings is said to be
.I matched\^
by the \s-1RE\s+1.
The \s-1RE\s+1s allowed by
\f4ed\fP
are constructed as follows:
.PP
The following
.I one-character\^
.IR \s-1RE\s+1 s
match a
.I single\^
character:
.TP "\w'1.1\ \ \ \ 'u"
1.1
An ordinary character
(\c
.I not\^
one of those discussed in 1.2 below)
is a
one-character \s-1RE\s+1
that matches itself.
.TP "\w'1.1\ \ \ \ 'u"
1.2
A backslash (\f4\^\e\fP) followed by any special character is a
one-character \s-1RE\s+1
that matches the special character itself.
The special characters are:
.RS
.TP "\w'a.\ \ \ \ 'u"
a.
\&\f4.\fP,
\f4\(**\fP,
\f4[\fP,
and
\f4\^\e\fP (period, asterisk, left square bracket,
and backslash, respectively), which are always special,
.I except\^
when they appear within square brackets (\^\f4[\|]\fP\^; see 1.4 below).
.TP
b.
\*^ (caret or circumflex), which is special at the
.I beginning\^
of an
.I entire\^
\s-1RE\s+1
(see 4.1 and 4.3 below),
or when it immediately follows the left of a pair of square brackets (\^\f4[\|]\fP\^) (see 1.4 below).
.TP
c.
\f4$\fP (dollar sign), which is special at the
\f4end\fP
of an
\f2entire\f1 \s-1RE\s+1
(see 4.2 below).
.TP
d.
The character used to bound
(i.e., delimit) an entire \s-1RE\s+1, which is special for that RE (for example,
see how slash (\^\f4/\fP\^) is used in the
.Ec g
command, below.)
.RE
.TP "\w'1.1\ \ \ \ 'u"
1.3
A period (\^\f4.\fP\^) is a
one-character \s-1RE\s+1
that matches any character except new-line.
.TP
1.4
A non-empty string of characters enclosed in
square brackets (\^\f4[\|]\fP\^) is a
one-character \s-1RE\s+1
that matches
.I "any one\^"
character in that string.
If, however, the first character of the string is a circumflex
(\*^), the
one-character \s-1RE\s+1
matches any character
.I except\^
new-line and the remaining characters in the string.
The \*^ has this special meaning
.I only\^
if it
occurs first in the string.
The minus (\f4\-\fP) may be used to indicate a range of consecutive
characters;
for example, \f4[0\-9]\fP is equivalent to \f4[0123456789]\fP.
The \f4\-\fP loses this special meaning if it occurs first (after
an initial \*^, if any)
or last in the string.
The right square bracket (\^\f4]\fP\^)
does not terminate such a string when it
is the first character within it (after an initial \*^, if any);
e.g., \f4[\|]a\-f]\fP matches either a right square
bracket (\^\f4]\fP\^) or one of the
.SM ASCII
letters \f4a\fP through \f4f\fP inclusive.
The four characters listed in 1.2.a above stand for themselves
within such a string of characters.
.PP
The following rules may be used to construct
.IR \s-1RE\s+1 s
from
one-character
\s-1RE\s+1s:
.TP "\w'1.1\ \ \ \ 'u"
2.1
A
one-character \s-1RE\s+1
is a
\s-1RE\s+1
that matches whatever the
one-character \s-1RE\s+1 matches.
.br
.ne 5
.TP
2.2
A
one-character \s-1RE\s+1
followed by an asterisk (\f4\(**\fP) is a
\s-1RE\s+1
that matches
.I zero\^
or more occurrences of the
one-character \s-1RE\s+1.
If there is any choice,
the longest leftmost string that permits a match is chosen.
.TP
2.3
A
one-character \s-1RE\s+1
followed by \f4\^\e{\fP\^\f2m\fP\^\f4\e}\fP,
\f4\^\e{\fP\^\f2m,\fP\^\f4\e}\fP,
or
\f4\^\e{\fP\^\f2m,n\fP\^\f4\e}\fP is a
\s-1RE\s+1
that matches a
.I range\^
of occurrences of the
one-character \s-1RE\s+1.
The values of
.I m
and
.I n
must be non-negative integers less than 256;
\f4\^\e{\fP\^\f2m\fP\^\f4\e}\fP
matches
.I exactly\^
.I m
occurrences;
\f4\^\e{\fP\^\f2m,\fP\^\f4\e}\fP
matches
.I "at least\^"
.I m
occurrences;
\f4\^\e{\fP\^\f2m,n\fP\^\f4\e}\fP
matches
.I "any number\^"
of occurrences
.I between\^
.I m
and
.I n
inclusive.
Whenever a choice exists,
the
\s-1RE\s+1
matches as many occurrences as possible.
.TP
2.4
The concatenation of
\s-1RE\s+1s
is a
\s-1RE\s+1
that matches the concatenation of the strings matched by each component of the
\s-1RE\s+1.
.TP
2.5
A
\s-1RE\s+1
enclosed between the character sequences
\f4\^\e(\fP and \f4\^\e)\fP
is a
\s-1RE\s+1
that matches whatever the unadorned
\s-1RE\s+1
matches.
.TP
2.6
The expression \f4\^\e\fP\f2n\fP\^ matches the same string of characters
as was
matched by an expression enclosed between \f4\^\e(\fP and \f4\^\e)\fP
.I earlier\^
in the same \s-1RE\s+1.
Here
.I n
is a digit;
the sub-expression specified is that beginning with the
.IR n -th
occurrence of \f4\^\e(\fP counting from the left.
For example, the expression \*^\f4\e(.\(**\e)\e1$\fP matches a line
consisting of two repeated appearances of the same string.
.PP
A RE may be constrained to match words.
.TP "\w'1.1\ \ \ \ 'u"
3.1
\f4\\<\fP constrains a RE to match the beginning of a string or to follow
a character that is not a digit, underscore, or letter.
The first character matching the RE must be a digit, 
underscore, or letter.
.TP
3.2
\f4\\>\fP constrains a RE to match the end of a 
string or to precede
a character that is not a digit, underscore, or letter.
.PP
An
.I entire\^
.I \s-1RE\s+1\^
may be constrained to match only an initial segment or final segment
of a line (or both).
.TP "\w'1.1\ \ \ \ 'u"
4.1
A circumflex (\*^) at the beginning of an
entire \s-1RE\s+1
constrains that \s-1RE\s+1 to match an
.I initial\^
segment of a line.
.TP
4.2
A dollar sign (\^\f4$\fP\^)
at the end of an
entire \s-1RE\s+1
constrains that
\s-1RE\s+1
to match a
.I final\^
segment of a line.
.TP
4.3
The construction
\%\*^\f2entire \s-1RE\s+1\fP\|\f4$\fP
constrains the
entire \s-1RE\s+1
to match the entire line.
.PP
The null \s-1RE\s+1 (e.g., \f4//\fP)
is equivalent to the last \s-1RE\s+1 encountered.
See also the last paragraph before
.SM FILES
below.
.PP
To understand addressing in
\f4ed\fP
it is necessary to know that at any time there is a
.IR "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.
.I Addresses\^
are constructed as follows:
.bp
.TP "\w'1.1\ \ \ \ 'u"
\01.
The character \f4.\fP addresses the current line.
.TP
\02.
The character \f4$\fP addresses the last line of the buffer.
.TP
\03.
A decimal number
.I n
addresses the
.IR n -th
line of the buffer.
.TP
\04.
\f2\(fmx\fP\^ addresses the line marked with the
mark name character
.IR x ,
which must be an \s-1ASCII\s0 lower-case letter
\f1(\f4a\f1\-\f4z\f1).
Lines are marked with the
.Ec k
command described below.
.TP
\05.
A \s-1RE\s+1 enclosed by slashes (\^\f4/\fP\^) addresses
the first line found by searching
.I forward\^
from the line
.I following\^
the current line
toward the end of the
buffer and stopping at the first line containing a
string matching the \s-1RE\s+1.
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
.SM FILES
below.
.TP
\06.
A \s-1RE\s+1 enclosed in question marks (\^\f4?\fP\^) addresses
the first line found by searching
.I backward\^
from the line
.I preceding\^
the current line
toward the beginning of
the buffer and stopping at the first line containing
a string matching the \s-1RE\s+1.
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
.SM FILES
below.
.TP
\07.
An address followed by a plus sign (\^\f4+\fP\^)
or a minus sign (\f4\-\fP) followed by a decimal number specifies that address plus
(respectively minus) the indicated number of lines.
A shorthand for .+5 is .5.
.TP
\08.
If an address begins with \f4+\fP or \f4\-\fP,
the addition or subtraction is taken with respect to the current line;
e.g, \f4\-5\fP is understood to mean \f4.\-5\fP.
.TP
\09.
If an address ends with \f4+\fP or \f4\-\fP,
then 1 is added to or subtracted from the address, respectively.
As a consequence of this rule and of Rule 8, immediately above,
the address \f4\-\fP 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 \f4\-\fP.)
Moreover,
trailing
\f4+\fP and \f4\-\fP characters
have a cumulative effect, so \f4\-\-\fP refers to the current
line less 2.
.TP
10.
For convenience, a comma (\^\f4,\fP\^) stands for the
address pair \f41,\^$\fP,
while a semicolon (\^\f4;\fP\^) stands for the
pair \f4.\^,\^$\fP.
.PP
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 an insufficient number of addresses is given;
if more addresses are given than such a command requires,
the last one(s) are used.
.PP
Typically, addresses are separated from each other by a comma
(\^\f4,\fP\^).
They may also be separated by a semicolon
(\^\f4;\fP\^).
In the latter case,
the first address is calculated,
the current line (.) is set to that value,
and then
the second address is calculated.
This feature can be used to 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 in the buffer that follows
the line corresponding to the first address.
.bp
.PP
In the following list of
\f4ed\fP
commands, the parentheses shown prior to the command are
.I not\^
part of
the address; rather they show the default address(es)
for the command.
.PP
It is generally illegal for more than one
command to appear on a line.
However, any command
(except
.Ec e ,
.Ec f ,
.Ec r ,
or
.Ec w )
may be suffixed by \f4l\fP, \f4n\fP,
or \f4p\fP in which case
the current line is either
listed, numbered or printed, respectively,
as discussed below under the
.Ec l ,
.Ec n ,
and
.Ec p
commands.
.HP
\f4(\|.\|)a\f1
.br
.ns
.HP
<text>
.br
.ns
.HP
.if t .rs
.if t .sp -.5v
\&\f4.\fP
.br
The
.Ec a ppend
command accepts zero or more lines of text
and appends it after the addressed line in the buffer.
The current line (\&\f4.\fP) is left
at the last inserted line, or, if there
were none, at the addressed line.
Address 0 is legal for this command: it causes the ``appended'' text
to be placed
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).
.HP
\f4(\|.\|)c\f1
.br
.ns
.HP
<text>
.br
.ns
.HP
.if t .rs
.if t .sp -.5v
\&\f4.\fP
.br
The
.Ec c hange
command deletes the addressed lines from the buffer, then accepts zero
or more lines of
text that replaces these lines in the buffer.
The current line (\&\f4.\fP) is left at the last line input, or,
if there were none,
at the first line that was not deleted.
.HP
\f4C\f1
.br
Same as the
\f4X\f1
command, described later, except that
\f4ed\fP
assumes all text read in for the
\f4e\f1
and
\f4r\f1
commands is encrypted unless a null key is typed in.

.HP
\f4(\|.\|,\|.\|)d\f1
.br
The
.Ec d elete
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.
.HP
\f4e\f2 file\^\f1
.br
The
.Ec e dit
command deletes the entire contents of the buffer
and then reads the contents of \f2file\f1 into the buffer.
The current line (\&\f4.\fP) is set to the last line of the buffer.
If \f2file\f1 is not given, the currently remembered file name, if any, is used
(see the
.Ec f
command).
The number of characters read in is printed;
.I file\^
is remembered for possible use as a default file name
in subsequent
.Ec e ,
.Ec r ,
and
.Ec w
commands.
If
.I file\^
is replaced by \f4!\fP, the rest of the line
is taken to be a
shell [\f4sh\fP\^(1)]
command whose output is to be read in.
Such a
shell
command is
.I not\^
remembered as the current file name.
See also
.SM DIAGNOSTICS
below.
.HP
\f4E\f2 file\^\f1
.br
The
.Ec E dit
command is like
.Ec e ,
except that the editor does not check to see
if any changes have been made to the buffer since
the last
.Ec w
command.
.HP
\f4f\f2 file\^\f1
.br
If
.I file\^
is given,
the
.Ec f \|ile-name
command changes
the currently remembered file name to
.IR file ;
otherwise, it prints the currently remembered file name.
.HP
\f4(\|1\|,\|$\|)g/\f2\s-1RE\s+1\f4/\f2command list\^\f1
.br
In the
.Ec g lobal
command, the first step is to mark every line that matches
the given
\s-1RE\s+1.
Then, for every such line, the
given
.I "command list\^"
is executed with the current line (\f4.\fP) 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 \f4\^\e\fP;
.Ec a ,
.Ec i ,
and
.Ec c
commands and associated input are permitted.
The \f4.\fP terminating input mode may be omitted if it would be the
last line of the
.IR "command list" .
An empty
.I "command list\^"
is equivalent to the
.Ec p
command.
The
.Ec g ,
.Ec G ,
.Ec v ,
and
.Ec V
commands
are
.I not\^
permitted in the
.IR "command list" .
See also the
.SM NOTES
and the last paragraph before
.SM FILES
below.
.HP
\f4(\|1\|,\|$\|)G/\f2\s-1RE\s+1\f4/\^\f1
.br
In the interactive
.Ec G lobal
command, the first step
is to mark every line that matches the given
\s-1RE\s+1.
Then, for every such line, that line is printed,
the current line (\f4.\fP) is changed to that line, and
any
.I one\^
command (other than one of the
.Ec a ,
.Ec c ,
.Ec i ,
.Ec g ,
.Ec G ,
.Ec v ,
and
.Ec 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 \f4&\fP causes the re-execution of the most recent command executed within
the current invocation of
.Ec G .
Note that the
commands input
as part of the execution of the
.Ec G
command
may
address and affect
.I any\^
lines in the buffer.
The
.Ec G
command can be terminated by an interrupt signal (\s-1ASCII\s0 \s-1DEL\s0 or \s-1BREAK\s0).
.HP
.br
\f4h\f1
.br
The
.Ec h elp
command gives a short error message that explains
the reason for the most recent \f4?\fP diagnostic.
.HP
\f4H\f1
.br
The
.Ec H elp
command causes
\f4ed\fP
to enter a mode in which error messages are
printed for all subsequent \f4?\fP diagnostics.
It will also explain the previous \f4?\fP if
there was one.
The
.Ec H
command alternately turns this mode
on and off; it is initially off.
.HP
\f4(\|.\|)i\f1
.br
.ns
.HP
<text>
.br
.ns
.HP
.if t .rs
.if t .sp -.5v
\&\f4.\fP
.br
The
.Ec i nsert
command accepts zero or more lines of text and
inserts it before the addressed line in the buffer.
The current line (\&\f4.\fP) is left at the last inserted line, or,
if there were none,
at the addressed line.
This command differs from the
.Ec 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).
.HP
\f4(\|.\|,\|.+1\|)j\f1
.br
The
.Ec j oin
command joins contiguous lines by removing the appropriate new-line characters.
If exactly one address is given, this command does nothing.
.HP
\f4(\|.\|)k\f2x\^\f1
.br
The mar\f4k\fP\^ command marks the addressed line with
name
.IR x ,
which must be an \s-1ASCII\s0 lower-case letter
\f1(\f4a\f1\-\f4z\f1).
The address \f2\(fmx\fP\^ then addresses this line;
the current line (\&\f4.\fP) is unchanged.
.HP
\f4(\|.\|,\|.\|)l\f1
.br
The
.Ec l ist
command
prints the addressed lines in an unambiguous way:
a few non-printing characters (e.g.,
.IR "tab, backspace" )
are represented by visually mnemonic overstrikes.
All other non-printing
characters are
printed in octal,
and long lines are folded.
An
.Ec l
command may be appended to any command
other than
.Ec e ,
.Ec f ,
.Ec r ,
or
.Ec w .
.HP
\f4(\|.\|,\|.\|)m\f2\^a\^\f1
.br
The
.Ec m ove
command repositions the addressed line(s) after the line
addressed by
.IR a .
Address \f40\fP is legal for
.I a
and causes the addressed line(s) to be moved to
the beginning of the file.
It is an error if address
.I a
falls within the range of moved lines;
the current line (\&\f4.\fP) is
left at the last line moved.
.HP
\f4(\|.\|,\|.\|)n\f1
.br
The
.Ec n umber
command prints the addressed lines,
preceding each line by its
line number and a tab character;
the current line (\&\f4.\fP) is
left at the last line printed.
The
.Ec n
command
may
be appended to any command
other than
.Ec e ,
.Ec f ,
.Ec r ,
or
.Ec w .
.HP
\f4(\|.\|,\|.\|)p\f1
.br
The
.Ec p rint
command prints the addressed lines;
the current line (\&\f4.\fP) is
left at the last line printed.
The
.Ec p
command
may
be appended to any command
other than
.Ec e ,
.Ec f ,
.Ec r ,
or
.Ec w .
For example, \f4dp\fP\^
deletes the current line and prints the
new current line.
.HP
.br
\f4P\f1
.br
The editor will prompt with a \f4\(**\fP for
all subsequent commands.
The
.Ec P
command alternately turns this mode on and off; it is
initially off.
.HP
\f4q\f1
.br
The
.Ec q uit
command causes
\f4ed\fP
to exit.
No automatic write
of a file is done;
however, see
.SM DIAGNOSTICS ,
below.
.HP
.br
\f4Q\f1
.br
The editor exits without
checking if changes have been made in the buffer since the
last
.Ec w
command.
.HP
\f4(\|$\|)r\f2 file\^\f1
.br
The
.Ec r ead
command
reads the contents of \f2file\f1 into the buffer.
If \f2file\f1 is not given,
the currently remembered file name, if any, is used
(see the
.Ec e
and
.Ec f
commands).
The currently remembered file name is
.I not\^
changed unless
.I file\^
is the very first file name
mentioned
since
\f4ed\fP
was invoked.
Address 0 is legal for
.Ec r
and causes the
file to be read in at the beginning of the buffer.
If the read is successful, the number of characters
read in is printed;
the current line (\&\f4.\fP) is set to the last line read in.
If
.I file\^
is replaced by \f4!\fP, the rest of the line
is taken to be a
shell [see \f4sh\fP\^(1)]
command whose output is to be read in.
For example,
\f4$r !ls\fP
appends current directory
to the end of the file being edited.
Such a
shell
command is
.I not\^
remembered as the current file name.
.HP
\f4(\|.\|,\|.\|)s/\f2\s-1RE\s+1\f4/\f2replacement\f4/\^\f1
\ \ \ \ \ \ \ \ or
.br
.ns
.HP
\f4(\|.\|,\|.\|)s/\f2\s-1RE\s+1\f4/\f2replacement\f4/g\^\f1
\ \ \ \ \ \ \ or
.br
.ns
.HP
\f4(\|.\|,\|.\|)s/\f2\s-1RE\s+1\f4/\f2replacement\f4/\f2n\^\f1
\ \ \ \ \ \ \ \f2n\fP = 1-512
.br
The
.Ec s ubstitute
command searches each addressed
line for an occurrence of the specified \s-1RE\s+1.
In each line in which a match is found,
all (non-overlapped) matched strings are
replaced by the
.I replacement\^
if the global replacement indicator \f4g\fP appears after the command.
If the global indicator does not appear, only the first occurrence
of the matched string is replaced.
If a number
.IR n ,
appears after the command, only the
.IR n -th
occurrence of the matched string on each addressed line is replaced.
It is an error if the substitution fails on
.I all\^
addressed lines.
Any character other than space or new-line
may be used instead of \f4/\fP to delimit the \s-1RE\s+1
and
the
.IR replacement ;
the current line (\&\f4.\fP) is left at the last line on which
a substitution occurred.
See also the last paragraph before
.SM FILES
below.
.IP
An ampersand (\^\f4&\fP\^) appearing in the
.I replacement\^
is replaced by the string matching the \s-1RE\s+1 on the current line.
The special meaning of \f4&\fP in this context may be
suppressed by preceding it by \f4\^\e\fP\|.
As a more general feature,
the characters
\f4\^\e\fP\f2n\fP\^,
where
.I n
is a digit,
are replaced by the text matched by the
.IR n -th
regular subexpression
of the specified \s-1RE\s+1
enclosed between \f4\^\e\|(\fP and \f4\^\e\|)\fP.
When
nested parenthesized subexpressions
are present,
.I n
is determined by counting occurrences of \f4\^\e\|(\fP starting from the left.
When the character \f4%\fP is the only character
in the
.IR replacement ,
the
.I replacement\^
used in the most recent substitute
command is used as the
.I replacement\^
in the current substitute command.
The \f4%\fP loses its special meaning when it is
in a replacement string of more than one
character or is preceded by a \f4\^\e\fP\|.
.IP
A line may be split by substituting a new-line character into it.
The new-line in the
.I replacement\^
must be escaped by preceding it by \f4\^\e\fP\|.
Such substitution cannot be done as part of a
.Ec g
or
.Ec v
command list.
.HP
\f4(\|.\|,\|.\|)t\f2a\^\f1
.br
This command acts just like the
.Ec m
command, except that a
.I copy\^
of the addressed lines is placed
after address
.Ec a
(which may be 0);
the current line (\f4.\fP) is left at the last line copied.
.HP
\f4u\f1
.br
The
.Ec u ndo
command nullifies the effect of the most recent
command that modified anything in the buffer, namely
the most recent
.Ec a ,
.Ec c ,
.Ec d ,
.Ec g ,
.Ec i ,
.Ec j ,
.Ec m ,
.Ec r ,
.Ec s ,
.Ec t ,
.Ec v ,
.Ec G ,
or
.Ec V
command.
.HP
\f4(\|1\|,\|$\|)v/\f2\s-1RE\s+1\f4/\f2command list\^\f1
.br
This command is the same as the global command
.Ec g ,
except that the
lines marked during the first step are those that do
.I not
match the
\s-1RE\s+1.
.HP
\f4(\|1\|,\|$\|)V/\f2\s-1RE\s+1\f4/\^\f1
.br
This command is the same as the interactive global command
.Ec G ,
except
that the lines that are marked during the first step are those that do
.I not\^
match the
\s-1RE\s+1.
.HP
\f4(\|1\|,\|$\|)w\f2 file\^\f1
.br
The
.Ec w rite
command writes the addressed lines into \f2file\f1.
If \f2file\f1 does not exist,
it is created with mode \f4666\fP (readable and writable by everyone),
unless your
file creation mask
dictates otherwise; see the description of the
\f4umask\f1
special command on
\f4sh\fP(1).
The currently remembered file name is
.I not\^
changed unless
.I file\^
is the very first file name mentioned since
\f4ed\fP
was invoked.
If no file name is given,
the currently remembered file name, if any, is used
(see the
.Ec e
and
.Ec f
commands);
the current line (\&\f4.\fP) is unchanged.
If the command is successful, the number of characters written is
printed.
If
.I file\^
is replaced by \f4!\fP, the rest of the line
is taken to be a
shell [see \f4sh\fP\^(1)]
command whose standard input is the addressed lines.
Such a
shell
command is
.I not\^
remembered as the current file name.
.HP
\f4(\|1\|,\|$\|)W\f2 file\^\f1
.br
This command is the same as the 
.Ec w rite
command above, except that it appends the
addressed lines
to the end of 
.I file
if it exists.  
If
.I file
does not exist, it is created
as described above for the \f2w\f1 command.
.HP
\f4X\f1
.br
A key is prompted for, and it is used in
subsequent
.Ec e ,
.Ec r ,
and
.Ec w
commands to decrypt
and encrypt text using the
\f4crypt\fP(1)
algorithm.
An educated guess is made to determine whether
text read in for the
.Ec e
and
.Ec r
commands is encrypted.
A null key turns off encryption.
Subsequent
.Ec e ,
.Ec r ,
and
.Ec w
commands will use this key to encrypt or decrypt the text
[see
\f4crypt\fP(1)].
An explicitly empty key turns off encryption.
Also, see the
\f4\-x\f1
option of
\f4ed\fP.
.HP
\f4(\|$\|)=\f1
.br
The line number of the addressed line is typed;
the current line (\f4.\fP) is unchanged by this command.
.HP
\f4!\f2shell\| command\^\f1
.br
The remainder of the line after the \f4!\fP is sent
to the
.SM UNIX
system shell [see \f4sh\fP\^(1)] to be interpreted as a command.
Within the text of that command, the unescaped character
\f4%\f1
is replaced with the remembered file name;
if a
\f4!\f1
appears as the first character of the shell command,
it is replaced with the text of the previous shell command.
Thus,
\f4!!\f1
will repeat the last shell command.
If any expansion is performed, the expanded line is echoed;
the current line (\&\f4.\fP) is unchanged.
.br
.ne 5
.HP
\f4(\|.+1\|)\f1<new-line>
.br
An address alone on a line causes the addressed line to be printed.
A new-line alone is equivalent to \f4.+1p\fP; it is useful
for stepping forward through the buffer.
.PP
If an interrupt signal (\s-1ASCII\s0 \s-1DEL\s0 or \s-1BREAK\s0) is sent,
\f4ed\fP
prints a \f4?\fP and returns to
.I its\^
command level.
.PP
Some size limitations:
512 characters in a line,
256 characters in a global command list,
and 64 characters in the pathname of a file
(counting slashes).
The limit on the number of lines depends on the amount of user memory:
each line takes 1 word.
.PP
When reading a file,
\f4ed\fP
discards
.SM ASCII
.SM NUL
characters.
.PP
If a file is not terminated by a new-line character,
\f4ed\f1
adds one and puts out a message explaining what it did.
.PP
If the closing delimiter of a \s-1RE\s+1 or of a
replacement string (e.g., \f4/\fP) would be the last
character before a new-line,
that delimiter may be omitted, in
which case the addressed line
is printed.
The following pairs of commands are equivalent:
.PD 0
.RS
.TP 16
.ft 4
s/s1/s2
s/s1/s2/p
.ft 1
.TP
.ft 4
g/s1
g/s1/p
.ft 1
.TP
.ft 4
?s1
?s1?
.ft 1
.RE
.PD
.ne 8
.SH FILES
.PD 0
.TP 12
\f4$TMPDIR\f1
if this environmental variable is not null,
its value is used in place of
\f4/var/tmp\f1
as the directory name for the temporary work file.
.TP
\f4/var/tmp\fP
if
\f4/var/tmp\f1
exists, it is used as the directory name for the
temporary work file.
.TP
\f4/tmp\fP
if the environmental variable
\f4TMPDIR\f1
does not exist or is null, and if
\f4/var/tmp\f1
does not exist, then
\f4/tmp\f1
is used as the directory name for the temporary work file.
.TP
\f4ed\s+4.\s-4hup\fP
work is saved here if the terminal is hung up.
.PD
.SH "SEE ALSO"
\f4edit\fP(1), \f4ex\fP(1), \f4grep\fP(1), \f4sed\fP(1), \f4sh\fP(1), \f4stty\fP(1), \f4umask\fP(1), \f4vi\fP(1).
.br
\f4fspec\fP(4),
\f4regexp\fP(5) in the \f2System Administrator's Reference Manual\f1.
.SH DIAGNOSTICS
.TP 12
\f4?\f1
for command errors.
.TP
\f4?\f2file\^\f1
for an inaccessible file.
.br
(use the
.Ec h elp
and
.Ec H elp
commands for detailed explanations).
.PD
.PP
If changes have been made in the buffer since the last
.Ec w
command that wrote the entire buffer,
\f4ed\fP
warns the user if an attempt is made to destroy
\f4ed\fP's
buffer via the
.Ec e
or
.Ec q
commands.
It
prints
\f4?\f1
and
allows one to continue editing.
A second
.Ec e
or
.Ec q
command
at this point will take effect.
The \f4\-s\fP command-line option inhibits this feature.
.SH NOTES
The
\f4\-\f1
option, although it continues to be supported,
has been replaced in the documentation by the
\f4\-s\f1
option that follows the
Command Syntax Standard
[see
\f4intro\fP(1)].
.PP
The encryption options and commands are provided
with the Security Administration Utilities package,
which is available only in the United States.
.PP
A
.Ec !
command cannot be subject to a
.Ec g
or a
.Ec v
command.
.PP
The
.Ec !
command
and the
\f4!\f1
escape from the
.Ec e ,
.Ec r ,
and
.Ec w
commands
cannot be used if the editor is invoked from a restricted
shell [see
\f4sh\fP(1)].
.PP
The sequence \f4\^\en\fP in a \s-1RE\s+1 does not
match a new-line character.
.PP
If the editor input is coming from a command file (e.g.,
\f4ed\f1
.I file
\f4<\f1
.IR ed_cmd_file ),
the editor exits at the first failure.
.\"	@(#)ed.1	6.2 of 9/2/83
.Ee
