Museum

Home

Lab Overview

Retrotechnology Articles

⇒ Online Manual

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

crypt(1)

ex(1)

sed(1)

vi(1)

ED(1)  —  USER COMMANDS

NAME

ed − text editor

SYNOPSIS

ed [ − ] [ −x ] [ filename ]

DESCRIPTION

Ed is the basic text editor in the UNIX system.  While ed is for all practical purposes superseded by vi, ed is still used by other system utilities such as SCCS.  Ed is a line editor — in general, you must specify the line or lines on which to perform the operation you choose (see Line Addressing, below, for a discussion of how to form line-addresses for ed). You can tell ed to perform various operations on the lines.  For instance, you can print (display) lines; you can change lines; you can insert new lines into the buffer; you can delete existing lines; you can move or copy lines to a different place in the buffer; you can substitute character strings within lines.  See List of Operations, below, for a guide.  Also, see Regular Expressions for string-matching metacharacters. 

Ed does not directly change the contents of a file — when editing a file, ed reads the contents of the file into a buffer or scratchpad.  All changes made during an editing session are made on the contents of this buffer.  The copy must be ‘saved’ or ‘written’ — using the w (write) operation — to save changes. 

The command-line shown in the synopsis above invokes ed. If filename is given, ed reads a copy of filename into its buffer so that it can be edited (simulates an e operation on filename). 

Ed commands have a simple and regular structure: commands consists of an optional line-address, or two optional line-addresses separated by a comma, then a single letter operation, optionally followed by some other parameter:

[line-address [ ,line-address ] ] operation [ parameter]

For example, ‘1,10p’ means ‘print (display) lines 1 through 10’ (two line-addresses), ‘5a’ means ‘append after line 5’ (one line-address), and d means ‘delete the current line’ (no line-address with the current line used as default).  Parameter varies for each operation — for the move and transfer operations, for example, it is the line that the addressed lines are to be moved or transferred after.  These operations actually have three line-addresses.  For reading and writing a file, parameter specifies the name of the file that is to be read. 

Ed is extremely terse in its interaction with the user — ed’s normal response to just about any problem is simply a question mark ?.  You get this response when, for instance, ed can’t find a specified line in the buffer, or if a search for a regular expression fails in a substitute (s) operation. 

OPTIONS

−Suppress the display of character counts normally given by the e, r, and w operations — can be used when the standard input is an editor script. 

−xSimulate an x operation on the named file before reading it into the buffer, to handle encryption. 

LINE ADDRESSING

The format of ed operations above shows that an operation can be preceded by one or two line-addresses, both of which are optional. If only one line-address is specified, operations are performed on that specific line.  If two line-addresses are supplied, ed operates on the inclusive range of lines between them. 

Line-addresses are usually separated from each other by a comma — for instance:

1,10p

prints (displays) lines 1 thru 10. 

Line addresses may also be separated by a semicolon.  Whereas the starting position for line-addresses separated by a comma is the same place in the buffer, when a line-address is followed by a semicolon, the current line is set to the line-address preceding the semicolon before any subsequent line-addresses are interpreted.  For example:

/Domaine Chandon/;//p

sets the current line to the first occurrence of the string ‘Domaine Chandon’ before starting the search for the second occurrence.  This feature can be used to determine the starting line for forward and backward searches (‘/’, ‘?’). 

Lines can be accessed (addressed, in ed terminology) in several ways, but the most easily understood way of addressing lines is by line number.  Line numbers in ed are relative to the start of the buffer.  In practice, addressing lines by number proves to be the most awkward to use, so ed provides other mechanisms for line-addressing.  Note that the line numbers associated with lines in the buffer are not physically present with the text of the lines — they are just an addressing mechanism. 

While ed is working on the buffer, it keeps track of the line on which you last performed some operation.  This line is called the ‘current line’.  As described below, you can indicate the current line by typing a period character (.).

If you don’t specify a line for an operation to operate on, most ed operations work on the line addressed by the current line. 

When ed starts working on a file, the current line is positioned at the last line in the buffer.  Thereafter, the current line usually changes when any operation is performed.  In general, the current line sits at the last line affected by whatever ed operation you used.  For instance, if you print lines 1 through 10 of the buffer, after the lines are displayed, the current line will be positioned at line 10. 

Line-addresses are constructed from elements as shown in the list below.  Some special characters are used as a shorthand for certain line-addresses:

