ED(1)

NAME
ed, red - basic line editor

SYNOPSIS
ed [ - ] [ -sx ] [ -p string ] [ filename ]

red [ - ] [ -sx ] [ -p string ] [ filename ]

DESCRIPTION

ed is the most basic line editor of the UNIX system. Although superseded by ex(1) and vi(1) for most purposes, ed is still used by various system utilities.

ed operates on a copy of filename, called a buffer, and overwrites a file only when you issue the w (write) command. ed provides line oriented editing commands to display or change lines, to insert and delete lines from the buffer, to move or copy lines within the buffer, or to substitute character strings within lines.

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

OPTIONS

-
-s  Suppress printing of character counts (by e, r, and w commands), diagnostics (by e and q commands), and the ! prompt (after a ! command). Also, suppress printing the ? diagnostic before overwriting unsaved changes in the buffer.

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

-p string
    Use string as the editing prompt in command mode.

USAGE

  Command Structure

ed commands have a simple and regular structure. They consist of an optional address, or two optional addresses separated by a comma or semicolon, then a single-character command, which may be followed by a parameter for that command:

    [address[ ,address ]]command [parameter]

If only one address is specified, operations are performed on that line. If two addresses are specified, ed performs the operation on the inclusive range of lines. Commands that requires an address use certain addresses by default, typically the address of the current line.

For example, 1,10p means “print (display) lines 1 through 10” (two addresses), 5a means “append text after line 5” (one address), and d means “delete the current line” (no address with the current line used as default). The meaning of parameter varies for each operation — for the move (m) and transfer (t) operations, for instance, it is the line that the addressed lines are to be moved to or transferred after. For reading (r) and writing (w) a file, parameter specifies the name of the file that is to be read or written.

ed is extremely terse in its interaction with the user. Its normal response to most problems is simply a question mark (?). This may happen when ed cannot find a specified line in the buffer, or if a search for a regular expression fails in a substitute (s) command. The h command prints a somewhat more complete diagnostic for the most recent error encountered; the H command requests that the diagnostic be printed for all errors.

  Addresses

Lines can be addressed in several ways:

nnn By line number. Lines in the buffer are numbered relative to the start of the buffer. When displayed, line numbers are not physically present with the text of the file or buffer.

$   The last line of the buffer.

.   The current line. ed keeps track of the line on which you last performed an operation. This line is called the current line. You can address this line by typing a “dot” character.

±n  By relative line number. Address the line number that is n lines higher, or n lines lower than the current line.

′c  Address the line marked with the mark character c, which must be a lower-case letter. Lines are marked with the k command, described below.

/RE/
    An RE is a Regular Expression, described under Regular Expressions below. When enclosed by slashes, RE addresses the first line found by searching for a matching string. The search proceeds forward from the line following the current line, and wraps through the beginning of the buffer to include all preceding lines, as well as the current line.

?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. The search wraps through the end of the buffer to include all lines following the current line (in reverse order), as well as the current line.

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

