ed - Use the ed line-oriented text editor

Format

ed [-Bbs] [-p prompt] [-W option[option] ... [file]

Description

ed is a line-oriented text editor that lets you manipulate text files interactively. It reads the text of a file into memory and stores it in an area called a buffer. You can edit the text in the buffer by using its subcommands. You can also write the contents of the buffer back out to the file, thereby overwriting the old contents of the file.

Options

-B
Disables the automatic conversion of tagged files. This option is ignored if the filecodeset or pgmcodeset options (-W option) are specified.
-b
Enables you to edit larger files by restricting the amount of memory dedicated to paging. Using a large amount of memory may make ed run more slowly.
-p prompt
Displays the given prompt string prompting you to input a subcommand. By default, ed does not typically prompt for subcommand input. See Subcommands for more information about subcommand prompting.
-s
Puts ed into a quiet mode, in which e, E, r, and w, subcommands do not display file size counts; the q and e subcommands do not check buffer modification; and ! is not displayed after calling the shell to run a subcommand. This mode is particularly useful when you invoke ed from within a shell script.
-W option[,option]...
Specifies z/OS-specific options. The option keywords are case-sensitive. Possible options are:
filecodeset=codeset
Performs text conversion from one code set to another when reading from the file. The coded character set of the file is codeset. codeset can be a code set name known to the system or a numeric coded character set identifier (CCSID). Note that the command iconv -l lists existing CCSIDs along with their corresponding code set names. The filecodeset and pgmcodeset options can be used on files with any file tag.

If pgmcodeset is specified but filecodeset is omitted, then the default file code set is ISO8859-1 even if the file is tagged with a different code set. If neither filecodeset nor pgmcodeset is specified, text conversion will not occur unless automatic conversion is enabled or the _TEXT_CONV environment variable indicates text conversion. For more information about text conversion, see Controlling text conversion for z/OS UNIX shell commands.

If filecodeset or pgmcodeset is specified, then automatic conversion is disabled for this command invocation and the -B option is ignored if it is also specified.

When specifying values for filecodeset, use the values that Unicode Service supports.

pgmcodeset=codeset
Performs text conversion from one code set to another when reading from the file. The coded character set of the program (command) is codeset. codeset can be a code set name that is known to the system or a numeric coded character set identifier (CCSID). Note that the command iconv -l lists existing CCSIDs along with their corresponding code set names. The filecodeset and pgmcodeset options can be used on files with any file tag.

If filecodeset is specified but pgmcodeset is omitted, then the default program code set is IBM-1047. If neither filecodeset nor pgmcodeset is specified, text conversion will not occur unless automatic conversion is enabled or the _TEXT_CONV environment variable indicates text conversion. For more information about text conversion, see Controlling text conversion for z/OS UNIX shell commands.

If filecodeset or pgmcodeset is specified, then automatic conversion is disabled for this command invocation and the -B option is ignored if it is also specified.

The only supported values for pgmcodeset are IBM-1047 and 1047.

If the optional file argument is present on the command line, ed reads the specified file into the editor by simulating an efile subcommand.

Addresses

You can prefix subcommands in ed with zero, one, or two addresses. These addresses let you refer to single lines or ranges of lines in the buffer. You do not need to specify addresses for certain subcommands that use default addresses. Consult the description for a particular subcommand. You can construct each address out of the following components:
.
The single dot character represents the current line in the buffer. Many subcommands set the current line; 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 regular expression, regexp. 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 current line. If it does find 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 is 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 finding a match, ed wraps around and continues searching backward 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 that is marked with the mark name l. The name l must be a lowercase letter that is set by the k subcommand.

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 two.

You can specify address ranges in the following ways:
a1,a2
Specifies a range of addresses from address a1 to address a2, inclusive. If you omit a1 and a2 (that is, the comma alone is specified), this is equivalent to the range 1,$.
a1;a2
Is similar to the previous form except that ed resets the current line after calculating the first address, a1, so that the second address, a2, is relative to a1. If you omit a1 and a2 (that is, the semicolon alone is specified), this 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.

Subcommands

An ed command has the form [address] command

All commands end with a newline; you must press <Enter>. Most commands allow only one command on a line, although you can modify commands by appending the l, n, and p commands.

Subcommands generally take a maximum of zero, one, or two addresses, depending upon the particular subcommand. In the following descriptions, 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 permitted addresses for the command. In any of the subcommands that take a file argument, file can be a path name or:
!command-line
If you use the ! form, ed runs the given command line, reading its standard output (stdout) or writing its standard input (stdin), depending on whether the ed command does reading or writing.
If a terminal disconnect is detected:
  • If the buffer is not empty and has changed since the last write, the ed utility will attempt to write a copy of the buffer to a file named ed.hup in the current directory. If this write fails, ed will attempt to write a copy of the buffer to a file name ed.hup in the directory that is named by the HOME environment variable. If both these attempts fail, ed will exit without saving the buffer.
  • The ed utility will not write the file to the currently remembered path name or return to command mode, and will terminate with the exit status of 1.
If an end-of-file is detected on standard input:
  • If the ed utility is in input mode, ed will terminate input mode and return to command mode. Any partially entered lines (that is, input text without a terminating newline) will be saved.
  • If the ed utility is in command mode, it will act as if a q command had been entered.
ed accepts the following subcommands:
.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 workstation until a line consisting solely of an unescaped . (dot) is entered. ed sets the current-line indicator 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 subcommands. If the variable _UNIX03 is set to YES, address 0 is valid for this subcommand and it will be interpreted as if address 1 were specified.
.,.d
Deletes the addressed range of lines. The line after the last line deleted becomes the new current line. If you delete the last line of the buffer, ed sets the current line to the new last line. If no lines remain in the buffer, it sets the current line 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 contents of file. The text conversion that is specified on the ed command (for example, the -B or -W option) is used. If you did not specify file, ed uses the remembered file name, if any. In all cases, the e subcommand 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, ed warns you if you try to run an e subcommand, and does not run the subcommand. If you enter the e subcommand a second time, ed goes ahead and runs 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 prints the line and waits for you to type in the subcommand to be run. You cannot use the a, c, i, g, G, v, and V subcommands. If you enter &, the G subcommand reruns the last subcommand you typed in. If you just press <Enter>, G does not run any subcommand for that line. Note that the subcommands input as part of the execution of the G subcommand can address and affect any lines in the buffer. If the variable _UNIX03 is set to YES, any line modified by the subcommand will be unmarked.
1,$g/regexp/command
Performs command on all lines that contain strings matching the regular expression regexp. This subcommand 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. The second pass performs command on those lines. If the variable _UNIX03 is set to YES, any line modified by the command will be unmarked. You cannot use !, g, G, V, or v as command. command consists of one or more ed subcommands, the first of which must appear on the same line as the g subcommand. All lines of a multiline command list, except the last, must end with a backslash (\). If command is empty, ed assumes it to be the p subcommand. 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 subcommand in command. Instead of the slash (/) to delimit regexp, you can use any character other than space or newline.
H
Tells ed to display more descriptive messages when errors occur. If ed is already printing descriptive messages, H returns to terse error messages. Normally, ed indicates error messages by displaying a ?. When you turn on descriptive error messages with this subcommand, ed also displays the descriptive message for the most recent ? message.
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 subcommand, 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 number to the last inserted line. If the variable _UNIX03 is set to YES, address 0 is valid for this subcommand and it will be interpreted as if address 1 were specified.
.,.+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 current line number to the resulting combined line.
.kx
Marks the addressed line with the mark name x, which is any single lowercase letter of the alphabet. This lets you refer to a marked line with the construct 'x. 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 nonprintable (control) characters in a visible manner. The end of each line will be marked with a '$' character. The characters listed in the Base Definitions volume of IEEE Std 1003.1-2001, Table 5-1, Escape Sequences and Associated Actions ( '\\', '\a', '\b', '\f', '\r', '\t', '\v' ) shall be written as the corresponding escape sequence; the '\n' in that table is not applicable. If the variable _UNIX03 is set to YES,'$' characters within the text will be written with a preceding backslash. ed sets the current line to the last line so displayed. You can append this subcommand to most other commands, to check on the effect of those subcommands.
.,.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 command, 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 subcommand (except for E, e, f, Q, r, w, or !) so that you can check on the effect that the subcommands had.
P
Turns on subcommand prompting if it is not already on. 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 subcommand. If you did not include the -p option, ed uses the * character as a prompt. If subcommand prompting is currently turned on, issuing the P subcommand turns it off.
.,.p
Displays (prints) the addressed lines. The last line displayed becomes the new current line. You can append p to most subcommands, so that you can check on the effect that the subcommands had.

You can append p to any subcommand (except for E, e, f, Q, r, w, or !) so that you can check on the effect that the subcommands had.

Q
Quits unconditionally, without checking for buffer changes.
q
Causes the editor to exit. If you have made changes to the buffer since the last save and you try to quit, ed issues a warning. Entering the q subcommand again lets you quit, regardless of unsaved changes.
$r [file]
Reads the contents of the file into the buffer after the addressed line. The text conversion that is specified on the ed command (for example, the -B or -W option) is used. 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. If file contains bytes that are not valid in the current character set, they are replaced by the rubout character.

The r subcommand 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 subcommand replaces the first such matching string in each line with the string new. The s subcommand sets the current line to the last line on which a substitution occurred. If ed makes no such replacements, ed considers it an error.
flags can be one 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 subcommand.
n
Displays the new current line in the format of the n subcommand.
p
Displays the new current line in the format of the p subcommand.
You can use any single printable character other than space or newline instead of / to separate parts of the subcommand provided that you use the same character to delimit all parts of the subcommand. You can 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 command. If the variable _UNIX03=YES is set and there was no previous s command, the use of % in this manner is an error. If & appears anywhere in new, ed replaces it with the text matching the regexp. If you want new to contain a literal 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 subcommand that changed the buffer. For the purposes of u, subcommands 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 switches the most recent change back and forth. This subcommand sets the current line number to the value it had immediately before the subcommand being undone started.
1,$V/regexp/
Is similar to the G subcommand, except that this subcommand gives you the chance to edit only those lines that do not match the given regular expression.
1,$v/regexp/commands
Is similar to the g (global) command, except that ed applies the given commands only to lines that do not match the given regular expression.
1,$W [file]
Is similar to the w subcommand, 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. The text conversion that is specified on the ed command (for example, the -B or -W option) is used. This does not change the current line. 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 displays the number of characters written unless you had specified the -s option.
X
Prompts you to enter an encryption key. All subsequent e, r, and w subcommands use this key to decrypt or encrypt text read from or written to files. To turn encryption off, issue an X subcommand 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 subcommand to contain a literal %, put a backslash (\) in front of the character. As a special case, typing !! reruns the previous command.
$=
Displays the line number of the addressed line. This does not change the current line.
.=
Displays the current line number.
.+1.,.+1
If you supply zero, one, or two addresses without an explicit subcommand, ed displays the addressed lines in the mode of the last print subcommand: p, l, or n. This sets the current line number to the last line displayed.

Examples

  1. To edit or browse a file using the ed line-oriented text editor:
    ed myFile
  2. To edit or browse a file containing UTF-8 characters using the ed line-oriented text editor, assuming that
    • The text file is untagged and you do not want to tag it or enable automatic conversion, and
    • You cannot alter the tag (for example, you are browsing an untagged public text file or a read-only text file)
    then issue:
    ed -W filecodeset=UTF-8,pgmcodeset=IBM-1047 myUtf8File
  3. To edit or browse a file containing EBCDIC characters using the ed line-oriented text editor, assuming that automatic conversion has been enabled but the text file is incorrectly tagged as ASCII:
    ed -B myMisTaggedFile

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
The path name of the directory being used for temporary files. If it is not set, ed uses /tmp.
_TEXT_CONV
Contains text conversion information for the command. The text conversion information is not used when either the -B option or the filecodeset or pgmcodeset option (-W option) is specified. For more information about text conversion, see Controlling text conversion for z/OS UNIX shell commands.
_UNIX03
For more information about the effect of _UNIX03 on this command, see Shell commands changed for UNIX03.

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.
ed.hup
When ed receives a hang up signal (or detects a terminal disconnect) and the current buffer has changed since the last write, ed will attempt to write the current buffer to ed.hup in the current directory. If this write fails, ed will attempt to write the current buffer to ed.hup in the $HOME directory.

Localization

ed uses the following localization environment variables:
  • LANG
  • LC_ALL
  • LC_COLLATE
  • LC_CTYPE
  • LC_MESSAGES
  • LC_SYNTAX
  • NLSPATH

Exit values

0
Successful completion
1
Failure due to any of the following reasons:
  • Addressed line out of range
  • Only one file name is allowed
  • No space for the line table
  • Temporary file error
  • Badly constructed regular expression
  • No remembered regular expression
  • File read error
  • Out of memory
  • Unknown command
  • Command suffix not permitted
  • No match found for regular expression
  • Wrong number of addresses for the subcommand
  • Not enough space after the subcommand
  • The name is too long
  • Badly formed name
  • Subcommand redirection is not permitted
  • Restricted shell
  • No remembered file name
  • The mark name must be lowercase
  • The mark name is not defined
  • m and t subcommands require a destination address
  • The destination cannot straddle source in m and t
  • A subcommand not allowed inside g, v, G, or V
  • The x subcommand has become X (uppercase)
  • The global command is too long
  • Write error (no disk space)
  • The code set is not valid
  • Could not turn off automatic conversion
  • Could not perform requested text conversion
2
Usage error

Messages

Some error messages are issued only if h or H subcommands are used after ed displays ?. Possible error messages include:

Destination cannot straddle source in m and t
The range of lines being moved or copied by m or t cannot include the destination address.
Global command too long
There is a limit on the length of a global instruction (g or v). See Limits for this limit.
m and t require destination address
You must follow the m or t subcommands with an address indicating where you want to move or copy text. You omitted this address.
No remembered file name
You tried to run a subcommand that used a remembered file name (for example, you used w to write without specifying an output file name). However, there is no remembered file name at present. Run the subcommand again, but specify a file name this time.
Restricted shell
The command line invoked the restricted form of ed, but you tried an action that was not allowed in the restricted editor (the ! subcommand).
Temporary file error
You ran out of space on disk or encountered other errors involving the page file stored in the temporary file.
Warning: file not saved
You entered a subcommand to quit editing the current file, for example, q or e to edit a new file; however, you have changed the file since the last time you saved it. ed is suggesting that you save the file before you exit it; otherwise, your recent changes will be lost. To save the file, use the w command. If you really do not want to save the recent changes, use q to quit or e to edit a new file.
?file
An error occurred during an attempt to open or create file. This is applicable to the e, r, and w subcommands.
?
An unspecified error occurred. Use the h or H subcommand for more information. If the input to ed comes from a script rather than from a workstation, ed exits when any error occurs.

Limits

ed allows a limit of 1024 bytes per line and 28 000 lines per file. It does not allow the NUL ('\0') character. The maximum length of a global command is 256 characters, including newlines.

Portability

POSIX.2, X/Open Portability Guide, UNIX systems.

The addresses < and >, the -B, -b, and -W options, and the W and X subcommands are extensions of the POSIX standard.

Related information

awk, diff, env, ex, grep, sed, vi

See Regular expressions (regexp) for more information about regexp.