ED(1)
NAME
ed − text editor
USAGE
ed [ − ] [ name ]
DESCRIPTION
Ed is the standard text editor. If a you specify a name argument, ed simulates an e command (see below) on the named file; i.e., ed reads the file into its buffer for editing purposes. The optional dash (−) suppresses the printing of explanatory output. You should use it when the standard input is an editor script.
Ed operates on a copy of any file it is editing. Changes made in the copy have no effect on the file until you supply a w (write) command. The copy of the text being edited resides in a temporary file called the buffer.
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 in input mode. In this mode, ed only collects input and does not recognize any commands. To exit the input mode, type a single period at the beginning of the current line.
REGULAR EXPRESSIONS
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, character means any character but newline.
1.Any character except a special character matches itself. Special characters are the regular expression delimiter, plus the backslash (\), left bracket ([), and period (.) characters, and sometimes the caret (^), asterisk (*), and dollar sign ($) characters..
2.A period matches any character.
3.A backslash (\) followed by any character except a digit or a set of parentheses matches that character.
4.A nonempty string s bracketed [s] (or [^s]) matches any character in (or not in) s. In s, a backslash (\) has no special meaning, and a right bracket (]) may only appear as the first letter. A substring a−b, with a and b in ascending ASCII order, stands for the inclusive range of ASCII characters.
5.A regular expression of the form 1-4 followed by an asterisk (*) matches a sequence of zero or more matches of the regular expression.
6.A regular expression, x, of the form 1-8, bracketed \(x\) matches what x matches.
7.A backslash (\) followed by a digit n matches a copy of the string that the bracketed regular expression beginning with the nth \( matched.
8.A regular expression of the form 1-8, x, followed by a regular expression of the form 1-7, y matches a match for x followed by a match for y, with the x match being as long as possible while still permitting a y match.
9.A regular expression of the form 1-8 preceded by a caret (^), or followed by a dollar sign ($), is constrained to matches that begin at the left (or end at the right) end of a line.
10.A regular expression of the form 1-9 picks out the longest among the leftmost matches in a line.
11.An empty regular expression stands for a copy of the last regular expression encountered.
Regular expressions are used in addresses to specify lines and in one command (see the s command below) to specify a portion of a line that is to be replaced. If you want to use one of the regular expression metacharacters as an ordinary character, you may precede that character by a backslash (\). This also applies to the character bounding the regular expression, often a slash (/) character, and to the backslash (\) itself.
ADDRESSES
To understand addressing in ed, you must 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. A period (.) addresses the current line.
2. A dollar sign ($) addresses the last line of the buffer.
3. A decimal number n addresses the nth line of the buffer.
4. An ′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.
6. A regular expression enclosed in question marks (?) 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.
7. 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.
8. If an address begins with a plus (+) or minus (−) sign, the addition or subtraction is taken with respect to the current line; e.g., −5 means .−5.
9. If an address ends with a plus (+) or minus (−) sign, then one is added (or subtracted). As a consequence of this rule and rule 8, the address of minus (−) refers to the line before the current line. Moreover, trailing plus (+) and minus (−) signs have a cumulative effect, so two minuses (−−) refer to the current line minus two.
10. To maintain compatibility with earlier versions of the editor, a caret (^) in addresses is equivalent to a minus (−).
Commands may require zero, one, or two addresses. Commands requiring no addresses regard the presence of an address as an error. Commands accepting 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 usually separated from each other by a comma (,). They may also be separated by a semicolon (;). In this case, the current line (represented by a period) is set to the previous address before the next address is interpreted. This feature helps 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. The special form represented by a percent sign (%) is an abbreviation for the 1,$ address pair.
COMMANDS
In the following list of ed commands, the default addresses are shown in parentheses. The parentheses are not part of the address.
As mentioned, it is generally illegal for more than one command to appear on a line. However, most commands may be suffixed by p or by l, in which case the current line is either printed or listed respectively in the way discussed below. Commands may also be suffixed by n, meaning the output of the command is to be line numbered. These suffixes may be combined in any order.
( . )a
<text>
.
Read the given text and append it after the addressed line. Leave a period on the last line input or at the addressed line. Address 0 is legal for this command; text is placed at the beginning of the buffer.
( . , . )c
<text>
.
Delete the addressed lines, then accept input text to replace these lines. Leave a period at the last line input. If no lines were input, leave it at the line preceding the deleted lines.
( . , . )d
Delete 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
Delete the entire contents of the buffer, and then read in the named file. Set the current line (.) to the last line of the buffer. Print the number of characters read. Remember filename for possible use as a default filename in a subsequent r or w command. If filename is missing, use the remembered name.
E filename
Perform the same function as e, but do not produce diagnostics if no w was specified since the last buffer alteration.
f filename
Without an argument, print the currently remembered filename. With an argument, change the currently remembered filename to filename.
(1,$)g/regular-expression/command-list
Mark every line that matches the given regular expression. Then, for every such line, execute the given command list with the current line initially set to that line. A single command or the first of multiple commands appears on the same line with this command. All lines of a multiline list except the last line must end with a backslash (\). A, i, and c commands and associated input are permitted; the period that terminates input mode may be omitted if it will be on the last line of the command list. The commands g and v are not permitted in the command list.
( . )i
<text>
.
Insert the given text before the addressed line. Leave the period at the last line input or the line before the addressed line. This command differs from the a command only in the placement of the text.
( . , . +1)j
Join the addressed lines into a single line. Intermediate newlines simply disappear. Leave a period at the resulting line.
( . )kx
Mark the addressed line with name x, which must be a lowercase letter. The ′x address form then addresses this line.
( . , . )l
List the addressed lines in an unambiguous way. Print nongraphic characters in two-digit octal, and fold long lines. You may place this command on the same line following any non-I/O command.
( . , . )ma
Reposition the addressed lines after the line addressed by a. The last of the moved lines becomes the current line.
( . , . )p
Print the addressed lines. The last line printed becomes the current line. You may place this command on the same line following any non-I/O command.
( . , . )P
Perform the same function as the p command.
q Cause ed to exit without doing an automatic write of a file.
Q Perform the same function as the q command, but do not produce diagnostics if no w has been given since the last buffer alteration.
($)r filename
Read in the given file after the addressed line. Without a filename argument, use the remembered filename, if any (refer to the e and f commands). The filename is remembered if there was no remembered filename 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. The last line read in from the file becomes the current line.
(.,.)s/ regular expression / replacement /
or
.BI (.,.)s/ regular expression / replacement /g
Search each addressed line for an occurrence of the specified regular expression. On each line in which a match is found, replace all matched strings by the specified replacement, if the global replacement indicator g appears after the command. If the global indicator does not appear, replace only the first occurrence of the matched string. It is an error for the substitution to fail on all addressed lines. You may use any punctuation character instead of a slash (/) to delimit the regular and the replacement. The last line substituted becomes the current line.
An ampersand (&) appearing in the replacement is replaced by the string matching the regular expression. The special meaning of the ampersand in this context may be suppressed by preceding it by a backslash (\). The characters \n (where n is a digit) are replaced by the text matched by the nth regular subexpression enclosed between the \( and \) characters. When nested subexpressions in parentheses are present, n is determined by counting occurrences of the \( characters, starting from the left.
Lines may be split by substituting newline characters into them. The newline in the replacement string must be escaped by preceding it with a backslash (\).
One or two trailing delimiters may be omitted, implying the p suffix. The special form s followed by no delimiters repeats the most recent substitute command on the addressed lines. The s may be followed by these letters: r (use the most recent regular expression for the left-hand side, instead of the most recent left-hand side of a substitute command), p (complement the setting of the p suffix from the previous substitution), or g (complement the setting of the g suffix). You may combine these letters in any order.
( . , . )t a
Perform the same function as the m command, but also place a copy of the addressed lines after address a (which may be 0). The last line of the copy becomes the current line.
( . , . )u
Restore the buffer to its state before the most recent buffer-modifying command. Also restore the current line. Buffer-modifying commands are a, c, d, g, i, k, m, r, s, t, and v. For the purposes of the u command, g and v are considered to modify a single buffer. The u command is its own inverse.
When ed runs out of memory a full “undo” is not possible, and u can only undo the effect of the most recent substitute on the current line. This restricted “undo” also applies to editor scripts when ed is invoked with the dash (−) option.
(1,$)v/regularexpression/commandlist
Perform the same function as the g command, but execute the command list with the period initially set to every line except those matching the regular expression.
(1,$)w filename
Write the addressed lines onto the given file. Create the file if it does not exist. Remember the filename, if there is no remembered filename already. If you do not specify a filename, this command uses the remembered filename, if any (refer to the e and f commands). The current line is unchanged. If the command is successful, the number of characters written is printed.
(1,$)W filename
Perform the same function as the w command, but append the addressed lines to the file.
(1,$)wq filename
Perform the same function as the w command, and then exit the editor after the file is written.
( . +1)zor,
( . +1)zn
Scroll through the buffer starting at the addressed line. With an argument, print n lines. Without an argument, print 22 lines by default. The last line printed becomes the current line. The value n is sticky in that it becomes the default for future z commands.
($)=
Print the line number of the addressed line. The current line is unchanged by this command.
!< shell command >
Send the remainder of the line after the exclamation point (!) to sh(1) to be interpreted as a command. The current line is unchanged by this command.
( . +1, . +1)<newline>
Print the addressed line if an address appears by itself on a line. A blank line alone is equivalent to “.+1p” and is useful for stepping through text. If two addresses are present with no intervening semicolon, ed prints the range of lines. If they are separated by a semicolon, the second line is printed.
If an interrupt signal (ASCII DEL) is sent, ed prints the message “?interrupted” and returns to its command level.
CAUTIONS
Size limitations: 512 characters per line, 256 characters per global command list, 64 characters per filename, and, on minicomputers, 128K characters in the temporary file. The limit on the number of lines depends on the amount of memory: each line takes 2 words.
When reading a file, ed discards ASCII NUL characters and all characters after the last newline. It refuses to read files containing non-ASCII characters.
The l command mishandles DEL.
The u command causes marks to be lost on affected lines.
FILES
/tmp/e*
edhup: work is saved here if your terminal hangs up
DIAGNOSTICS
“?name” for inaccessible file; “?self-explanatory message” for other errors.
To protect against throwing away valuable work, ed produces an error message if you try to use a q or e command without having first done a w since the last buffer change. A second q or e is obeyed regardless.