Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ ed(1) — Ultrix-11 2.0

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

grep(1)

sed(1)

sh(1)

stty(1)

ED(1)

NAME

ed − text editor

SYNOPSIS

ed [ − ] [ name ]

red [ − ] [ name ]

DESCRIPTION

Ed is the standard text editor. 

If a name argument is given, ed simulates an e command (see below) on the named file; that is to say, the file is read into ed’s buffer so that it can be edited.  The optional − suppresses the printing of character counts by e, r, and w commands, of diagnostics from e and q commands, and of the !  prompt after a !shell command.

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

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

Commands to ed have a simple and regular structure: zero or more addresses followed by a single character command, possibly followed by parameters to the command.  These addresses specify one or more lines in the buffer.  Missing addresses are supplied by default. 

In general, only one command may appear on a line.  Certain commands allow the addition of text to the buffer.  While ed is accepting text, 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 specifies a set of strings of characters.  A member of this set of strings is said to be matched by the regular expression.  In the following specification for regular expressions the word ’character’ means any character but newline. 

The regular expressions allowed by ed are constructed as follows:

The following one-character regular expressions match a single character:

1.1 An ordinary character ( not one of those discussed in 1.2 below) is a one-character regular expression that matches itself. 

1.2 A backslash (\) followed by any special character is a one-character regular expression that matches the special character itself.  The special characters are:

a.  ., ∗, [, and \ (period, asterisk, left square bracket, and backslash, respectively), which are always special, except when they appear within square brackets ([]; see 1.4 below).

b.  ^ (caret or circumflex), which is special at the beginning of an entire regular expression (see 3.1 and 3.2 below), or when it immediately follows the left of a pair of square brackets ([]) (see 1.4 below).

c.  $ (currency symbol), which is special at the end of an entire regular expression (see 3.2 below). 

d.  The character used to bound (i.e., delimit) an entire regular expression, which is special for that regular expression (for example, see how slash (/) is used in the g command, below.) 

1.3 A period (.) is a one-character regular expression that matches any character except new-line.

1.4 A non-empty string of characters enclosed in square brackets ([]) is a one-character regular expression that matches any one character in that string.  If, however, the first character of the string is a circumflex (^), the one-character regular expression matches any character except newline and the remaining characters in the string.  The ^ has this special meaning only if it occurs first in the string.  The minus (−) may be used to indicate a range of consecutive ASCII characters; for example, [0−9] is equivalent to [0123456789].  The − loses this special meaning if it occurs first (after an initial ^, if any) or last in the string.  The right square bracket (]) does not terminate such a string when it is the first character within it (after an initial , if any); e.g., []a−f] matches either a right square bracket (]) or one of the letters a through f inclusive.  The four characters listed in 1.2.a above stand for themselves within such a string of characters. 

The following rules may be used to construct regular expressions from one-character regular expressions:

2.1 A one-character regular expression is a regular expression that matches whatever the one-character regular expression matches. 

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

2.3 A one-character regular expression followed by \{m\}, \{m,\}, or \{m,n\} is a regular expression that matches a range of occurrences of the one-character regular expression.  The values of m and n must be non-negative 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 regular expression matches as many occurrences as possible. 

2.4 The concatenation of regular expressions is a regular expression that matches the concatenation of the strings matched by each component of the regular expression. 

2.5 A regular expression enclosed between the character sequences \( and \) is a regular expression that matches whatever the unadorned regular expression matches. 

2.6 The expression \n matches the same string of characters as was matched by an expression enclosed between \( and \) earlier in the same regular expression.  Here n is a digit; the sub-expression 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. 

Finally, an entire regular expression may be constrained to match only an initial segment or final segment of a line (or both):

3.1 A circumflex (^) at the beginning of an entire regular expression constrains that regular expression to match an initial segment of a line. 

3.2 A currency symbol ($) at the end of an entire regular expression constrains that regular expression to match a final segment of a line. 

Regular expressions are used in addresses to specify lines and in one command (see s below) to specify a portion of a line which is to be replaced. If it is desired to use one of the regular expression metacharacters as an ordinary character, that character may be preceded by ’\’. This also applies to the character bounding the regular expression (often ’/’) and to ’\’ itself.

The construction ­entire regular expression$ constrains the entire regular expression to match the entire line. 

