man(1) Manual page archive

ed(1) - Unix First Edition Manual Page
11/3/71ED (I)

NAME ed -- editor
SYNOPSIS ed [ name ]
DESCRIPTION ed is the standard text editor. ed is based on QED[reference] but is fully if succinctly described here.

Differences between ed and QED are also noted to simplifythe transition to the less powerful editor.

If the optional argument is given, simulates an e commandon the named file; that is to say, the file is read into ed's buffer so that it can be edited. ed operates on a copy of any file it is editing; changesmade in the copy have no effect on the file until an explicit write (w) command is given. The copy of the textbeing edited resides in a temporary file called the buffer. There is only one buffer. Commands to ed have a simple and regular structure: zero ormore addresses followed by a single character command, possibly followed by parameters to the command. Theseaddresses specify one or more lines in the buffer. Every command which requires addresses has default addresses, sothat the addresses can often be omitted.

In general only one command may appear on a line. Certaincommands allow the input of text. This text is placed in the appropriate place in the buffer. While ed is acceptingtext, it is said to be in input mode. In this mode, no commands are recognized; all input is merely collected.Input mode is left by typing a period (.) alone at the beginning of a line. ed supports a limited form of regular expression notation.A regular expression is an expression which specifies a set of strings of characters. A member of this set of stringsis said to be matched by the regular expression. The regular expressions allowed by are constructed as follows:

1 . An ordinary character (not one of thosediscussed below) is a regular expression

and matches that character. 2. A circumflex (^) at the beginning of a regularexpression matches the null character

at the beginning of a line.

11/3/71ED (I)

3. A currency symbol ($) at the end of a regularexpression matches the null character

at the end of a line. 4. A period (.) matches any character but a new--linecharacter.

5. A regular expression followed by an asterisk (*)matches any number of adjacent occurrences

(including zero) of the regular expression itfollows.

6. A string of characters enclosed in square brackets([]) matches any character in the string but no

others. If, however, the first character of thestring is a circumflex (^) the regular expression matches any character but new--line and thecharacters in the string.

7. The concatenation of regular expressions is aregular expression which matches the concatenation

of the strings matched by the components of theregular expression.

8. The null regular expression standing alone isequivalent to the last regular expression

encountered. Regular expressions are used in addresses to specify linesand in one command (s, see below) to specify a portion of a line which is to be replaced.

If it is desired to use one of the regular expressionmetacharacters as an ordinary character, that character may be preceded by "\". This also applies to the characterbounding the regular expression (often "/") and to \ itself. Addresses are constructed as follows. To understandaddressing in ed it is necessary to know that at any time there is a current line. Generally speaking, the currentline is the last line affected by a command; however, the exact effect on the current line by each command isdiscussed under the description of the command.

1. The character "." addresses the current line.2. The character "$" addresses the last line of the

buffer.3. A decimal number n addresses the nth line of the buffer.

11/3/71ED (I)

4. A regular expression enclosed in slashes "/"addresses the first line found by searching toward

the end of the buffer and stopping at the firstline containing a string matching the regular expression. If necessary the search wraps around tothe beginning of the buffer.

5. A regular expression enclosed in queries "?"addresses the first line found by searching toward

the beginning of the buffer and stopping at thefirst line found containing a string matching the regular expression. If necessary the search wrapsaround to the end of the buffer.

6. An address followed by a plus sign "+" or a minussign "--" followed by a decimal number specifies

that address plus (resp. minus) the indicatednumber of lines. The plus sign may be omitted.

Commands may require zero, one, or two addresses. Commandswhich require no addresses regard the presence of an address as an error. Commands which require the presence ofone address all assume a default address (often ".") but if given more than one address ignore any extras and use thelast given. Commands which require two addresses have defaults in the case of zero or one address but use thelast two if more than two are given.

Addresses are separated from each other typically by acomma (,). They may also be separated by a semicolon ";". In this case the current line is set to the the previousaddress before the next address is interpreted. This feature is used to control the starting line for forwardand backward searches ("/", "?").

In the following list of ed commands, the default addressesare shown in parentheses. The parentheses are not part of the address, but are used to show that the given addressesare the default.

