ED(1V)

NAME
ed, red - line editor

SYSTEM V SYNOPSIS
ed [ - ] [ -p string ] [ -x ] [ filename ]

SYSTEM V DESCRIPTION
Ed is the standard System V line editor. If the filename argument is given, ed reads the named file into the buffer for editing.

Ed operates on a copy of the file it is editing; changes made to the copy have no effect on the file until a w (write) command is given. The copy being edited resides in a temporary file called the buffer. There is only one buffer.

Red is a restricted version of ed. It will only allow editing of files in the current directory, and prohibits executing shell commands with the ! command. Attempts to bypass these restrictions result in an error message (restricted shell).

OPTIONS

-   Suppress printing of character counts (by e, r, and w commands), diagnostics (by e and q commands), and the ! prompt (after a ! command).

-p string
    Specify a prompt string.

-x  Edit an encrypted file (see crypt(1) for details.

USAGE

  Command Structure

ed commands have a simple and regular structure: zero, one, or two addresses are followed by a single-character command, which may be followed by parameters for that command.

    [address[ ,address ]]command [parameter]

A single address specifies a single line in the buffer. A pair specifies an inclusive range. Commands that requires an address uses certain addresses by default (typically the address of the current line).

In general, only one command can appear on a line. Certain commands allow you to insert or add text, which is placed in the appropriate location in the buffer. While accepting text, ed is said to be in input mode. In this mode, no commands are recognized; all input is added or inserted into the buffer. To exit input mode, type a period (.) by itself on a line.

  Addresses

Generally speaking, the current line is the last line affected by a command. Explicit addresses are constructes as follows:

.   Addresses the current line.

$   Addresses the last line of the buffer.

n   A decimal number n addresses the n'th line in the buffer.

′x  addresses the line marked with the mark character x, which must be a lower-case letter. (Lines are marked with the k command).

/RE/
    An RE is a regular expression, as described below. When enclosed by slashes, it addresses the first line found by searching for a string that it matches. The search proceeds forward from the line following the current line. 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.

?RE?
    An RE enclosed in question marks addresses the first line containing a match found by searching backward from the line preceding the current line. If necessary, the search wraps around to the end of the buffer and continues up to and including the current line.

addr+n
addr-n
    An address followed by a plus sign (+) or a minus sign (-), followed by a decimal number, specifies that base address plus or minus the indicated number of lines. (The plus sign may be omitted.) If addr is omitted, the current line is used as the base. For instance, 31-3 addresses the 28'th line in the buffer.

    If an address ends with + or -, then `1' is added to or subtracted from the base address, respectively. The address - refers to the line preceding the current line. (To maintain compatibility with earlier versions of the editor, the character in addresses is equivalent to -.) Trailing + and - characters have a cumulative effect, so -- refers to the current line, less 2.

    For convenience, a comma (,) stands for the address pair 1,$, while a semicolon (;) stands for the pair .,$.

  Regular Expressions

Ed supports a limited form of "regular expression" notation, which can be used in an address to specify lines by their contents. A regular expression (RE) specifies a set of character strings to match. These strings are built up from the following "single-character" RE's:

c   Any ordinary character not listed below. An ordinary character matches itself.

\   Backslash. When followed by a special character, the RE matches the special character itself.

.   Period (or "dot"). Matches any single character.

^   As the leftmost character, a carat (or circumflex) constrains the RE to the leftmost segment of a line.

$   As the rightmost character, a dollar-sign (or currency symbol) constrains the RE to the rightmost segment of a line.

    The construction RE$

*   When it follows any RE that matches a single-character, an asterisk (or "star") matches a string composed of zero or more occurrences of the RE.

d   The delimiting character for the RE.

[c...]
    A nonempty string of characters, enclosed in square brackets, matches any single character in the string. When the first character of the string is a carat (^), then the RE matches any character except those in the remainder of the string. A carat in any other position is taken as an ordinary character. The right square bracket doesn't terminate the enclosed string if it is the first character (after an initial ^, if any).

[l-r]
    The minus sign, between two characters, indicates a range of consecutive ASCII characters to match. For instance, the range [0-9] is equivalent to the string [0123456789]. The - is treated as an ordinary character if it occurs first (or first after an initial ^) or last in the string.

d   The character used to delimit an RE is special for that RE (for example, see how / is used in the g command, below).

The following rules and special characters allow for constructing RE's from single-character RE's:

    The concatenation of RE's matches the concatenation of text strings, each of which is matched by a component RE.

*   A one-character RE, followed by an asterisk (*) matches zero or more occurrences of the one-character RE. If there is a choice, the longest leftmost string that permits a match is chosen.

\{m\}
\{m,\}
\{m,n\}
    A one-character RE followed by \{m\}, \{m,\}, or \{m,n\} is an RE that matches a range of occurrences of the one-character RE. The values of m and n must be nonnegative integers less than 256; \{m\} matches exactly m occurrences; \{m,\} matches at least m occurrences; \{m,n\} matches any number of occurrences between m and n inclusive. Whenever a choice exists, the RE matches as many occurrences as possible.

\(...\)
    An RE enclosed between the character sequences \( and \) matches whatever the unadorned RE matches. The expression \n matches the same string of characters as was matched by an expression enclosed between \( and \) earlier in the same RE. Here n is a digit; the subexpression specified is that beginning with the n-th occurrence of \( counting from the left. For example, the expression \(.*\)\1$ matches a line consisting of two repeated appearances of the same string. constrains the entire RE to match the entire line.

//  The null RE (//) is equivalent to the last RE encountered.

  Commands

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.

Typically, addresses are separated from each other by a comma (,). They may also be separated by a semicolon (;). In the latter case, the current line (.) is set to the first address, and only then is the second address calculated. This feature can be used to determine the starting line for forward and backward searches. The second address of any two-address sequence must correspond to a line that follows, in the buffer, the line corresponding to the first address.

In the following list of ed commands, the default addresses are shown in parentheses. The parentheses are not part of the address; they show that the given addresses are the default.

It is generally illegal for more than one command to appear on a line. However, any command (except e, f, r, or w) may be suffixed by l, n, or p in which case the current line is either listed, numbered or printed, respectively).

(.)a
text
.   Read the given text and append it after the addressed line; . 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 NEWLINE
     character).

(.)c
text
.   The change command deletes the addressed lines, then accepts input text that replaces those lines; . is left at the last line input, or, if there were none, at the first line that was not deleted.

(.,.)d
    The delete 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.

e file
    The edit command causes the entire contents of the buffer to be deleted, and then the named file to be read in; . is set to the last line of the buffer. If no filename is given, the currently-remembered filename, if any, is used (see the f command). The number of characters read is typed; file is remembered for possible use as a default filename in subsequent e, r, and w commands. If file is replaced by !, the rest of the line is taken to be a shell (sh(1)) command from which output is read. Such a shell command is not remembered as the current filename. See also DIAGNOSTICS below.

E file
    The Edit command is like e, except that the editor does not check to see if any changes have been made to the buffer since the last w command.

f file
    If file is given, the file command changes the currently-remembered filename to file; otherwise, it prints the currently-remembered filename.

(1,$)g/ RE/command-list
In the global command, the first step is to mark every line that matches the given RE. Then, for every such line, the given command-list is executed with . 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 multiline list, except the last line, must end with a \; a, i, and c commands and associated input are permitted. The . terminating input mode may be omitted if it would be the last line of the command-list. An empty command-list is equivalent to the p command. The g, G, v, and V commands are not permitted in the command-list. See also BUGS.

(1,$)G/ RE/
In the interactive Global command, the first step is to mark every line that matches the given RE. Then, every such line is printed, . is changed to that line, and any one command (other than one of the a, c, i, g, G, v, and V commands) can be typed in for execution. After the execution of that command, the next marked line is printed, and so on; a NEWLINE acts as a null command; an & causes the reexecution of the most recent command executed within the current invocation of G. Note that the commands input as part of the execution of the G command may address and affect any lines in the buffer. The G command can be terminated by an interrupt signal (ASCII DEL or BREAK).

h   The help command gives a short error message that explains the reason for the most recent ? diagnostic.

H   The Help command alternates between automatic diagnostic message, or the normal mode of diagnositcs on request. It will also explain the previous ? if there was one.

(.)i
text
.   The insert command inserts the given text before the addressed line; . is left at the last inserted line, or, if there were none, at the addressed line. This command differs from the a command 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 NEWLINE character).

(.,.+1)j
    The join command joins contiguous lines by removing the appropriate NEWLINE characters. If exactly one address is given, this command does nothing.

(.)k c
The mark command marks the addressed line with name c, which must be a lower-case letter. The address ′c then addresses this line; . is unchanged.

(.,.)l
    The list command prints the addressed lines in an unambiguous way: a few nonprinting characters, such as TAB and BACKSPACE are represented by (hopefully) mnemonic overstrikes. All other nonprinting characters are printed in octal, and long lines are folded. An l command may be appended to any command other than e, f, r, or w.

(.,.)m a
The move command repositions the addressed line(s) after the line addressed by a. Address 0 is legal for a and causes the addressed line(s) to be moved to the beginning of the file. It is an error if address a falls within the range of moved lines; . is left at the last line moved.

(.,.)n
    The number command prints the addressed lines, preceding each line by its line number and a TAB character; . is left at the last line printed. The n command may be appended to any command other than e, f, r, or w.

(.,.)p
    The print command prints the addressed lines; . is left at the last line printed. The p command may be appended to any command other than e, f, r, or w. For example, dp deletes the current line and prints the new current line.

P   The editor prompts with a * for all subsequent commands. The P command alternately turns this mode on and off; it is initially off.

q   The quit command. ed exits. No automatic write of a file is done, but if changes have been made since the last time the buffer was written, ed warns you (unless the - option is in effect) by printing a ? diagnostic. A second q exits, destroying the buffer's contents.

Q   The editor exits without checking for changes since the last w command.

($)r file
The read command reads in the given file after the addressed line. If no file is named, the currently-remembered filename, if any, is used (see e and f commands). The currently-remembered filename is not changed unless file is the very first filename mentioned since ed was invoked. Address 0 is legal for r and causes the file to be read at the beginning of the buffer. If the read is successful, the number of characters read is typed; . is set to the last line read in. If file is replaced by !, the rest of the line is taken to be a shell (sh(1)) command whose output is to be read. For example, $r !ls appends current directory to the end of the file being edited. A shell command is not remembered as the current filename.

(.,.)s/ RE/replacement/ [g| n]
The substitute command searches each addressed line for an occurrence of the specified RE. In each line for which a match is found, all (non-overlapped) matched strings are replaced by the replacement if the global replacement indicator g appears after the command. If the global indicator does not appear, only the first occurrence of the matched string is replaced. If a number n appears after the command, only the n'th occurrence of the matched string on each addressed line is replaced. It is an error for the substitution to fail on all addressed lines. Any character other than SPACE or NEWLINE can be used instead of / to delimit the RE and the replacement string. . is left at the last line on which a substitution occurred.

    An ampersand (&) appearing in the replacement is replaced by the string matching the RE on the current line. The special meaning of & in this context may be suppressed by preceding it by \. As a more general feature, the characters \n, where n is a digit, are replaced by the text matched by the n'th regular subexpression of the specified RE enclosed between \( and \). When nested parenthesized subexpressions are present, n is determined by counting occurrences of \( starting from the left. When the character % is the only character in the replacement, the replacement used in the most recent substitute command is used as the replacement in the current substitute command. The % loses its special meaning when it is in a replacement string of more than one character or is preceded by a backslash.

    A line may be split by substituting a NEWLINE character into it. The NEWLINE in the replacement must be escaped by preceding it by \. Such substitution cannot be done as part of a g or v command-list.

(.,.)t a
This command acts just like the m command, except that a copy of the addressed lines is placed after address a (which may be `0'); . is left at the last line of the copy.

u   The undo command nullifies the effect of the most recent command that modified anything in the buffer, namely the most recent a, c, d, g, i, j, m, r, s, t, v, G, or V command.

(1,$)v /RE/command-list
This command is the same as the global command g except that the command-list is executed with . initially set to every line that does not match the RE.

(1,$)V /RE/
This command is the same as the interactive global command G except that the lines that are marked during the first step are those that do not match the RE.

(1,$)w file
The write command writes the addressed lines into the named file. If the file does not exist, it is created with mode 666 (readable and writable by everyone), unless your umask setting (see sh(1)) dictates otherwise. The currently-remembered filename is not changed unless file is the very first filename mentioned since ed was invoked. If no filename is given, the currently-remembered filename, if any, is used (see e and f commands); . is unchanged. If the command is successful, the number of characters written is typed. If file is replaced by !, the rest of the line is taken to be a shell (sh(1)) command for which the standard input is the addressed lines. Such a shell command is not remembered as the current filename.

X   A key string is demanded from the standard input. Subsequent e, r, and w commands encrypt and decrypt the text with this key by the algorithm of crypt(1). An explicitly empty key turns off encryption.

($)=
    The line number of the addressed line is typed; . is unchanged by this command.

!shell-command
    The remainder of the line after the ! is sent to the UNIX system shell (sh(1)) to be interpreted as a command. Within the text of that command, the unescaped character % is replaced with the remembered filename; if a ! appears as the first character of the shell command, it is replaced with the text of the previous shell command. Thus, !! will repeat the last shell command. If any expansion is performed, the expanded line is echoed; . is unchanged.

(.+1) NEWLINE
An address, alone on a line, prints the addressed line. A NEWLINE alone is equivalent to .+1p, which is useful for stepping forward through the buffer.

If an interrupt signal (ASCII DEL or BREAK) is sent, ed prints a ? and returns to its command level.

  File Format Specification Support

Both ed and red support the fspec(4) formatting capability. After including a format specification as the first line of filename and invoking ed with your terminal in stty -tabs or stty tab3 mode (see stty(1V), the tab stops specified are used when displaying lines. For example, if the first line of a file contained:

        <:t5,10,15 s72:>

tab stops would be set at columns 5, 10, and 15, and a maximum line length of 72 would be imposed. While inserting text, however, tab characters are expanded to every eighth column.

LIMITATIONS
The following limitations apply:

    512 characters per line.
    256 characters per global command-list.
    64 characters per filename.
    128K characters in the buffer.
    The limit on the number of lines depends on the amount of user memory:
    each line takes 1 word.

When reading a file, ed discards ASCII NUL characters and all characters after the last NEWLINE. Files (like a.out) that contain characters not in the ASCII set (bit 8 on) cannot be edited by ed.

If the closing delimiter of an RE or of a replacement string (such as /) would be the last character before a NEWLINE, that delimiter may be omitted, in which case the addressed line is printed. The following pairs of commands are equivalent:
    s/s1/s2
        s/s1/s2/p
    g/s1
        g/s1/p
    ?s1 ?s1?

FILES
/tmp/e#
    temporary; # is the process number.
ed.hup
    work is saved here if the terminal is hung up.

DIAGNOSTICS

?   For command errors.

?file
    For an inaccessible file (use the help and Help commands for detailed explanations).

If changes have been made in the buffer since the last w command that wrote the entire buffer, ed warns the user if an attempt is made to destroy ed's buffer via the e or q commands. It prints ? and allows one to continue editing. A second e or q command at this point will take effect. The - command-line option inhibits this feature.

SEE ALSO
ed(1), crypt(1), grep(1V), sed(1V), sh(1), stty(1V), fspec(4), regexp(5)

CAVEATS AND BUGS
A ! command cannot be subject to a g or a v command.

The ! command and the ! escape from the e, r, and w commands cannot be used if the the editor is invoked from a restricted shell (see sh(1)).

The sequence \n in an RE does not match a NEWLINE character.

The l command mishandles DEL.

Files encrypted directly with the crypt(1) command with the null key cannot be edited.

Characters are masked to 7 bits on input.

If the editor input is coming from a command file, the editor exits at the first failure of a command in that file.