The null regular expression (e.g., //) is equivalent to the last regular expression encountered.  See also the last paragraph before FILES below. 

To understand addressing in ed it is necessary to know that at any time there is a current line.  Generally speaking, the current line is the last line affected by a command; however, the exact effect on the current line is discussed under the description of the command.  Addresses are constructed as follows. 

1.  The character ’.’ addresses the current line. 

2.  The character ’$’ addresses the last line of the buffer. 

3.  A decimal number n addresses the n-th line of the buffer.

4.  ′x addresses the line marked with the name x, which must be a lowercase letter. Lines are marked with the k command described below. 

5.  A regular expression enclosed in slashes ’/’ addresses the line found by searching forward from the current line and stopping at the first line containing a string that matches the regular expression.  If necessary the search wraps around to the beginning of the buffer.  See also the last paragraph before FILES below. 

6.  A regular expression enclosed in queries ’?’ addresses the line found by searching backward from the current line and stopping at the first line containing a string that matches the regular expression.  If necessary the search wraps around to the end of the buffer.  See also the last paragraph before FILES below. 

7.  An address followed by a plus sign ’+’ or a minus sign ’−’ followed by a decimal number specifies that address plus (respectively minus) the indicated number of lines. The plus sign may be omitted.

8.  If an address begins with ’+’ or ’−’ the addition or subtraction is taken with respect to the current line; e.g. ’−5’ is understood to mean ’.−5’. 

9.  If an address ends with ’+’ or ’−’, then 1 is added to or subtracted from the address, respectively.  As a consequence of this rule and of rule 8 immediately above, the address − refers to the line preceding the current line.  (To maintain compatibility with earlier versions of the editor, the character in addresses is entirely equivalent to −.)  Moreover, trailing ´+’ and ´-’ characters have a cumulative effect, so ’−−’ refers to the current line less 2. 

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

Commands may require zero, one, or two addresses.  Commands which require no addresses regard the presence of an address as an error.  Commands which accept one or two addresses assume default addresses when insufficient ones are given.  If more addresses are given than such a command requires, the last one or two (depending on what is accepted) are used.

Addresses are separated from each other typically by a comma ’,’.  They may also be separated by a semicolon ’;’.  In this case the current line ’.’ is set to the previous address before the next address is interpreted.  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 following 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, but are used to 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, as discussed below under the l, n and p commands. 

(.)a

<text>

. 
The append command reads the given text and appends it 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 the beginning of the buffer.  See also the last paragraph before FILES below. 

(.,.)c

<text>

. 
The change command deletes the addressed lines, then accepts input text which replaces these lines.  ’.’ is left at the last line input; if there were none, it is left at the line preceding the deleted lines. 

(.,.)d
The delete command deletes the addressed lines from the buffer. The line originally after the last line deleted becomes the current line; if the lines deleted were originally at the end, the new last line becomes the current line.

e filename
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 file name, if any, is used (see the f command).  The number of characters read is typed; ´filename’ is remembered for possible use as a default file name in subsequent e, r, and w commands. If ’filename’ is replaced by !, the rest of the line is taken to be a shell (sh(1)) command whose output is to be read. Such a shell command is not remembered as the current file name.  See also DIAGNOSTICS below. 

E filename
This command is the same as e, except that no diagnostic results when no w has been given since the last buffer alteration. 

f filename
The filename command prints the currently remembered file name. If ’filename’ is given, the currently remembered file name is changed to ’filename’.

(1,$)g/regular expression/command list
In the global command, the first step is to mark every line which matches the given regular expression. Then for every such line, the given command list is executed with ’.’ initially set to that line.  A single command or the first of multiple commands appears on the same line with the global command.  All lines of a multi-line list except the last line must be ended with ’\’.  A, i, and c commands and associated input are permitted; the ’.’ terminating input mode may be omitted if it would be on the last line of the command list.  The g, G, v, and V commands are not permitted in the command list.  See also RESTRICTIONS and the last paragraph before FILES below. 

(1,$)G/ regular expression /
In the interactive Global command, the first step is to mark every line that matches the given regular expression. Then, for every such line, that 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) may be input and is executed.  After the execution of that command, the next marked line is printed, and so on; a new-line acts as a null command; an ’&’ causes the re-execution 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 causes ed to enter a mode in which error messages are printed for all subsequent ? diagnostics.  It will also explain the previous ? if there was one.  The H command alternately turns this mode on and off; it is initially off. 