.(‘dot’) addresses the current line. 

$addresses the last line of the buffer. 

nnnA decimal number nnn addresses the nnn-th line of the buffer.

′xaddresses the line marked with the name x, which must be a lower-case letter.  Mark lines with the k operation described below. 

/regular expression/
A regular expression enclosed in slashes ‘/’ searches forward from the current line and stops at the first line containing a string that matches the regular expression.  If necessary, the search wraps around to the beginning of the buffer.

?regular expression?
A regular expression enclosed in question marks ‘?’ searches backward from the current line and stops at the first line containing a string that matches the regular expression.  If necessary the search wraps around to the end of the buffer.

address±nnn
An address followed by a plus sign ‘+’ or a minus sign ‘−’ followed by a decimal number specifies that line-address plus or minus the indicated number of lines.  Plus is assumed if no signs are given. 

±address
An address beginning with ‘+’ or ‘−’ is taken relative to the current line; in other words, ‘−5’ is understood to mean ‘.−5’.

address±
An address ending with ‘+’ or ‘−’, adds or subtracts 1.  As a consequence of this rule and the previous rule, the line-address ‘−’ refers to the line before the current line.  Moreover, trailing ‘+’ and ‘−’ characters have cumulative effect, so ‘−−’ refers to the current line less 2.

^To maintain compatibility with earlier versions of ed, the character ‘^’ in line-addresses is equivalent to ‘−’.

Ed operations do not necessarily use line-addresses; they may use one or two.  Operations which don’t use line-addresses regard the presence of a line-address as an error.  Operations which accept one or two line-addresses assume default line-addresses if these are not specified.  If more line-addresses are given than such an operation requires, the last one or two (depending on what is accepted) are used.  The second line-address of any two-address sequence must be greater than the first line-address — that is, the second line must follow the first line in the buffer. 

LIST OF OPERATIONS

Ed operates in one of two major modes: command mode and text input mode.  Ed always starts up in command mode. 

While you are typing commands at ed, you are in command mode. Some commands — a for append, c for change, and i for insert — provide for adding new text to the buffer.  While ed is accepting new text, you are in text input mode.  You exit from text input mode by typing a period ‘.’ alone at the beginning of a line.  Ed then reverts to command mode.  For example, here is a very short illustration of command mode versus text mode:

example%  ed  winelist(tell ed to edit a file called winelist)
42(ed states there are 42 characters in the file)
1,$p(in command mode — tell ed to print all lines)
1978  Chateau Chunder
1979  Redeye Canyon
a(in command mode — tell ed to append text)
1980  Doomsday Special(text input mode — add a new line)
.(period ends text input mode)
p(back in command mode — print last line entered)
1980  Doomsday Special
w(command mode — write the file)
65(ed displays the number of characters written)
q(command mode — quit the edit session)
example%(back in the Shell)

If you interrupt ed, it displays ‘?interrupted’ and returns to command mode.

a    Append Text.
Reads the text entered in input mode and appends it to the buffer after the addressed line.  a accepts one line-address — default line-address is the current line.  The new current line is the last line input, or at the addressed line if no text is entered.  Address ‘0’ is a valid place to append text, in which case text is placed at the beginning of the buffer. 

c    Change Lines.
Deletes the addressed lines, then accepts input text which replaces these lines.  c accepts two line-addresses — default line-address is the current line.  The current line is left on the last line input, or at the line preceding the deleted lines if no text is entered. 

d    Delete Lines.
Delete the addressed lines from the buffer.  d accepts two line-addresses — default line-address is the current line.  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 filenameR    Edit a file.
Deletes the entire contents of the buffer, and then reads in the named file.  e sets the current line to the last line of the buffer, and reports the number of characters read into the buffer.  e remembers filename for possible use as a default file name in a subsequent r or w operations.  If no filename is given, the remembered filename is used.  e displays a ? if the buffer has not been written out since the last change made — a second e operation says you really mean it. 

E filename
Same as e, but will silently allow you to quit an editing session without warning you if you have not written your file.  e, on the other hand, reminds you to save your changes if you have altered the buffer at all. 

f filename    Display Remembered Filename.
Display the currently ‘remembered filename’.  If filename is given, the currently ‘remembered filename’ is changed to filename.

