ed - Text editor
ed is the standard UNIX text editor. When the file 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. 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 of the text being edited resides in a temporary buffer. There is only one buffer.
Commands to ed have a simple and regular structure: zero, one, or two addresses followed by a single-character command, possibly followed by parameters to that command. These addresses specify one or more lines in the buffer. Every command that requires addresses has default addresses, so that the addresses can very often be omitted.
In general, only one command may appear on a line. Certain commands allow the input of text. This text is placed in the appropriate place in 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 entering a period (.) alone at the beginning of a line.
The ProLine version of ed does not support regular expression parsing, other than simple /text/ and ?text? syntaxes for bi-directional pattern searching. A null regular expression, // or ??, is equivalent to the last one used.
To understand addressing in ed , it is necessary to know that there is a current line at all times. Generally speaking, the current line is the last line affected by a command; the exact effect on the current line is discussed under the description of each command. Addresses are constructed as follows:
The character . addresses the current line.
The character $ addresses the last line of the buffer.
A decimal number n addresses the n-th line of the buffer.
A regular expression enclosed by slashes (/) addresses the first line found by searching forward from the line following the current line toward the end of the buffer and stopping at the first line containing a string matching the regular expression. 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.
A regular expression enclosed in question marks (?) addresses the first line found by searching backward from the line preceding the current line toward the beginning of the buffer and stopping at the first line containing a string matching the regular expression. If necessary, the search wraps around to the end of the buffer and continues up to and including the current line. Also see the last paragraph before Files below.
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 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.
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 7 immediately above, the address - refers to the line preceding the current line. Moreover, 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 .,$.
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 address(es) 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 (see rules 4 and 5 above). 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 p or by l, in which case the current line is either printed or listed, respectively, as discussed below under the p and l commands.
The append command reads the given text and appends it after the addressed line; dot is left at the last inserted line, or, if there were no inserted lines, 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. This is equivalent to the 1i command.
The change command deletes the addressed lines, then accepts input text that replaces these lines; dot is left at the last line input, or, if there were none, at the first line that was not deleted.
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.
The edit command causes the entire contents of the buffer to be deleted, and then the named file to be read in; dot 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.
The Edit command is like e, except the editor does not check to see if any changes have been made to the buffer since the last w command.
If file is given, the filename command changes the currently remembered filename to file; otherwise, it prints the currently remembered filename.
The help command gives a short error message that explains the reason for the most recent ? diagnostic.
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 diagnostic if there was one. The H command alternately turns this mode on and off; it is initially off.
The insert command inserts the given text before the addressed line; dot is left at the last inserted line, or if there were no inserted lines, at the addressed line. This command differs from the a command only in the placement of the input text. Address 0 is not legal for this command.
The join command joins contiguous lines by removing the appropriate newline characters. If only one address is given, this command does nothing.
The list command prints the addressed lines in an unambiguous way: a few nonprinting characters (e.g., tab, backspace) are represented by 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.
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; dot is left at the last line moved.
The number command prints the addressed lines, preceding each line by its line number and a tab character; dot is left at the last line printed. The n command may be appended to any command other than e, f, r, or w.
The print command prints the addressed lines; dot 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.
The editor will prompt with a * for all subsequent commands. If the optional prompt is included it becomes the new prompt string. P alone alternately turns this mode on and off; it is initially on.
The quit command causes ed to exit. No automatic write of a file is done.
The editor exits without checking if changes have been made in the buffer since the last w command.
The read command reads in the given file after last line in the buffer. If no filename is given, 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. If the read is successful, the number of characters read is typed; dot is set to the last line read in.
The substitute command searches each addressed line for an occurrence of the specified regular expression. In each line in which a match is found, all (nonoverlapped) 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. It is an error for the substitution to fail on all addressed lines. Any character other than space or newline may be used instead of / to delimit the regular expression and the replacement; dot is left at the last line on which a substitution occurred.
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); dot is left at the last line of the copy.
The write command writes the addressed lines into the named file. If the file does not exist, it is created. 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); dot is unchanged. If the command is successful, the number of characters written is displayed.
The line number of the addressed line is typed; dot is unchanged by this command.
An address alone on a line causes the addressed line to be printed. A RETURN alone on a line is equivalent to .+1p. This is useful for stepping forward through the editing buffer a line at a time.
If an interrupt signal is sent, ed prints a question mark (?) and returns to its command level.
Some size limitations: 255 characters per line, 255 characters per global command list, 64 characters per filename. The limit on the number of lines depends on the amount of memory.
Files that contain characters not in the ASCII set (bit 8 on), cannot be edited by ed. Editable file types are TXT and CMD. TXT files cannot be the random-access text files.
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 you to continue editing. A second e or q command at this point will take effect.
edit(C), grep(C), csh(C), setenv(C), vedit(C)