(.)i

<text>

. 
This command inserts the given text before the addressed line. ´.’ is left at the last line input, or, if there were none, at the line before the addressed line.  This command differs from the a command only in the placement of the 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
This command joins the addressed lines into a single line; intermediate newlines simply disappear. ´.’ is left at the resulting line. 

( . )kx
The mark command marks the addressed line with name x, which must be a lowercase letter.  The address ′x then addresses this line; ´.’ is unchanged.

(.,.)l
The list command prints the addressed lines in an unambiguous way: non-graphic characters are printed in two-digit octal, and long lines are folded. The l command may be placed on the same line after any non-i/o command. 

(.,.)ma
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 number command may placed on the same line after any non-i/o command.

(.,.)p
The print command prints the addressed lines. ´.’ is left at the last line printed.  The p command may be placed on the same line after any non-i/o command. 

P The editor will prompt with an asterisk (*) for all subsequent commands.  The P command alternately turns this mode on and off; it is initially off. 

q The quit command causes ed to exit.  No automatic write of a file is done. 

Q This command is the same as q, except that no diagnostic results when no w has been given since the last buffer alteration. 

($)r filename
The read command reads in the given file after the addressed line. If no ’filename’ is given, the remembered file name, if any, is used (see e and f commands). The file name is remembered if there was no remembered file name already. 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 left at the last line read in from the file.  If ’filename’ 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. Such a shell command is not remembered as the current file name. 

( .l,. )s/regular expression/replacement/
or,

( .,. )s/regular expression/replacement/g
 
The substitute command searches each addressed line for an occurrence of the specified regular expression. On each line in which a match is found, all matched strings are replaced by the replacement specified, 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. It is an error for the substitution to fail on all addressed lines. Any character other than space or new-line may be used instead of ’/’ to delimit the regular expression and the replacement. ´.’ is left at the last line substituted.  See also the last paragraph before FILES below. 

An ampersand ’&’ appearing in the replacement is replaced by the string matching the regular expression.  The special meaning of ’&’ in this context may be suppressed by preceding it by ’\’.  The characters ’\n’ where n is a digit, are replaced by the text matched by the n-th regular subexpression enclosed between ’\(’ and ’\)’. When nested, parenthesized subexpressions are present, n is determined by counting occurrences of ’\(’ starting from the left. 

Lines may be split by substituting new-line characters into them.  The new-line in the replacement string must be escaped by preceding it by ’\’.  Such substitution cannot be done as part of a g, G, v, or V command list. 

(.,.)ta
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 on 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/ regular expression / 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 regular expression. 

(1,$)V/ regular expression /
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 regular expression. 

(1,$)w filename
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 filename is the very first file name 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 filename is replaced by !, the rest of the line is taken to be a shell (sh(1)) command whose standard input is the addressed lines. Such a shell command is not remembered as the current file name. 

(1,$)W filename
This command is the same as w, except that the addressed lines are appended to the file. 

($)=
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 file name; 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 causes the addressed line to be printed. A blank line alone is equivalent to ’.+1p’; it is useful for stepping through text.

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

Some size limitations: 512 characters per line, 256 characters per global command list, 64 characters per file name, and 128K characters in the temporary file.  The limit on the number of lines depends on the amount of core: each line takes 1 word. 

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

If the closing delimiter of a regular expression or of a replacement string (e.g., /) would be the last character before a new-line, 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*
ed.hup work is saved here if terminal hangs up
 

SEE ALSO

grep(1), sed(1), sh(1), stty(1)
B. W. Kernighan, A Tutorial Introduction to the ED Text Editor
B. W. Kernighan, Advanced editing on UNIX
 

DIAGNOSTICS

´?name’ for inaccessible file; ´?’ for errors in commands; ´?TMP’ for temporary file overflow. 
(use the help and Help commands for detailed explanations).

To protect against throwing away valuable work, a q or e command is considered to be in error, unless a w has occurred since the last buffer change.  A second q or e will be obeyed regardless. 
 

RESTRICTIONS

The l command mishandles DEL. 
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)).
Because 0 is an illegal address for a w command, it is not possible to create an empty file with ed. 
Characters are masked to 7 bits on input.

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026