g/regular expression/operation list
This is the global operation: perform operation list on all lines in the range of line-addresses containing regular expression.  g accepts two line-addresses — default is all lines in the buffer.  Also see the v operation, which inverts the sense of regular expression.

If your operation list actually takes up more than a single line, you must end every line except the last (the true ‘end’ of the global operation) with an escape character, ‘\’.  For example, if you want to substitute ‘jimjams’ for ‘frammis’, then append several lines of text to every line containing the string ‘widget’ and print those lines, you would type this sequence:

g/widget/s/frammis/jimjams/\
a \
new line of text\
another new line of text\
.\
p

Note that the a, i, and c operations, which put ed in input mode, are permitted in the operation list; the final . terminating input may be omitted if it is the last line of the operation list.  The g and v operations are not permitted in the operation list.

i    Insert Text.
Insert lines of text into the buffer before the addressed line. i accepts one line-address — default line-address is the current line.  The current line is placed at the last line input; if no text is input, the current line is left at the line before the addressed line.  i differs from a only in the placement of the text. 

j    Join Lines.
Joins the addressed lines into a single line; intermediate newlines simply disappear.  j accepts two line-addresses — default is the current line and the following line.  The current line is placed at the resulting line. 

kx    Mark Line.
Marks the addressed line with name x (the name must be a lower-case letter).  The line-address form ′x then addresses this line.  k accepts one line-address — default line-address is the current line. 

l    Display Non-printing Characters.
Displays non-graphic characters in the addressed lines such that they are displayed in two-digit octal, and long lines are folded.  l accepts two line-addresses — default line-address is the current line.  l may be placed on the same line after any non-I/O operation. 

maddress    Move lines.
Reposition the addressed lines after the line-addressed by address. m accepts two line-addresses to specify the range of lines to be moved — default line-address is the current line.  The last of the moved lines becomes the current line. 

p    Print (display) Lines.
Displays the addressed lines.  p accepts two line-addresses — default line-address is the current line.  The current line is placed at the last line printed.  p may be placed on the same line after any non-I/O operation. 

PSynonym for p. 

q    Quit Edit Session.
Exit from the editing session.  Note, however, that the buffer is not automatically written out (do a ‘w’ to write if you want to save your changes).  Ed warns you once if you haven’t saved your file — a second q says you really mean it. 

QSame as q, but you don’t get any warning if you haven’t previously written out the buffer. 

r filename    Read from file.
Reads the contents of filename into the buffer after the addressed line.  If filename is not given, the ‘remembered filename’, if any, is used (see e and f).  r accepts one line-address — default line-address is $.  If line-address ‘0’ is used, r reads the file in at the beginning of the buffer.  If the read is successful, r displays the number of characters read in.  The current line is left at the last line read in from the file. 

s/regular expression/replacement string/    or,

s/regular expression/replacement string/g
Substitute the replacement string for the first occurrence of regular expression on each line where the regular expression occurs.  In the first form of the s operation, only the first occurrence of the matched string on each line is replaced.  If you use the g (global) suffix, all occurrences of the regular expression are replaced in the line.  Keep the g suffix of the s operation distinct from the g operation itself — they are completely different.  s accepts two line-addresses to delimit the range of lines within which the substitutions should be done — default line-address is the current line.  The current line is left at the last line substituted. 

Special Characters:

Any punctuation character may be used instead of ‘/’ to delimit the regular expression and the replacement string.

An ampersand ‘&’ appearing in the replacement string 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 ‘\’. 

taddress    Transfer Lines.
Transfers a copy of the addressed lines to after line address.  transfer is like move, but it makes copies of the lines, leaving the original text where it was.  t accepts two line-addresses preceeding the operation letter — default line-address is default.  The current line is left on the last line of the copy.  ‘0’ is a legal line-address for the destination. 

u    Undo.  Undo previous substitute.
undo undoes the effect of the the last substitute operation, providing that the current line has not been moved since the substitute operation.

v/regular expression/operation list
Like a negative of the global operation, g:  perform operation list on all lines except those containing regular expression.  v accepts two line-addresses — default is all lines in the file. 

w    Write Lines.
Write the addressed lines from the buffer into the file specified by the ‘remembered filename’.  w accepts two line-addresses — default is all lines in the file.  The current line is unchanged.  If the write is successful, ed displays the number of characters written. 

