V10/man/man1/ed.1

Compare this file to the similar file:
Show the results in this format:

.TH ED 1
.CT 1 editor
.ds . (\|\f5.\fP\|)
.ds .. (\|\f5.,.\fP\|)
.ds 1$ (\|\f51,$\fP\|)
.SH NAME
ed, e \- text editor
.SH SYNOPSIS
.B ed
[
.B -
]
[
.B -o
]
[
.I file
]
.SH DESCRIPTION
.I Ed
is the standard text editor;
.I e
is another name for it.
.PP
If a
.I file
argument is given,
.I ed
simulates an
.L e
command (see below) on that file:
it is read into
.I ed's
buffer so that it can be edited.
The options are
.TP
.B -
Suppress the printing
of character counts by
.LR e ,
.LR r ,
and
.L w
commands and of the confirming 
.L !
by
.L !
commands.
.TP
.B -o
(for output piping)
Place on the standard error file all output except writing by
.L w
commands.
If no 
.I file
is given, make
.F /dev/stdout
the remembered file; see the
.L e
command below.
.PP
.I Ed
operates on a `buffer', a copy of the file it is editing;
changes made
in the buffer have no effect on the file until a
.L w
(write)
command is given.
The copy of the text being edited resides
in a temporary file called the 
.IR buffer .
.PP
Commands to
.I ed
have a simple and regular structure: zero or
more
.I addresses
followed by a single character
.I command,
possibly
followed by parameters to the command.
These addresses specify one or more lines in the buffer.
Missing addresses are supplied by default.
.PP
In general, only one command may appear on a line.
Certain commands allow the 
addition of text to the buffer.
While
.I ed
is accepting text, it is said
to be in
.I  "input mode."
In this mode, no commands are recognized;
all input is merely collected.
Input mode is left by typing a period 
.L .
alone at the
beginning of a line.
.PP
.I Ed
supports a limited form of
.I "regular expression"
notation.
A regular expression specifies
a set of strings of characters.
A member of this set of strings is said to be
.I matched
by the regular expression.
In the following specification for regular expressions
the word `character' means any character but newline.
.IP 1.
Any character except a special character
matches itself.
Special characters are
the regular expression delimiter plus
.L \e[.
and sometimes
.LR ^*$ .
.IP 2.
A
.B .
matches any character.
.IP 3.
A
.L \e
followed by any character except a digit,
.LR ( ,
or
.L )
matches that character.
.IP 4.
A nonempty string
.I s
bracketed
.BI [ \|s\| ]
(or
.BI [^ s\| ])
matches any character in (or not in)
.I s.
In 
.I s,
.L \e
has no special meaning, and
.L ]
may only appear as
the first letter.
A substring 
.IB a - b ,
with
.I a
and
.I b
in ascending
.SM ASCII 
order, stands for the inclusive
range of
.SM ASCII 
characters.
.IP 5.
A regular expression of form 1-4 followed by
.L *
matches a sequence of
0 or more matches of the regular expression.
.IP 6.
A regular expression,
.I x,
of form 1-8, bracketed
.BI \e( \|x\| \e)
matches what
.I x
matches.
.IP 7.
A
.L \e
followed by a digit 
.I n
matches a copy of the string that the
bracketed regular expression beginning with the
.IR n th
.L \e(
matched.
.IP 8.
A regular expression of form 1-8,
.I x,
followed by a regular expression of form 1-7,
.I y
matches a match for
.I x
followed by a match for
.I y,
with the
.I x
match being as long as possible while still permitting a
.I y
match.
.IP 9.
A regular expression of form 1-8, or a null string, preceded by
.L ^
(and/or followed by
.LR $ ),
is constrained to matches that
begin at the left (and/or end at the right) end of a line.
.IP 10.
A regular expression of form 1-9 picks out the
longest among the leftmost matches in a line.
.IP 11.
An empty regular expression stands for a copy of the
last regular expression encountered.
.PP
Regular expressions are used in addresses to specify
lines and in one command
(see
.I 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 `\e'.
This also applies to the character bounding the regular
expression (often 
.LR / )
and to
.L \e
itself.
.PP
To understand addressing in
.I ed
it is necessary to know that at any time there is a
.I "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.
.TP
1.
The character
.LR . ,
customarily called `dot',
addresses the current line.
.TP
2.
The character 
.L $
addresses the last line of the buffer.
.TP
3.
A decimal number
.I n
addresses the
.IR n -th
line of the buffer.
.TP
4.
.BI \'x
addresses the line marked with the name
.IR x ,
which must be a lower-case letter.
Lines are marked with the
.L k
command described below.
.TP
5.
A regular expression enclosed in slashes 
.L /
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.
.TP
6.
A regular expression enclosed in queries 
.L ?
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.
.TP
7.
An address followed by a plus sign 
.L +
or a minus sign
.L -
followed by a decimal number specifies that address plus
(resp. minus) the indicated number of lines.
The plus sign may be omitted.
.TP
8.
An address followed by 
.L +
(or
.LR - )
followed by a
regular expression enclosed in slashes specifies the first
matching line following (or preceding) that address.
The search wraps around if necessary.
The 
.L +
may be omitted, so
.L 0/x/
addresses the
.I first
line in the buffer with an 
.LR x .
Enclosing the regular expression in 
.L ?
reverses the search direction.
.TP
9.
If an address begins with 
.L +
or
.L -
the addition or subtraction is taken with respect to the current line;
e.g.\&
.L -5
is understood to mean
.LR .-5 .
.TP
10.
If an address ends with 
.L +
or
.LR - ,
then 1 is added (resp. subtracted).
As a consequence of this rule and rule 9,
the address
.L -
refers to the line before the current line.
Moreover,
trailing
.L +
and
.L -
characters
have cumulative effect, so
.L --
refers to the current
line less 2.
.TP
11.
To maintain compatibility with earlier versions of the editor,
the character 
.L ^
in addresses is 
equivalent to
.LR - .
.PP
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.
.PP
Addresses are separated from each other typically by a comma
.LR , .
They may also be separated by a semicolon
.LR ; .
In this case the current line 
is set to
the previous address before the next address is interpreted.
If no address precedes a comma or semicolon, line 1 is assumed;
if no address follows, the last line of the buffer is assumed.
The second address of any two-address sequence
must correspond to a line following the line corresponding to the first address.
.PP
In the following list of
.I 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.
`Dot' means the current line.
.TP
.RB \*. \|a
.br
.ns
.TP
<text>
.br
.ns
.TP
.B .
Read the given text
and append it after the addressed line.
Dot is left
on the last line input, if there
were any, otherwise at the addressed line.
Address 
.L 0
is legal for this command; text is placed
at the beginning of the buffer.
.TP
.RB \*(.. \|b [ +- ][\fIpagesize\fP][ pln\fR]
Browse.
Print a `page', normally 20 lines.
The optional 
.L +
(default) or
.L -
specifies whether the next or previous
page is to be printed.
The optional
.I pagesize
is the number of lines in a page.
The optional
.LR p ,
.LR n ,
or 
.L l
causes printing in the specified format, initially
.LR p .
Pagesize and format are remembered between 
.L b
commands.
Dot is left at the last line displayed.
.TP
.RB \*(.. \|c
.br
.ns
.TP
<text>
.br
.ns
.TP
.B .
.br
Change.
Delete the addressed lines, then accept input
text to replace these lines.
Dot is left at the last line input; if there were none,
it is left at the line preceding the deleted lines.
.TP
.RB \*(.. \|d
Delete the addressed lines from the buffer.
Dot is set to the line following the last line deleted, or to
the last line of the buffer if the deleted lines had no successor.
.TP
.BI e " filename"
Edit.
Delete the entire contents of the buffer;
then read the named file into the buffer.
Dot is set to the last line of the buffer.
The number of characters read is typed.
The file name is remembered for possible use in later
.LR e ,
.LR r ,
or
.L w
commands.
If
.I filename
is missing, the remembered name is used.
.TP
.BI E " filename"
Unconditional
.LR e ;
see `DIAGNOSTICS' below.
.TP
.BI f " filename"
Print the currently remembered file name.
If
.I filename
is given,
the currently remembered file name is first changed to
.I filename.
.TP
.RB \*(1$ \|g/\fIregular\ expression\fP/\fIcommand\ list\fP
.PD 0
.TP
.RB \*(1$ \|g/\fIregular\ expression\fP/
.TP
.RB \*(1$ \|g/\fIregular\ expression\fP
.PD
Global.
First mark every line which matches
the given
.I regular expression.
Then for every such line, execute the
.I command list
with dot 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 end with
.LR \e .
The
.RB \&` \&. \&'
terminating input mode for an
.LR a ,
.LR i ,
.L c
command may be omitted if it would be on the
last line of the command list.
The commands
.L g
and
.L v
are not permitted in the command list.
Any character other than space or newline may
be used instead of 
.L /
to delimit the regular expression.
The second and third forms mean
.BI g/ regular\ expression /p .
.TP
.RB (\| .\| ) \|i
.PD 0
.TP
<text>
.TP
.B .
Insert the given text before the addressed line.
Dot is left at the last line input, or, if there were none,
at the line before the addressed line.
This command differs from the
.I a
command only in the placement of the
text.
.PD
.TP
.RB (\| .,.+1 \|) \|j
Join the addressed lines into a single line;
intermediate newlines are deleted.
Dot is left at the resulting line.
.TP
.RB \*. \|k\fIx\fP
Mark the addressed line with name
.I x,
which must be a lower-case letter.
The address form
.BI \' x
then addresses this line.
.ne 2.5
.TP
.RB \*(.. \|l
List.
Print the addressed lines in an unambiguous way:
a tab is printed as
.LR \et ,
a backspace as
.LR \eb ,
backslashes as
.LR \e\e ,
and non-printing characters are
printed as a backslash followed by three octal digits.
Long lines are folded,
with the second and subsequent sub-lines indented one tab stop.
If the last character in the line is a blank,
it is followed by
.LR \en .
An
.L l
may be appended, like
.LR p ,
to any non-I/O command.
.TP
.RB \*(.. \|m\fIa
Move.
Reposition the addressed lines after the line
addressed by
.IR a .
Dot is left at the last moved line.
.TP
.RB \*(.. \|n
Number.
Perform
.LR p ,
prefixing each line with its line number and a tab.
An
.L n
may be appended, like
.LR p ,
to any non-I/O command.
.TP
.RB \*(.. \|p
Print the addressed lines.
Dot is left at the last line printed.
A
.L p
appended to any non-I/O command causes the then current line
to be printed after the command is executed.
.TP
.RB \*(.. \|P
This command is a synonym for
.LR p .
.TP
.B q
Quit the editor.
No automatic write
of a file is done.
.TP
.B Q
Quit unconditionally; see `DIAGNOSTICS' below.
.TP
.RB ( $ )\|r\ \fIfilename\fP
Read in the given file after the addressed line.
If no
.I filename
is given, the remembered file name is used.
The file name is remembered if there were no
remembered file name already.
If the read is successful, the number of characters
read is typed.
Dot is left at the last line read in from the file.
.TP
.RB \*(.. \|s\fIn\fP/\fIregular\ expression\fP/\fIreplacement\fP/
.PD 0
.TP
.RB \*(.. \|s\fIn\fP/\fIregular\ expression\fP/\fIreplacement\fP/g
.TP
.RB \*(.. \|s\fIn\fP/\fIregular\ expression\fP/\fIreplacement\fP
.PD
Substitute.
Search each addressed
line for an occurrence of the specified regular expression.
On each line in which
.I n
matches are found
.RI ( n
defaults to 1 if missing),
the
.IR n th
matched string is replaced by the replacement specified.
If the global replacement indicator 
.L g
appears after the command,
all subsequent matches on the line are also replaced.
It is an error for the substitution to fail on all addressed lines.
Any character other than space or newline
may be used instead of 
.L /
to delimit the regular expression
and the replacement.
Dot is left at the last line substituted.
The third form means
.BI s n / regular\ expression / replacement\fP/p\fR.
The second
.L /
may be omitted if the replacement is
empty.
.IP
An ampersand 
.L &
appearing in the replacement
is replaced by the string matching the regular expression.
The characters
.BI \e n,
where
.I n
is a digit,
are replaced by the text matched by the
.IR n -th
regular subexpression
enclosed between
.L \e(
and
.LR \e) .
When
nested, parenthesized subexpressions
are present,
.I n
is determined by counting occurrences of
.LR \e (
starting from the left.
.IP
A literal 
.LR & ,
.LR / ,
.L \e
or newline may be included in a replacement
by prefixing it with
.LR \e .
.TP
.RB \*(.. \|t\|\fIa
Transfer.
Copy the addressed lines 
after the line addressed by
.I a.
Dot is left at the last line of the copy.
.TP
.RB \*(.. \|u
Undo.
Restore the preceding contents
of the current line, which must be the last line
in which a substitution was made.
.TP
.RB \*(1$ \|v/\fIregular\ expression\fP/\fIcommand\ list\fP
.PD 0
.TP
.RB \*(1$ \|v/\fIregular\ expression\fP/
.TP
.RB \*(1$ \|v/\fIregular\ expression\fP
.PD
This command is the same as the global command
.L g
except that the command list is executed with
dot initially set to every line
.I except
those
matching the regular expression.
.TP
.RB \*(1$ \|w " \fIfilename\fP"
Write the addressed lines onto
the given file.
If the file does not exist,
it is created with mode 666 (readable and writable by everyone).
If no
.I filename
is given, the remembered file name, if any, is used.
The file name is remembered if there were no 
remembered file name already.
Dot is unchanged.
If the write is successful, the number of characters written is
printed.
.TP
.RB \*(1$ \|W " \fIfilename\fP"
Perform
.LR w ,
but append to, instead of overwriting, any existing file contents.
.TP
.RB ( $ ) \|=
Print the line number of the addressed line.
Dot is unchanged.
.TP
.BI ! shell\ command
Send the remainder of the line after the 
.L !
to
.IR sh (1)
to be interpreted as a command.
Dot is unchanged.
.TP
.RB (\| .+1 )\|<newline>
An address without a command is taken as a
.L p 
command.
A terminal
.L /
may be omitted from the address.
A blank line alone is equivalent to
.LR .+1p ;
it is useful
for stepping through text.
.PP
If an interrupt signal 
.SM (ASCII DEL)
is sent,
.I ed
prints a 
.L ?
and returns to its command level.
.PP
When reading a file,
.I ed
discards
.SM ASCII NUL
characters
and all characters after the last newline.
It refuses to read files containing non-\c
.SM ASCII
characters.
.SH FILES
.F /tmp/e*
.br
.F ed.hup
\ \ work is saved here if terminal hangs up
.SH "SEE ALSO"
.IR sam (9.1), 
.IR sed (1), 
.IR vi (1)
.SH DIAGNOSTICS
.BI ? name
for inaccessible file;
.L ?TMP
for temporary file overflow;
.L ?
for errors in commands or other overflows.
.PP
To protect against throwing away valuable work,
a
.L q
or
.L e
command is considered to be in error if the buffer has
been modified since the last
.LR w ,
.LR q ,
or
.L e
command.