As mentioned, it is generally illegal for more than onecommand to appear on a line. However, any command may be suffixed by "p" (for "print ). In that case, the currentline is printed after the command is complete.

In any two--address command, it is illegal for the

11/3/71ED (I)

first address to lie after the second address.(.)a


The append command reads the given text and appendsit after the addressed line "." is left on the last line input, if there were any,otherwise at the addressed line. Address "0" is legal for this command; text is placed at thebeginning of the buffer. (NOTE: the default address differs from that of QED.)


. The change command deletes the addressed lines, thenaccepts input text which replaces these lines. "." is left at the last line input; if there were none,it is left at the first line not changed.

(.,.)dThe delete command deletes the addressed lines from the buffer. "." is left at the first line notdeleted.

e filenameThe edit command causes the entire contents of the buffer to be deleted. and then the named file to beread in. "." is set to the last line of the buffer. The number of characters read is typed. (1 ,s)g/regular expression/commandIn the global command, the first step is to mark

every line which matches the given regularexpression. Then for every such line, the given command is executed with "." set to that line. Therepeated command cannot be a, g, i, or c.


. This command inserts given text before the addressedline. "." is left at the last line input; if there were none, at the addressed line. This commanddiffers from the a command only in the placement of the text.

(.,.)lThe list command prints the addressed lines in an unambiguous way. Non--printing

11/3/71ED (I)

characters are over--struck as follows:char prints

bs [overstruct - \]tab [overstruct - >] ret [overstruct - <]SI [overstruct I -] SO [overstruct O -]All characters preceded by a prefix (ESC) character are printed over--struck with without the prefix.Long lines are folded with the sequence \newline.

(.,.p)The print command prints the addressed lines. . is

left at the last line printed

qThe quit command causes ed to exit. No automatic write of a file is done. ($)r filenameThe read command reads in the given file after the

addressed line. If no file name is given, the filelast mentioned in e, r, or w commands is read. Address "0" is legal for r and causes the file tobe read at the beginning of the buffer. If the read is successful, the number of characters read istyped."." is left at the last line of the file.

(.,.)s/regular expression/replacement/The substitute command searches each addressed line for

an occurence of the specified regular expression.On each line in which a match is found, the first (and only first, compare QED) matched string isreplaced by the replacement specified. It is an error for the substitution to fail on all addressedlines. Any character other than space or new--line may be used instead of "/" to delimit the regularexpression and the replacement. "." is left at the last line substituted. The ampersand "&" appearing in the replacement isreplaced by the regular expression that was matched. The special meaning of "&" in this contextmay be suppressed by preceding it by "\".

(1,$)w filenameThe write command writes the addressed lines onto

the given file. If no file name is given, the filelast named in e, r, or w

11/3/71ED (I)

commands is written. "." is unchanged. If the command is successful, the number of characters written is typed. The line number of the addressed line is typed. . is unchanged by this command.

($)=The line number of the addressed line is typed. "."

is unchanged by this command. !UNIX commandThe remainder of the line after the "!" is sent to

UNIX to be interpreted as a command. "."isunchanged.

(newline>A blank line alone is equivalent to ".+lp"; it is useful for stepping through text. Ed can edit at most 1500 lines and the maximum size of aline is 256 characters, The differences between ed and QED are:

1. There is no \f character; input mode is left bytyping . alone on a line. 2. There is only one buffer and hence no streamdirective. 3. The commands are limited to:

a c d e g i l p q r s w = ! where e is new. 4. The only special characters in regular expressionsare:

* ^ $ [ . which have the usual meanings. However, "^" and "$"are only effective if they are the first or last character respectively of the regular expression.Otherwise suppression of special meaning is done by preceding the character by "\ , which is nototherwise special.

5. In the substitute command, only the leftmostoccurrence of the matched regular

expression is substituted. 7. The a command has a different default address.
FILES /tmp/etma, etmb, ... temporary/etc/msh is used to implement the "!" command.

11/3/71ED (I)

DIAGNOSTICS ? for any error
BUGS ed is used as the shell for the editing system. It has theediting system UID built in and if invoked under this UID

will give slightly different responses. This is a littlekludgy.