w filename    Write Lines.
Write the addressed lines into filename.  Filename is created if it does not already exist.  Filename becomes the ‘remembered filename’ (see the e and f operations).  w accepts two line-addresses — default is all lines in the file.  The current line is unchanged.  If the write is successful, ed displays the number of characters written. 

W filename
Same as w, but appends the addressed lines to the named file instead of overwriting the file.  W accepts two line-addresses — default is all lines in the file. 

x    Encrypt File.
When x is used, ed demands a key string from the standard input.  Later r, e, and w operations will encrypt and decrypt the text with this key by the algorithm of crypt(1). An explicitly empty key turns off encryption.

=  Display Line Number.
Display the line number of the addressed line.  = accepts one line-address — default line-address is $.  The current line is unchanged by this operation. 

!<shell command>
The remainder of the line after the ‘!’ is sent to sh(1) to be interpreted as a shell command.  The current line is unchanged.

address<newline>
Display the addressed line. If you type a line-address and type RETURN, ed displays the addressed line.  If you simply type RETURN, the line following the current line is displayed (equivalent to ‘.+1p’).  This is useful for stepping through text.

REGULAR EXPRESSIONS

Ed supports a limited form of regular expression notation.  A regular expression (also known as a pattern) specifies a set of strings of characters — 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 Line Addressing, above), and also for selecting strings to be substituted in the s (substitute) operation described previously. 

An empty regular expression, indicated by two regular expression delimiters in a row, stands for a copy of the last regular expression encountered. 

Any given regular expression matches the the longest among the leftmost matches in a line. 

In the following specification for regular expressions, the notation c stands for any single ordinary character, where a character is anything except a newline character. 

cany ordinary character except a special character matches itself.  Special characters are the delimiters that actually surround the regular expression, plus \ (the escape character), [ (the opening bracket for a character class as described below), . (period which matches any single character), and sometimes the ∗ (closure) ^ and $ characters.  If you want a literal occurrence of one of these special characters you must escape them with the \ character. 

^at the very start of the regular expression constrains the match to the beginning of the 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 at the very start of the regular expression. 

$at the very end of the regular expression constrains the match to the end of the 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 at the very end of the regular expression. 

.(period) matches any single character except a newline character. 

[string]
A string of characters enclosed in brackets matches any one of the characters in the brackets.  For example, [abcxyz] matches any single character from the set ‘abcxyz’.  If the first character inside the bracket is a ^, the string matches any character not inside the brackets.  For instance, [^456787] matches any character except ‘45678’.  You can use a shorthand notation to refer to an inclusive range of characters:  a−b.  Such a bracketed string of characters is known as a character class.

When two regular expressions are concatenated, they match the leftmost and then the longest possible string that can be divided with the first part of the string matching the first regular expression, followed by the second string matching the second regular expression. 

∗Any regular expression followed by ∗ matches a sequence of 0 or more matches of the regular expression.  Such a pattern is called a closure.  For example:  [a−z][a−z]∗ matches any string of one or more lower case letters. 

\(regular expression\)
The regular expression within the \( and \) brackets essentially ‘remembers’ whatever the regular expression matches.  This provides a mechanism for extracting parts of strings.  There can be up to nine such partial matches in a string.  Parenthesized regular expressions can be nested.

\nwhere n is in the range 1 thru 9, matches a copy of the string that the bracketed regular expression beginning with the nth \( matched, as described in the previous paragraph on matching parts of strings.  When nested, parenthesized subexpressions are present, n is determined by counting occurrences of \( starting from the left. 

Regular expressions are used in line-addresses to specify lines and in one operation (see s for substitute above) 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.

FILES

/tmp/e∗
ed.hup: work is saved here if telephone hangs up

SEE ALSO

Using the ed Line Editor in Editing and Text Processing on the Sun Workstation.
crypt(1)encode and decode
ex(1)extended line editor (part of vi)
sed(1)stream editor
vi(1)visual (display) editor

BUGS

The l operation mishandles DEL. 
The undo operation removes marks from affected lines. 
Because 0 is an illegal line-address for a w operation, it is not possible to create an empty file with ed.  Use cat(1) to create an empty file.

RESTRICTIONS

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.  Since each line uses two bytes of memory, the limit on the number of lines should not be exceeded in practice. 

When reading a file, ed discards ASCII NUL characters and all characters after the last newline.  ed refuses to read files containing non-ASCII characters. 

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

Sun Release 1.1  —  Last change: 13 March 1984

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