ed(1) MPE/iX Shell and Utilities ed(1)
______________________________________________________________________
NAME
ed, red -- line-oriented text editor
SYNOPSIS
ed [-bsx] [-p prompt] [file]
red [-bsx] [-p prompt] [file]
DESCRIPTION
ed is a text editor that lets you manipulate text files interac-
tively. ed reads the text of a file into memory and stores it in
an area called a buffer. Various commands let you edit the text
in the buffer. Finally, you can write the contents of the buffer
back out to the file, overwriting the old contents of the file.
red is a restricted version of ed. It is intended to protect the
novice user by disallowing the ! command and the ability to
access files found anywhere but the current directory.
Options
Both ed and red accept the following options:
-b lets you edit larger files by restricting the amount of mem-
ory dedicated to paging. This frequently makes ed run
slower.
-p prompt
displays the given prompt string prompting you to input a
command. By default, ed does not usually prompt for command
input. See the description of the P command for more on
command prompting.
-s puts ed into a quiet mode, in which the E, e, r, and w com-
mands do not display file size counts; the e and qe commands
do not check buffer modification; and ed does not display !
after calling the shell to execute a sub-command. This mode
is particularly useful when you invoke ed from within a
shell script.
-x performs an X command to handle encrypted files properly.
See the description of the X command for more details.
- is an obsolete version of -s.
If the optional file argument is present on the command line, ed
reads the specified file into the editor by simulating an e file
command.
Addresses
You can prefix commands in ed with zero, one, or two addresses.
These addresses let you reference single lines or ranges of lines
in the buffer. You do not need to specify addresses for certain
commands that use default addresses. Consult the description for
1
ed(1) MPE/iX Shell and Utilities ed(1)
______________________________________________________________________
a particular command.
You can construct each address out of the following components:
. The single dot character represents the current line num-
ber. Many commands set the current line number. For
example, the e command sets it to the last line of the new
file being edited.
$ The dollar sign refers to the last line in the buffer.
n The number n refers to the nth line in the buffer.
/regexp/ This searches for a line containing a string that
matches the basic regular expression regexp (see
regexp(3)). The search begins at the line immediately
following the current line. It proceeds forward through
the buffer; if ed reaches the end of the buffer without
finding a match, it wraps around to the first line of the
buffer and continues the search. If ed does not find a
match, the search ends when it reaches the original cur-
rent line. If it finds a match, the address /regexp/
refers to the first matching line. If you omit regexp,
the last used regular expression becomes the object of the
search. You can omit the trailing /. Within regexp, \/
represents a literal slash and not the regexp delimiter.
?regexp? This similar to the previous address form, except that
the search goes backward through the buffer. If the
search reaches the first line in the buffer without find-
ing a match, ed wraps around and continues searching back-
ward from the last line in the buffer. If you omit
regexp, the last used regular expression becomes the
object of the search. You can omit the trailing ?.
Within regexp, \? represents a literal question mark and
not the regexp delimiter.
'l The address is the line marked with the mark name l. The
name l must be a lowercase letter set by the k command.
You can combine these basic addresses with numbers using the +
and - operators, with the usual interpretation. Missing left
operands default to . (dot); missing right operands default to 1.
Missing right operands also have a cumulative effect; so an
address of -- refers to the current line number less 2.
You can specify address ranges in the following ways:
a1,a2 specifies a range of addresses from address a1 to address
a2, inclusive. Omitting a1 and a2 (that is, specifying
only the comma), is equivalent to the range 1,$.
a1;a2 is similar to the previous form except that ed resets the
2
ed(1) MPE/iX Shell and Utilities ed(1)
______________________________________________________________________
current line number after calculating a1, so that the sec-
ond address, a2, is relative to a1. Omitting a1 and a2
(that is, specifying only the semicolon), is equivalent to
.;$.
If you specify only a1 and the command requires both a1
and a2, the command operates as though you specified a
range of
a1;. command
> is equivalent to .,.+22 (that is, page forward) except
that it never attempts to address any line beyond $.
< is equivalent to .-22,. (that is, page backward) except
that it never addresses any line before line 1.
Commands
Commands generally take a maximum of zero, one, or two addresses,
depending upon the particular command. In the following descrip-
tions, we show commands with their default addresses (that is the
addresses used when you don't specify any addresses) in a form
that shows the maximum number of legal addresses for the command.
The E, e, r, W, and w commands allow you to specify a file argu-
ment. For these commands, file can be either a path name or a
shell escape of the form:
!command-line
If you use the ! form, ed calls the shell identified by the SHELL
environment variable to execute command-line. For the E, e, and
r commands, ed reads the standard output of this command line in
the same way that it read the contents of a file. For the W and
w commands, the command line treats the addressed lines as stan-
dard input.
ed accepts the following commands:
.a appends text after the specified line. Valid addresses
range from 0 (text is placed at the beginning of the buffer,
before the first line) to $ (text is placed after the last
line of the buffer). ed reads lines of text from the termi-
nal until a line consisting solely of an unescaped . (dot)
is entered. ed sets the current line number to the last
line appended.
.,.c changes the addressed range of lines by deleting the lines
and then reading new text in the manner of the a or i com-
mands.
.,.d deletes the addressed range of lines. The line after the
last line deleted becomes the new current line. If you
3
ed(1) MPE/iX Shell and Utilities ed(1)
______________________________________________________________________
delete the last line of the buffer, ed sets the current line
number to the new last line. If no lines remain in the buf-
fer, it sets the current line number to 0.
E [file]
is similar to the e command, but ed gives no warning if you
have changed the buffer.
e [file]
replaces the contents of the current buffer with the con-
tents of file. If you did not specify file, ed uses the
remembered file name, if any. In all cases, the e command
sets the remembered file name to the file that it has just
read into the buffer. ed displays a count of the bytes in
the file unless it is in quiet mode. If you have changed
the current buffer since the last time its contents were
written out, ed displays a warning message and does not exe-
cute the command. If you enter the e command a second time,
ed and executes the command.
f [file]
changes the remembered file name to file. ed displays the
new remembered file name. If you do not specify file, ed
displays the current remembered file name.
1,$G/regexp/
is similar to the g command except that when ed finds a line
that matches regexp, it displays the line and waits for you
to type in the command to be executed. You cannot use the a
, c , i , G , g , V , and v commands. If you enter &, the G
command re-executes the last command you typed in. If you
press ENTER, G does not execute any command for that line.
1,$g/regexp/command
performs command on all lines that contain strings matching
the regular expression regexp. This command works in two
passes. In the first pass, ed searches the given range of
lines and marks all those that contain strings matching the
regular expression regexp, while the second pass actually
performs command on those lines. You cannot use !, G, g, V,
or v as command. command consists of one or more ed com-
mands, the first of which must appear on the same line as
the g commands. All lines of a multi-line command list,
except the last, must end with a backslash (\). If command
is empty, ed assumes it to be the p command. If no lines
match regexp, ed does not change the current line number;
otherwise, the current line number is the one set by the
last command in command. You can use any character other
than space or newline instead of the slash (/) to delimit
regexp.
H tells ed to display more descriptive messages when errors
occur. If ed is already displaying descriptive messages,
4
ed(1) MPE/iX Shell and Utilities ed(1)
______________________________________________________________________
because of a previous H command, issuing the H commands
returns to terse error messages. Normally, ed indicates
error messages by displaying a ?. When you turn on descrip-
tive error messages with this command, ed also displays the
descriptive message for the most recent ? message (see the
description of the h command).
h provides a brief explanation of the last error that
occurred. This does not change the current line number.
.i works similarly to the a command except that ed places the
text before the addressed line. Valid addresses range from
line 1 to $ (the last line). ed sets the current line num-
ber to the last inserted line.
.,.+1j
joins a range of lines into one line. To be precise, the j
command removes all newline characters from the addressed
range of lines, except for the last one. ed sets the cur-
rent line number to the resulting combined line.
.kl marks the addressed line with the mark name l which is a
single lowercase letter of the alphabet. This lets you
refer to a marked line with the construct 'l. This is called
an absolute address because it always refers to the same
line, regardless of changes to the buffer.
.,.l displays the addressed range of lines, representing non-
printable (control) characters visibly. ed sets the current
line number to the last line so displayed. You can append
this command to most other commands to check on the effect
of those commands.
.,.ma
moves the addressed lines to the point immediately following
the line given by the address a. The address a must not be
in the range of addressed lines. If address a is 0, ed
moves the lines to the beginning of the buffer. The last
line moved becomes the new current line.
.,.n displays the addressed lines in a way similar to the p com-
mand, but ed puts the line number and a tab character at the
beginning of each line. The last line displayed becomes the
new current line. You can append n to any command other
than E, e, f, Q, q, r, w, or ! to check on the effect of
that command.
P turns on command prompting. If you specified the -p prompt
option on the ed command line, ed displays the prompt string
whenever it is ready for you to type in another command. If
you did not specify -p, ed uses the * character as a prompt.
If command prompting is currently turned on, issuing the P
command turns it off.
5
ed(1) MPE/iX Shell and Utilities ed(1)
______________________________________________________________________
.,.p displays the addressed lines. The last line displayed
becomes the new current line. You can append p to any com-
mand other than E, e, f, Q, q, r, w, or ! to check on the
effect of that command.
Q quits unconditionally, without checking for buffer modifica-
tions.
q exits edIf you have made changes to the buffer since the
last save, ed issues a warning. Entering the q command
again lets you quit, regardless of unsaved changes.
$r [file]
reads the contents of file into the buffer after the
addressed line. If the address is 0, ed places the text
before the first line in the buffer. If you do not specify
file, ed uses the remembered file name; if no remembered
file name exists, file becomes the new remembered name. The
r command displays the number of bytes read from file unless
you specified the -s option. The last line read from the
file becomes the new current line. If file is replaced by !,
the rest of the line is considered a shell command line, the
output of which is to be read.
.,.s/regexp/new/[flags]
searches the specified range of lines for strings matching
the regular expression regexp. Normally the s command
replaces the first such matching string in each line with
the string new. The s command sets the current line number
to the last line on which a substitution occurred. If ed
makes no such replacements, ed considers it an error.
flags can be zero or more of the following:
n replaces the nth matching string in the line instead of
the first one.
g replaces every matching string in each line, not just the
first one.
l displays the new current line in the format of the l com-
mand.
n displays the new current line in the format of the n com-
mand.
p displays the new current line in the format of the p com-
mand.
You can use any single printable character other than the
space or newline instead of / to separate parts of the com-
mand provided that you use the same character to delimit all
6
ed(1) MPE/iX Shell and Utilities ed(1)
______________________________________________________________________
parts of the command. You may omit the trailing delimiter.
You can include a newline in the new string by putting a \
immediately in front of the newline. This is a good way to
split a line into two lines. If new consists only of the %
character, s uses the new string from the previous s com-
mand. If & appears anywhere in new, ed replaces it with the
text matching the regexp. If you want new to contain a lit-
eral ampersand or percent sign, put a backslash (\) in front
of the & or % character.
.,.ta
copies the addressed lines to the point after the line given
by the address a. The address a must not fall in the range
of addressed lines. If address a is 0, ed copies the lines
to the beginning of the buffer. This sets the current line
to the last line copied.
u rolls back the effect of the last command that changed the
buffer. For the purposes of u, commands that change the
buffer are: a, c, d, g, G, i, j, m, r, s, t, v, V, and (of
course) u. This means that typing u repeatedly flips the
most recent change back and forth. ed treats all changes
made by a global command (G, g, V, or v) as a single change.
As a result, such changes can be easily undone. This com-
mand sets the current line number to the value it had imme-
diately before you issued the command being undone.
1,$V/regexp/
is similar to the G command except that this command only
gives you the chance to edit lines that do not match the
given regular expression.
1,$v/regexp/commands
is similar to the g (global) command except that ed only
applies the given commands to lines that do not match the
given regular expression.
1,$W [file]
is similar to the w command except that this command appends
data to the given file if the file already exists.
1,$w [file]
writes the addressed lines of the buffer to the named file.
This does not change the current line number. If you do not
provide file, ed uses the remembered file name; if there is
no remembered file name, file becomes the remembered name.
If the output file does not exist, ed creates it. ed dis-
plays the number of characters written unless you had speci-
fied the -s option.
X prompts you to enter an encryption key. All subsequent e, r,
and w commands use this key to decrypt/encrypt text read
7
ed(1) MPE/iX Shell and Utilities ed(1)
______________________________________________________________________
from or written to files. To turn off encryption, issue an
X command and press RETURN in response to the prompt for an
encryption key.
!command
runs command as if you typed it to your chosen command
interpreter. If command contains the % character, ed
replaces it with the current remembered file name. If you
want a command to contain a literal %, put a backslash (\)
in front of the character. As a special case, typing !!
re-issues the previous command.
$= displays the line number of the addressed line. This does
not change the current line number.
.+1,.+1
if you supply zero, one, or two addresses without an expli-
cit command, ed displays the addressed lines in the mode of
the last display command: p, l, or n. This sets the current
line number to the last line displayed.
ENVIRONMENT VARIABLES
ed uses the following environment variables:
COLUMNS
contains the terminal width in columns. ed folds lines at
that point. If it is not set, ed uses the appropriate value
from the TERMINFO database or if that is not available, it
uses a default of 80.
HOME
contains the path name of your home directory.
SHELL
contains the full path name of the current shell.
TMPDIR
is the path name of the directory being used for temporary
files. If it is not set, MPE/iX Shell and Utilities uses
/tmp.
FILES
ed uses the following files:
/tmp/e*
This is the paging file. It holds a copy of the file being
edited. You can change the directory for temporary files
using the environment variable TMPDIR (see environ(3)).
ed.hup
ed writes the current buffer to this file when it receives a
hang-up signal.
8
ed(1) MPE/iX Shell and Utilities ed(1)
______________________________________________________________________
DIAGNOSTICS
Possible exit status values are:
0 Successful completion.
1 A non-usage error occurs
2 Usage error.
Messages
Message: ?
Cause: An error occurred while ed was in terse mode. This is
the default.
Action: For a more verbose description of the error, type h.
To switch to verbose mode, type H.
Message: Addressed line out of range
Cause: You specified an address for a command that referenced
a line that does not exist.
Action: Modify the address given to correctly reference the
desired lines.
Message: Badly constructed regular expression
Cause: You made an error in the syntax of a regular expres-
sion.
Action: Review the documentation on regular expressions (-
regexp(3)) and correct the offending syntax.
Message: Badly formed name
Cause: You specified an improperly formed or missing file name
with a command which requires a file name as an argu-
ment (for example, e or f).
Action: Correct or provide the offending file name.
Message: command not allowed inside g, v, G, or V
Cause: You specified a command that cannot be used with the
issued global command (g, v, G, or V).
Action: Check the Commands subsection of this man page for a
list of commands that cannot be used with the various
global commands
Message: Destination cannot straddle source in 'm' and 't'
Cause: You specified a range of lines to be moved or copied by
m or t that included the destination address.
Action: Ensure that the specified range of lines for m or t
does not include the destination address.
Message: File filename: system error
Cause: See syserror(3).
Action: See syserror(3).
9
ed(1) MPE/iX Shell and Utilities ed(1)
______________________________________________________________________
Message: File read error
Cause: An error occurred while reading a file into the page
buffer.
Action: Exit editor and restart. Check for system problems.
Contact your system manager.
Message: Global command too long
Cause: You specified a global instruction (g or v) that was
longer than 256 characters, including newlines.
Action: Specify a global instruction that is less than 256
characters in length.
Message: Illegal command redirection
Cause: You attempted to use the ! command redirection with the
f command.
Action: Do not use the ! command redirection with the f com-
mand.
Message: Illegal command suffix
Cause: You specified a command suffix for a command that does
not accept suffixes.
Action: Check the Commands subsection of this man page for a
list of ed commands and their syntaxes.
Message: Input line too long
Cause: You entered an ed command which was too long.
Action: Simplify the command and try again.
Message: Incomplete regular expression.
Cause: You issued a g or G command but did not provide a regu-
lar expression as an argument.
Action: Provide a regular expression as an argument to the
offending command.
Message: insufficient memory
Cause: There were not enough free system resources to perform
the specified operation.
Action: Free up more resources.
Message: Line(s) too long -- truncated
Cause: One or more lines in the file being edited were too
long for ed to handle. Those lines were truncated.
Action: To avoid losing data, exit immediately without saving.
Message: 'm' and 't' require destination address
Cause: You issued an m or t command but did not provide a des-
tination address.
Action: Provide a destination address with the m or t command.
Message: Mark name must be lower case
Cause: You attempted to use the k command to mark an addressed
line with a character other than a lowercase letter.
Action: Use k to mark the line with a lowercase letter.
10
ed(1) MPE/iX Shell and Utilities ed(1)
______________________________________________________________________
Message: Missing trailing delimiter after pattern.
Cause: You specified a pattern as part of a ed command but did
not delimit it.
Action: Provide a trailing delimiter for the pattern.
Message: Name too long
Cause: The file name specified on the ed command line was too
long.
Action: Use a shorter file name.
Message: Need space after command
Cause: You did not separate a command from its file name argu-
ment with a space.
Action: Re-enter the command with the required space.
Message: No match found for regular expression
Cause: The / command failed to find any matching lines.
Action: Try a different regular expression.
Message: no memory for line number tables
Cause: There were not enough free system resource to allocate
initial resources for ed.
Action: Free up more system resources and restart program.
Message: no memory for pages
Cause: There were not enough free system resources to allocate
initial resources for ed.
Action: Free up more system resources and restart program.
Message: No remembered file name
Cause: You tried to execute a command that uses a remembered
file name when there was no remembered file name.
Action: Issue the command again, but specify a file name this
time.
Message: No remembered regular expression
Cause: You attempted to use & to refer to a remembered regular
expression when there was no remembered regular expres-
sion.
Action: Issue the command again, but specify a regular expres-
sion this time.
Message: no space for line table
Cause: There were not enough free system resources to allocate
initial resources for ed.
Action: Free up more system resources and restart program.
Message: Only one file name is allowed.
Cause: You specified more than one file name on the command
line when you invoked ed.
Action: Specify only one file name when invoking ed.
11
ed(1) MPE/iX Shell and Utilities ed(1)
______________________________________________________________________
Message: Out of memory for lines
Cause: ed was unable to allocate system resources while trying
to insert or append lines to the buffer.
Action: Split the file into small pieces.
Message: Restricted shell
Cause: You invoked the restricted form of ed (red), but then
tried to use a command that is not allowed in the
restricted editor (the ! command).
Action: See the DESCRIPTION section of this man page for a dis-
cussion of the differences between ed and red.
Message: Result line of join too long
Cause: You attempt to use the j command to join a range lines
into one line; however, the resulting line would be too
long for ed to handle.
Action: Specify a smaller range of lines to be joined.
Message: Result of substitution would produce a line too long
Cause: You specified a replacement string in a substitution
command that would produce a line that is too long for
ed to handle.
Action: Specify a shorter replacement string or split the orig-
inal line into shorter lines before performing the sub-
stitution.
Message: Temporary file error
Cause: An error occurred when accessing the paging file (see
FILES).
Action: See your system manager.
Message: Undefined mark name
Cause: You attempted to reference a mark name that you have
not assigned.
Action: Use the k command
Message: Unknown command
Cause: You entered a command that does not exist in ed.
Action: Check the Commands subsection of this man page for a
list of valid ed commands.
Message: Unknown option "-option".
Cause: You specified an option that is not valid for ed.
Action: Check the DESCRIPTION section of this man page for a
list of valid ed options.
Message: Wrong number of addresses for command
Cause: You specified the wrong number of addresses for the
command that you entered.
Action: Check the Commands subsection of this man page for a
list of ed commands and the number of addresses that
you can specify with each.
12
ed(1) MPE/iX Shell and Utilities ed(1)
______________________________________________________________________
PORTABILITY
POSIX.2. x/OPEN Portability Guide 4.0. All UNIX systems.
The addresses < and >, the -b and -x options, and the W and X
commands are extensions to the POSIX standard.
The red command is also an extension to the POSIX standard.
LIMITS
ed allows a limit of 1024 bytes per line and 500,000 lines per
file. It does not allow the NUL character. The maximum length
of a global command is 256 characters, including newlines.
MPE/iX NOTES
Although the current MPE/iX implementation of the ed command can
read non-byte stream files, it can only write byte stream files.
As a result, if you edit a non-byte stream file with ed and save
it, that file is now a byte stream file. File characteristics
like file code, record size, and so forth are not preserved by
this conversion.
For more information on how the current MPE/iX implementation may
affect the operation of this utility, see Appendix A, MPE/iX
Implementation Considerations.
SEE ALSO
awk(1), crypt(1), diff(1), ex(1), grep(1), sed(1), vi(1),
environ(3), regexp(3)
13