ED(1) ED(1) NAME ed - text editor SYNOPSIS ed [ - ] [ -o ] [ -X ] [ 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. If -X is pre- sent, an X command is simulated first to handle an encrypted file. The optional - suppresses the printing of character counts by e, r, and w commands and the confirming `!' by ! commands. If -o is present, ed writes all its interactive output on the standard error file instead of the standard output file, starts with /dev/stdout as its default output file, ignores any input file argument, suppresses printing of character counts, and simulates an initial a 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. Commands to ed have a simple and regular structure: zero or more addresses followed by a single character command, pos- sibly 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 col- lected. 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 charac- ters. 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 char- acter but newline. 1. Any character except a special character matches itself. Special characters are the regular expression delimiter plus \[. and sometimes ^*$. ED(1) ED(1) 2. A . matches any character. 3. A \ followed by any character except a digit or () matches that character. 4. A nonempty string s bracketed [s] (or [^s]) matches any character in (or not in) s. In s, \ has no special meaning, and ] 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 form 1-4 followed by * matches a sequence of 0 or more matches of the regular expres- sion. 6. A regular expression, x, of form 1-8, bracketed \(x\) matches what x matches. 7. A \ 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 form 1-8, x, followed by a reg- ular expression of 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 form 1-8 preceded by ^ (or fol- lowed by $), is constrained to matches that begin at the left (or end at the right) end of a line. 10. A regular expression of 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 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. 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; how- ever, the exact effect on the current line is discussed under the description of the command. Addresses are con- structed as follows. ED(1) ED(1) 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 lower-case 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 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. 7. An address followed by a plus sign `+' or a minus sign `-' followed by a decimal number specifies that address plus (resp. minus) the indicated number of lines. The plus sign may be omitted. 8. An address followed by `+' (or `-') followed by a regu- lar expression enclosed in slashes specifies the first matching line following (or preceding) that address. The search wraps around if necessary. The `+' may be omitted, so `0/x/' addresses the first line in the buffer with an `x'. Enclosing the regular expression in `?' reverses the search direction. 9. 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'. 10. If an address ends with `+' or `-', then 1 is added (resp. subtracted). As a consequence of this rule and rule 9, the address `-' refers to the line before the current line. Moreover, trailing `+' and `-' charac- ters have cumulative effect, so `--' refers to the cur- rent line less 2. 11. To maintain compatibility with earlier versions of the editor, the character `^' in addresses is equivalent to `-'. ED(1) ED(1) 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 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. If no address precedes a comma or semicolon, line 1 is assumed; if no address follows, the last line of the buffer is assumed. The second address of any two-address sequence must corre- spond 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. As mentioned, it is generally illegal for more than one com- mand 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. (.)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. (., .)b[+-][pagesize][pln] The browse command provides page-oriented printing. The optional `+' or `-' specifies whether the next or previous page is to be printed; if absent, `+' is assumed. If a pagesize is given, it is used for the current browse command and remembered as the default. The pagesize is initially 20 lines. if a terminal `p' `n', or `l' is provided, the lines are printed in the specified format, and the format is remembered as the default. Initially, `p' is the default. After a browse command, `.' is left at the last line displayed. (., .)c <text> . ED(1) ED(1) 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. The number of characters read is typed. `filename' is remembered for possible use as a default file name in a subsequent r or w command. If `filename' is missing, the remembered name is used. E filename This command is the same as e, except that no diagnos- tic 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 (1, $)g/regular expression/ (1, $)g/regular expression 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 permit- ted; the `.' terminating input mode may be omitted if it would be on the last line of the command list. The commands g and v are not permitted in the command list. Any character other than space or newline may be used instead of `/' to delimit the regular expression. The second and third forms mean `g/regular expression/p'. (.)i <text> ED(1) ED(1) . 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 com- mand only in the placement of the text. (., .+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 lower-case letter. The address form `'x' then addresses this line. (., .)l The list command prints the addressed lines in an unam- biguous way: a tab is printed as `\t', a backspace as `\b', backslashes as `\', a non-printing character is printed as a backslash followed by three octal digits, and a long line is folded, with the second and subse- quent sub-lines indented one tab stop. If the last character in the line is a blank, it is followed by `\n'. The l command may be placed on the same line after any non-i/o command. (., .)ma The move command repositions the addressed lines after the line addressed by a. The last of the moved lines becomes the current line. (., .)n This command is similar to the print command, but pre- fixes each line with its line number and a tab. (., .)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 This command is a synonym for p. 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 diagnos- tic results when no w has been given since the last buffer alteration. ED(1) ED(1) ($)r filename The read command reads in the given file after the addressed line. If no file name is given, the remem- bered file name, if any, is used (see e and f com- mands). 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. ( ., .)sn/regular expression/replacement/ ( ., .)sn/regular expression/replacement/g ( ., .)sn/regular expression/replacement The substitute command searches each addressed line for an occurrence of the specified regular expression. On each line in which n matches are found (n defaults to 1 if missing), the nth matched string is replaced by the replacement specified. If the global replacement indi- cator `g' appears after the command, all subsequent matches on the line are also 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. `.' is left at the last line substi- tuted. The third form means `s/regular expression/replacement/p'; the replacement must not be empty. An ampersand `&' appearing in the replacement is replaced by the string matching the regular expression. 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, parenthe- sized subexpressions are present, n is determined by counting occurrences of `\(' starting from the left. A literal `&', `/', `\' or newline may be included in a replacement by prefixing it with `\'. (., .)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 restores the preceding contents of the current line, which must be the last line in which a substitution was made. (1, $)v/regular expression/command list ED(1) ED(1) (1, $)v/regular expression/ (1, $)v/regular expression This command is the same as the global command g except that the command list is executed g with `.' initially set to every line except those matching the regular expression. The second and third forms mean `v/regular expression/p'. (1, $)w filename The write command writes the addressed lines onto the given file. If the file does not exist, it is created mode 666 (readable and writable by everyone). The file name is remembered if there was no remembered file name already. If no file name is given, the remembered file name, if any, is used (see e and f commands). `.' is unchanged. If the command is successful, the number of characters written is printed. (1, $)W filename This command is the same as w, except that the addressed lines are appended to the file. X A key string is demanded from the standard input. Later r, e and w commands will encrypt and decrypt the text with this key by the algorithm of crypt(1). An explicitly empty key turns off encryption. ($)= 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 sh(1) to be interpreted as a command. `.' is unchanged. (.+1)<newline> An address or addresses alone on a line cause the addressed lines 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 charac- ters per global command list, 127 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 newline. It refuses to read ED(1) ED(1) files containing non-ASCII characters. FILES /tmp/e* ed.hup: work is saved here if terminal hangs up SEE ALSO B. W. Kernighan, A Tutorial Introduction to the ED Text Edi- tor B. W. Kernighan, Advanced editing on UNIX sed(1), crypt(1) DIAGNOSTICS `?name' for inaccessible file; `?' for errors in commands; `?TMP' for temporary file overflow. 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. BUGS A ! command cannot be subject to a g command.