address±
    If an address ends with `+' or `-', then 1 is added to or subtracted from the address, respectively. As a consequence of this rule and the previous rule, the address `-' refers to the line preceding the current line. (To maintain compatibility with earlier versions of ed, the character `^' is equivalent to `-'.) Trailing `+' and `-' characters have a cumulative effect, so `--' refers to the current line, less 2.

,   By itself, a comma stands for the address pair `1,$'.

;   A semicolon by itself stands for the pair `.,$'.

    By default for a given command. If you do not specify an address for a command to operate on, a command that requires an address supplies one by default. This is typically the current line.

A pair of addresses separated by a comma signifies an inclusive range of lines, and the current line is not changed unless the command changes it. When addresses are separated by a semicolon, however, the current line is set to the address preceding the semicolon before any subsequent addresses are interpreted. This feature can be used to determine the starting line for forward and backward searches using `/', and `?'.

The second address of any two-address sequence must correspond to a line that occurs later in the buffer than that of the first address.

  Regular Expressions

ed supports a limited form of regular-expression notation, which can be used in a line address to specify lines by content. A regular expression (RE) specifies a set of character strings to match against — such as “any string containing digits 5 through 9” or “only lines containing uppercase letters.” A member of this set of strings is said to be matched by the regular expression. Regular expressions or patterns are used to address lines in the buffer (see Addresses , above), and also for selecting strings to be replaced using the s (substitute) command.

Where multiple matches are present in a line, a regular expression matches the longest of the leftmost matching strings.

Regular expressions can be 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 “quoted” character. A backslash followed by one of <, >, (, ), {, or }, represents an operator in a regular expression, as described below.

.   Dot. Matches any single character except NEWLINE.

^   As the leftmost character, a caret (or circumflex) constrains the RE to match the leftmost portion of a line. A match of this type is called an “anchored match” because it is “anchored” to a specific place in the line. The ^ character loses its special meaning if it appears in any position other than the start of the RE.

$   As the rightmost character, a dollar sign constrains the RE to match the rightmost portion of a line. The $ character loses its special meaning if it appears in any position other than at the end of the RE.

^RE$
    The construction ^RE$ constrains the RE to match the entire line.

\<  The sequence \< in an RE constrains the one-character RE immediately following it only to match something at the beginning of a “word”; that is, either at the beginning of a line, or just before a letter, digit, or underline and after a character not one of these.

\>  The sequence \> in an RE constrains the one-character RE immediately following it only to match something at the end of a “word.”

[c...]
    A nonempty string of characters, enclosed in square brackets matches any single character in the string. For example, [abcxyz] matches any single character from the set `abcxyz'. When the first character of the string is a caret (^), then the RE matches any character except NEWLINE and those in the remainder of the string. For example, `[^45678]' matches any character except `45678'. A caret in any other position is interpreted as an ordinary character.

[]c...]
    The right square bracket does not terminate the enclosed string if it is the first character (after an initial `^', if any), in the bracketed string. In this position it is treated as an ordinary character.

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

d   Delimiter character. The character used to delimit an RE within a command is special for that command (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:

    A concatenation of RE's matches a concatenation of text strings, each of which is a match for a successive RE in the search pattern.

*   A single-character RE, followed by an asterisk (*) matches zero or more occurrences of the single-character RE. Such a pattern is called a closure. For example, [a-z][a-z]* matches any string of one or more lower case letters.

\{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, inclusively. 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, but saves the string matched by the enclosed RE in a numbered substring register. There can be up to nine such substrings in an RE, and parenthesis operators can be nested.

\n  Match the contents of the nth substring register from the current RE. This provides a mechanism for extracting matched substrings. For example, the expression ^\(..*\)\1$ matches a line consisting entirely of two adjacent non-null appearances of the same string. When nested parenthesized substrings are present, n is determined by counting occurrences of \( starting from the left.

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

  Commands

The commands a for append, c for change, and i for insert, allow you to add new text to the buffer. While accepting new text, ed is said to be in input mode. While in input mode, no commands are recognized; all character input is inserted into the buffer. To exit from input mode, enter a dot (.) on a line by itself; ed then reverts to command mode. Or, you can interrupt ed (typically with CTRL-C), in which case it displays a ? and returns to command mode.

Commands may accept zero, one, or two addresses. Commands that accept no addresses regard the presence of an address as an error. Commands that accept one or two addresses assume default addresses when too few addresses are given; if more addresses are given than such a command requires, only the last ones are used.

In the following list of ed commands, the default addresses are shown in parentheses; the parenthesized addresses are not part of the command.

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 followed by l, n, or p in which case the current line is either listed, numbered or printed, respectively.

(.)a
text
.   Append text. Add lines of text into the buffer after the addressed line. The resulting current line is the last line of input, or the addressed line if no text is entered. Address 0 is legal for this command, in which case the text is placed at the beginning of the buffer. The maximum number of characters per input line (from a terminal) is 256, including the final NEWLINE.

(.)c
text
.   Change lines. Delete the addressed lines, and then accept lines of text to replace them. c accepts one or two addresses; the default is the current line. The resulting current line is the last line of input, or the line preceding the deleted lines if no text is entered.

(.,.)d
    Delete the addressed lines from the buffer. d accepts one or two addresses; the default is the current line. The resulting current line is the line following the last one deleted; if the deleted lines were at the end of the buffer, the new last line is the resulting current line.

e filename
    Edit a file. Delete the entire contents of the buffer, and then read in the named file. The resulting current line is the last line of the buffer. e reports the number of characters read into the buffer, and sets filename to be the current file (for use as a default filename in subsequent commands). If no filename is given, the current filename, if any, is used (see the f command, below). If filename is replaced by a shell (sh(1)) command prefaced with a `!', the shell command is executed and its output is read into the buffer after the current line. Such a shell command is not used as the current filename. e displays a ? if the buffer has not been written out since the last change made — another e command in response to the ? forces the command to take effect.

E filename
    The E command is like e, except that the editor does not check for changes to the buffer since the last w command was performed.

f filename
    Display or set the current filename. If filename is given as an argument, the file (f) command changes the current filename to filename; otherwise, it prints the current filename.

(1,$) g/RE/command-list
The global (g) command performs command-list on all lines in the range of addresses that match RE. ed executes command-list for each matching line in succession, setting the current line to each in turn. command-list can contain a single command, or it can be continued across input lines, with one ed command per line, by escaping all but the last NEWLINE with a \ character. Operations that place ed into input mode (a, i, and c), are permitted in command-list; the final `.' terminating text input may be omitted if it is the last line of the command-list. g, G, v, and V commands, however, are not permitted. An empty command-list is equivalent to the p command.

(1,$) G/RE/
The interactive G (Global) command, selects all lines that match the given RE. Then, each selected line is made current, and any one command (other than one of the a, c, i, g, G, v, and V commands) can be performed upon that line. A NEWLINE acts as a null command; an & reexecutes the most recent command. Commands entered during execution of the G command can address and affect lines other than the current line. The G command can be terminated by an interrupt (typically CTRL-D).

h   Help. Display a short error message that explains the reason for the most recent ? diagnostic.

H   Automatic printing of help diagnostics. Toggle between printing the ? diagnostic, or automatically printing diagnostic messages as well.

(.)i
text
.   Insert Text. Insert the given text into the buffer, above the addressed line. i accepts one address; the default is the current line. The resulting current line is the last line of input; if no text is input, it is the line just before the addressed line. This command differs from the a command only in the placement of the input text; Address 0 is not allowed 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
    Join Lines. Remove the NEWLINE character from between the two addressed lines. The defaults are the current line and the line following. If exactly one address is given, this command does nothing. The joined line is the resulting current line.

(.)kc
    Mark the addressed line with the name c, a lower-case letter. The address-form, ′c, addresses the line marked by c. k accepts one address; the default is the current line. The current line is left unchanged.

(.,.)l
    List nonprinting characters. Print the addressed lines in an unambiguous way: a few nonprinting characters, such as TAB and BACKSPACE are represented by visually mnemonic overstrikes. All other nonprinting characters are shown in octal, with long lines folded. l accepts one or two addresses; the default is the current line. The resulting current line is the last line printed. An l command may be appended to any command other than e, f, r, or w.

(.,.) maddress
Move addressed lines to just after address. Address 0 is legal, and moves the addressed line(s) to the beginning of the file. An error results if address falls within the range of lines to move. m accepts two addresses to specify a range of lines to move; the default is the current line. The resulting current line is the last of the moved lines.

(.,.)n
    Number the displayed lines. Print the addressed lines, preceding each with its line number and a TAB character. n accepts one or two addresses; the default is the current line. The resulting current line is the last line printed. The n command can be appended to any command other than e, f, r, or w.

(.,.)p
    Print the addressed lines. p accepts one or two addresses; the default is the current line. The resulting current line is 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   Toggle prompting on or off. When prompting is in effect, the editor prompts with a * for commands. A subsequent P command turns prompting off.

q   Quit. Exit from ed. Note, however, that the buffer is not automatically written out; you must write any changes to be saved with the w command; ed warns you once if you have not saved your changes (unless the `-' option is in effect). A second q forces ed to exit regardless, destroying the buffer's contents.

Q   Force quit. This is the same as q, but you do not get any warning if you have not previously written out the buffer. ed simply exits.

($) r filename
Read in the contents of filename, after the addressed line. If filename is not given, the current filename, if any, is used (see the e and f commands). The current filename is not altered; if there is no current filename, filename becomes the current filename. r accepts one address; the default is $. Address 0 is legal for r, in which case the file is read in at the beginning of the buffer. If the read is successful, the number of characters read is typed. The resulting current line is the last line read in from the file. If filename is replaced by a shell (sh(1)) command prefaced with a !, the shell command is executed and its output is read in. Such a shell command is not remembered as the current filename.

(.,.) s/RE/rs/
(.,.) s/RE/rs/g
(.,.) s/RE/rs/n
Substitute. Search each addressed line for the first occurrence of a string matching the specified RE, and replace it with rs, the replacement string. If g (global suffix) is appended to the command, replace all (non-overlapped) matching strings in each addressed line with the replacement string rs. Note: the g suffix is distinct from the g command. If a number n is appended, replace only the n'th occurrence of the matched string on each addressed line. s accepts one or two addresses; the default is the current line. The resulting current line is the last line on which a substitution is made. An error results if RE matches no strings in the addressed line or range. Any character (other than SPACE or NEWLINE can be used instead of / to delimit RE and rs. As with RE's in addresses, you can refer to the entire string matched by RE with an `&'; you can refer to parenthesized substrings within RE using \1...\n. When % is the only character in rs, the rs from in the most recent substitute command is used as the current rs. The % loses its special meaning when it is in a replacement string of more than one character, or if it 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 with an `\'. Such substitutions cannot be done as part of a g or v command list.

(.,.) taddress
Transfer. Transpose a copy of the addressed range of lines to just after the given address. t (transfer) is like m (move), except that it copies of the lines, rather than moving them. t accepts two addresses preceding the operation letter, the current address is the default. The resulting current line is the last line copied. Address 0 is allowed.

u   Undo. Reverse the effect of the most recent command that modified the buffer. A second u undoes the undo operation.

(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
Similar to the G command, except that the lines selected are those that do not match the RE.

(1,$) w filename
Write the addressed lines to filename. If the file does not exist, ed creates it. The current filename is not altered; if there is no current filename, then filename becomes current. If no filename is given, the current filename, if any, is used. w accepts one or two addresses; the default is all lines in the file. The current line is unchanged. If the command is successful, the number of characters written is displayed. If filename is replaced by a shell (sh(1)) command prefaced with a `!', the shell command is executed with standard input taken from the addressed lines. Such a shell command is not remembered as the current filename.

(1,$) W filename
Like w, but append the addressed lines onto the named file.

x   Encrypt the file. ed prompts for an encryption key from the standard input. Subsequent e, r, and w commands encrypt and decrypt the text with this key (see crypt(1)). An empty key turns off encryption. Encryption can also be specified on the command line with the -x option.

($)=
    Display the line number of the addressed line; the current line remains unchanged.

!shell-command
    Run a shell command. shell-command is a (Bourne shell) command line. ed replaces the unescaped character % with the current filename; if a ! appears as the first character of the shell command, it is replaced with the text of the previous shell command. (!! repeats the last shell command.) If any such expansion is performed, the expanded line is echoed. The current line is unchanged.

address
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

ed supports the fspec(5) formatting capability for displaying lines. When the first line of a file is a format specification of the form:

    <:ts [, ts]... smax:>

where ts is the column number of a TAB stop and max is the maximum line length for display purposes, and with the terminal in `stty -tabs' or `stty tab3' mode (see stty(1V) for details), the indicated TAB stops are used in displayed lines. While inserting text, however, TAB stops are set to every eighth column.

ENVIRONMENT

The environment variables LC_CTYPE, LANG, and LC_default control the character classification throughout ed. On entry to ed, these environment variables are checked in the following order: LC_CTYPE, LANG, and LC_default. When a valid value is found, remaining environment variables for character classification are ignored. For example, a new setting for LANG does not override the current valid character classification rules of LC_CTYPE. When none of the values is valid, the shell character classification defaults to the POSIX.1 “C” locale.

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

SEE ALSO
crypt(1), ex(1), grep(1V), sed(1V), sh(1), stty(1V), vi(1), regexp(3), fspec(5), locale(5), iso_8859_1(7)

LIMITATIONS

The following limitations apply:

    512 characters per line.
    256 characters per global command-list.
    1024 characters per filename.
    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 (such as executable images) that contain characters not in the ASCII set (bit 8 on) cannot be edited using ed.

If a file is not terminated by a NEWLINE character, ed adds one and prints a message saying that it has done so.

If the closing delimiter of an RE or of a replacement string (such as /) would be the last character before a NEWLINE, that delimiter can 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?

DIAGNOSTICS

?   For command errors.

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

If changes have been made in the buffer since the last w command, ed issues a warning ? when a command is given that would destroy the buffers contents. A second e or q command at this point will take effect. The `-' and -s command-line options inhibit this feature.

WARNINGS

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

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

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

The encryption facilities of ed are not available on software shipped outside the U.S.

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