ED(1) ED(1) NNAAMMEE ed, red - text editor SSYYNNOOPPSSIISS ed [-] [-Gs] [-p _s_t_r_i_n_g] [_f_i_l_e] red [-] [-Gs] [-p _s_t_r_i_n_g] [_f_i_l_e] DDEESSCCRRIIPPTTIIOONN eedd is a line-oriented text editor. It is used to create, display, mod- ify and otherwise manipulate text files. rreedd is a restricted eedd: it can only edit files in the current directory and cannot execute shell commands. If invoked with a _f_i_l_e argument, then a copy of _f_i_l_e is read into the editor's buffer. Changes are made to this copy and not directly to _f_i_l_e itself. Upon quitting eedd, any changes not explicitly saved with a _`_w_' command are lost. Editing is done in two distinct modes: _c_o_m_m_a_n_d and _i_n_p_u_t. When first invoked, eedd is in command mode. In this mode commands are read from the standard input and executed to manipulate the contents of the edi- tor buffer. A typical command might look like: ,s/_o_l_d/_n_e_w/g which replaces all occurences of the string _o_l_d with _n_e_w. When an input command, such as _`_a_' (append), _`_i_' (insert) or _`_c_' (change), is given, eedd enters input mode. This is the primary means of adding text to a file. In this mode, no commands are available; instead, the standard input is written directly to the editor buffer. Lines consist of text up to and including a _n_e_w_l_i_n_e character. Input mode is terminated by entering a single period (_.) on a line. All eedd commands operate on whole lines or ranges of lines; e.g., the _`_d_' command deletes lines; the _`_m_' command moves lines, and so on. It is possible to modify only a portion of a line by means of replacement, as in the example above. However even here, the _`_s_' command is applied to whole lines at a time. In general, eedd commands consist of zero or more line addresses, fol- lowed by a single character command and possibly additional parameters; i.e., commands have the structure: _[_a_d_d_r_e_s_s _[_,_a_d_d_r_e_s_s_]_]_c_o_m_m_a_n_d_[_p_a_r_a_m_e_t_e_r_s_] The address(es) indicate the line or range of lines to be affected by the command. If fewer addresses are given than the command accepts, then default addresses are supplied. OOPPTTIIOONNSS -G Forces backwards compatibility. Affects the commands _`_G_', _`_V_', _`_f_', _`_l_', _`_m_', _`_t_', and _`_!_!_'. -s Suppresses diagnostics. This should be used if eedd's standard input is from a script. -p _s_t_r_i_n_g Specifies a command prompt. This may be toggled on and off with the _`_P_' command. _f_i_l_e Specifies the name of a file to read. If _f_i_l_e is prefixed with a bang (!), then it is interpreted as a shell command. In this case, what is read is the standard output of _f_i_l_e executed via _s_h(1). To read a file whose name begins with a bang, prefix the name with a backslash (\). The default filename is set to _f_i_l_e only if it is not prefixed with a bang. LLIINNEE AADDDDRREESSSSIINNGG An address represents the number of a line in the buffer. eedd maintains a _c_u_r_r_e_n_t _a_d_d_r_e_s_s which is typically supplied to commands as the default address when none is specified. When a file is first read, the current address is set to the last line of the file. In general, the current address is set to the last line affected by a command. A line address is constructed from one of the bases in the list below, optionally followed by a numeric offset. The offset may include any combination of digits, operators (i.e., _+, _- and _^) and whitespace. Addresses are read from left to right, and their values are computed relative to the current address. One exception to the rule that addresses represent line numbers is the address _0 (zero). This means "before the first line," and is legal wherever it makes sense. An address range is two addresses separated either by a comma or semi- colon. The value of the first address in a range cannot exceed the value of the the second. If only one address is given in a range, then the second address is set to the given address. If an _n_-tuple of addresses is given where _n _> _2_, then the corresponding range is deter- mined by the last two addresses in the _n_-tuple. If only one address is expected, then the last address is used. Each address in a comma-delimited range is interpreted relative to the current address. In a semicolon-delimited range, the first address is used to set the current address, and the second address is interpreted relative to the first. The following address symbols are recognized. . The current line (address) in the buffer. $ The last line in the buffer. _n The _nth, line in the buffer where _n is a number in the range _[_0_,_$_]. - ^ The previous line. This is equivalent to _-_1 and may be repeated with cumulative effect. -_n ^_n The _nth previous line, where _n is a non-negative number. + The next line. This is equivalent to _+_1 and may be repeated with cumulative effect. +_n _w_h_i_t_e_s_p_a_c_e _n The _nth next line, where _n is a non-negative number. _W_h_i_t_e_s_- _p_a_c_e followed by a number _n is interpreted as _+_n. , % The first through last lines in the buffer. This is equivalent to the address range _1_,_$. ; The current through last lines in the buffer. This is equiva- lent to the address range _._,_$. /_r_e/ The next line containing the regular expression _r_e. The search wraps to the beginning of the buffer and continues down to the current line, if necessary. // repeats the last search. ?_r_e? The previous line containing the regular expression _r_e. The search wraps to the end of the buffer and continues up to the current line, if necessary. ?? repeats the last search. '_l_c The line previously marked by a _`_k_' (mark) command, where _l_c is a lower case letter. RREEGGUULLAARR EEXXPPRREESSSSIIOONNSS Regular expressions are patterns used in selecting text. For example, the eedd command g/_s_t_r_i_n_g/ prints all lines containing _s_t_r_i_n_g. Regular expressions are also used by the _`_s_' command for selecting old text to be replaced with new. In addition to a specifying string literals, regular expressions can represent classes of strings. Strings thus represented are said to be matched by the corresponding regular expression. If it is possible for a regular expression to match several strings in a line, then the left- most longest match is the one selected. The following symbols are used in constructing regular expressions: c Any character _c not listed below, including `{', '}', `(', `)', `<' and `>', matches itself. \_c A backslash-escaped character _c other than `{', '}', `(', `)', `<', `>', `b', 'B', `w', `W', `+', and `?' matches itself. . Matches any single character. _[_c_h_a_r_-_c_l_a_s_s_] Matches any single character in _c_h_a_r_-_c_l_a_s_s. To include a `]' in _c_h_a_r_-_c_l_a_s_s, it must be the first character. A range of characters may be specified by separating the end characters of the range with a `-', e.g., `a-z' specifies the lower case characters. The following literal expressions can also be used in _c_h_a_r_-_c_l_a_s_s to specify sets of characters: [:alnum:] [:cntrl:] [:lower:] [:space:] [:alpha:] [:digit:] [:print:] [:upper:] [:blank:] [:graph:] [:punct:] [:xdigit:] If `-' appears as the first or last character of _c_h_a_r_-_c_l_a_s_s, then it matches itself. All other characters in _c_h_a_r_-_c_l_a_s_s match themselves. Patterns in _c_h_a_r_-_c_l_a_s_s of the form: [._c_o_l_-_e_l_m.] or, [=_c_o_l_-_e_l_m=] where _c_o_l_-_e_l_m is a _c_o_l_l_a_t_i_n_g _e_l_e_m_e_n_t are interpreted according to _l_o_c_a_l_e(5) (not currently supported). See _r_e_g_e_x(3) for an explanation of these constructs. [^_c_h_a_r_-_c_l_a_s_s] Matches any single character, other than newline, not in _c_h_a_r_- _c_l_a_s_s. _c_h_a_r_-_c_l_a_s_s is defined as above. ^ If `^' is the first character of a regular expression, then it anchors the regular expression to the beginning of a line. Otherwise, it matches itself. $ If `$' is the last character of a regular expression, it anchors the regular expression to the end of a line. Other- wise, it matches itself. \(_r_e\) Defines a (possibly null) subexpression _r_e. Subexpressions may be nested. A subsequent backreference of the form `\_n', where _n is a number in the range [1,9], expands to the text matched by the _nth subexpression. For example, the regular expression `\(a.c\)\1' matches the string `abcabc', but not `abcadc'. Subexpressions are ordered relative to their left delimiter. * Matches the single character regular expression or subexpres- sion immediately preceding it zero or more times. If '*' is the first character of a regular expression or subexpression, then it matches itself. The `*' operator sometimes yields unexpected results. For example, the regular expression `b*' matches the beginning of the string `abbb', as opposed to the substring `bbb', since a null match is the only left-most match. \{_n_,_m\} \{_n_,\} \{_n\} Matches the single character regular expression or subexpres- sion immediately preceding it at least _n and at most _m times. If _m is omitted, then it matches at least _n times. If the comma is also omitted, then it matches exactly _n times. If any of these forms occurs first in a regular expression or subexpression, then it is interpreted literally (i.e., the reg- ular expression `\{2\}' matches the string `{2}', and so on). \< \> Anchors the single character regular expression or subexpres- sion immediately following it to the beginning (\<) or ending (\>) of a _w_o_r_d, i.e., in ASCII, a maximal string of alphanu- meric characters, including the underscore (_). The following extended operators are preceded by a backslash (\) to distinguish them from traditional eedd syntax. \` \' Unconditionally matches the beginning (\`) or ending (\') of a line. \? Optionally matches the single character regular expression or subexpression immediately preceding it. For example, the regu- lar expression `a[bd]\?c' matches the strings `abc', `adc' and `ac'. If \? occurs at the beginning of a regular expressions or subexpression, then it matches a literal `?'. \+ Matches the single character regular expression or subexpres- sion immediately preceding it one or more times. So the regu- lar expression `a\+' is shorthand for `aa*'. If \+ occurs at the beginning of a regular expression or subexpression, then it matches a literal `+'. \b Matches the beginning or ending (null string) of a word. Thus the regular expression `\bhello\b' is equivalent to `\'. However, `\b\b' is a valid regular expression whereas `\<\>' is not. \B Matches (a null string) inside a word. \w Matches any character in a word. \W Matches any character not in a word. CCOOMMMMAANNDDSS All eedd commands are single characters, though some require additonal parameters. If a command's parameters extend over several lines, then each line except for the last must be terminated with a backslash (\). In general, at most one command is allowed per line. However, most commands accept a print suffix, which is any of _`_p_' (print), _`_l_' (list) , or _`_n_' (enumerate), to print the last line affected by the command. An interrupt (typically ^C) has the effect of aborting the current com- mand and returning the editor to command mode. eedd recognizes the following commands. The commands are shown together with the default address or address range supplied if none is specified (in parenthesis). (.)a Appends text to the buffer after the addressed line, which may be the address 0 (zero). Text is entered in input mode. The current address is set to last line entered. (.,.)c Changes lines in the buffer. The addressed lines are deleted from the buffer, and text is appended in their place. Text is entered in input mode. The current address is set to last line entered. (.,.)d Deletes the addressed lines from the buffer. If there is a line after the deleted range, then the current address is set to this line. Otherwise the current address is set to the line before the deleted range. e _f_i_l_e Edits _f_i_l_e, and sets the default filename. If _f_i_l_e is not specified, then the default filename is used. Any lines in the buffer are deleted before the new file is read. The cur- rent address is set to the last line read. e !_c_o_m_m_a_n_d Edits the standard output of _`_!_c_o_m_m_a_n_d_', (see !_c_o_m_m_a_n_d below). The default filename is unchanged. Any lines in the buffer are deleted before the output of _c_o_m_m_a_n_d is read. The current address is set to the last line read. E _f_i_l_e Edits _f_i_l_e unconditionally. This is similar to the _e command, except that unwritten changes are discarded without warning. The current address is set to the last line read. f _f_i_l_e Sets the default filename to _f_i_l_e. If _f_i_l_e is not specified, then the default unescaped filename is printed. (1,$)g_/_r_e_/_c_o_m_m_a_n_d_-_l_i_s_t Applies _c_o_m_m_a_n_d_-_l_i_s_t to each of the addressed lines matching a regular expression _r_e. The current address is set to the line currently matched before _c_o_m_m_a_n_d_-_l_i_s_t is executed. At the end of the _`_g_' command, the current address is set to the last line affected by _c_o_m_m_a_n_d_-_l_i_s_t. Each command in _c_o_m_m_a_n_d_-_l_i_s_t must be on a separate line, and every line except for the last must be terminated by a back- slash (\). Any commands are allowed, except for _`_g_', _`_G_', _`_v_', and _`_V_'. A newline alone in _c_o_m_m_a_n_d_-_l_i_s_t is equivalent to a _`_p_' command. (1,$)G_/_r_e_/ Interactively edits the addressed lines matching a regular expression _r_e_. For each matching line, the line is printed, the current address is set, and the user is prompted to enter a _c_o_m_m_a_n_d_-_l_i_s_t. At the end of the _`_G_' command, the current address is set to the last line affected by (the last) _c_o_m_m_a_n_d_- _l_i_s_t. The format of _c_o_m_m_a_n_d_-_l_i_s_t is the same as that of the _`_g_' com- mand. A newline alone acts as a null command list. A single `&' repeats the last non-null command list. H Toggles the printing of error explanations. By default, expla- nations are not printed. It is recommended that ed scripts begin with this command to aid in debugging. h Prints an explanation of the last error. (.)i Inserts text in the buffer before the current line. Text is entered in input mode. The current address is set to the last line entered. (.,.+1)j Joins the addressed lines. The addressed lines are deleted from the buffer and replaced by a single line containing their joined text. The current address is set to the resultant line. (.)k_l_c Marks a line with a lower case letter _l_c. The line can then be addressed as _'_l_c (i.e., a single quote followed by _l_c ) in subsequent commands. The mark is not cleared until the line is deleted or otherwise modified. (.,.)l Prints the addressed lines unambiguously. If invoked from a terminal, eedd pauses at the end of each page until a newline is entered. The current address is set to the last line printed. (.,.)m(.) Moves lines in the buffer. The addressed lines are moved to after the right-hand destination address, which may be the address _0 (zero). The current address is set to the last line moved. (.,.)n Prints the addressed lines along with their line numbers. The current address is set to the last line printed. (.,.)p Prints the addressed lines. If invoked from a terminal, eedd pauses at the end of each page until a newline is entered. The current address is set to the last line printed. P Toggles the command prompt on and off. Unless a prompt was specified by with command-line option _-_p _s_t_r_i_n_g, the command prompt is by default turned off. q Quits ed. Q Quits ed unconditionally. This is similar to the _q command, except that unwritten changes are discarded without warning. ($)r _f_i_l_e Reads _f_i_l_e to after the addressed line. If _f_i_l_e is not speci- fied, then the default filename is used. If there was no default filename prior to the command, then the default file- name is set to _f_i_l_e. Otherwise, the default filename is unchanged. The current address is set to the last line read. ($)r !_c_o_m_m_a_n_d Reads to after the addressed line the standard output of _`_!_c_o_m_- _m_a_n_d_', (see the !_c_o_m_m_a_n_d below). The default filename is unchanged. The current address is set to the last line read. (.,.)s_/_r_e_/_r_e_p_l_a_c_e_m_e_n_t_/ (.,.)s_/_r_e_/_r_e_p_l_a_c_e_m_e_n_t_/g (.,.)s_/_r_e_/_r_e_p_l_a_c_e_m_e_n_t_/_n Replaces text in the addressed lines matching a regular expres- sion _r_e with _r_e_p_l_a_c_e_m_e_n_t. By default, only the first match in each line is replaced. If the _`_g_' (global) suffix is given, then every match to be replaced. The _`_n_' suffix, where _n is a postive number, causes only the _nth match to be replaced. It is an error if no substitutions are performed on any of the addressed lines. The current address is set the last line affected. _r_e and _r_e_p_l_a_c_e_m_e_n_t may be delimited by any character other than space and newline (see the _`_s_' command below). If one or two of the last delimiters is omitted, then the last line affected is printed as though the print suffix _`_p_' were specified. An unescaped `&' in _r_e_p_l_a_c_e_m_e_n_t is replaced by the currently matched text. The character sequence _`_\_m_', where _m is a number in the range [1,9], is replaced by the _mth backreference expression of the matched text. If _r_e_p_l_a_c_e_m_e_n_t consists of a single `%', then _r_e_p_l_a_c_e_m_e_n_t from the last substitution is used. Newlines may be embedded in _r_e_p_l_a_c_e_m_e_n_t if they are escaped with a backslash (\). (.,.)s Repeats the last substitution. This form of the _`_s_' command accepts a count suffix _`_n_', or any combination of the charac- ters _`_r_', _`_g_', and _`_p_'. If a count suffix _`_n_' is given, then only the _nth match is replaced. The _`_r_' suffix causes the reg- ular expression of the last search to be used instead of the that of the last substitution. The _`_g_' suffix toggles the global suffix of the last substitution. The _`_p_' suffix toggles the print suffix of the last substitution. The current address is set to the last line affected. (.,.)t(.) Copies (i.e., transfers) the addressed lines to after the right-hand destination address, which may be the address _0 (zero). The current address is set to the last line copied. u Undoes the last command and restores the current address to what it was before the command. The global commands _`_g_', _`_G_', _`_v_', and _`_V_'. are treated as a single command by undo. _`_u_' is its own inverse. (1,$)v_/_r_e_/_c_o_m_m_a_n_d_-_l_i_s_t Applies _c_o_m_m_a_n_d_-_l_i_s_t to each of the addressed lines not match- ing a regular expression _r_e. This is similar to the _`_g_' com- mand. (1,$)V_/_r_e_/ Interactively edits the addressed lines not matching a regular expression _r_e_. This is similar to the _`_G_' command. (1,$)w _f_i_l_e Writes the addressed lines to _f_i_l_e. Any previous contents of _f_i_l_e is lost without warning. If there is no default filename, then the default filename is set to _f_i_l_e_, otherwise it is unchanged. If no filename is specified, then the default file- name is used. The current address is unchanged. (1,$)wq _f_i_l_e Writes the addressed lines to _f_i_l_e, and then executes a _`_q_' command. (1,$)w !_c_o_m_m_a_n_d Writes the addressed lines to the standard input of _`_!_c_o_m_m_a_n_d_', (see the !_c_o_m_m_a_n_d below). The default filename and current address are unchanged. (1,$)W _f_i_l_e Appends the addressed lines to the end of _f_i_l_e. This is simi- lar to the _`_w_' command, expect that the previous contents of file is not clobbered. The current address is unchanged. (.)x Copies (puts) the contents of the cut buffer to after the addressed line. The current address is set to the last line copied. (.,.)y Copies (yanks) the addressed lines to the cut buffer. The cut buffer is overwritten by subsequent _`_y_', _`_s_', _`_j_', _`_d_', or _`_c_' commands. The current address is unchanged. (.+1)z_n Scrolls _n lines at a time starting at addressed line. If _n is not specified, then the current window size is used. The cur- rent address is set to the last line printed. !_c_o_m_m_a_n_d Executes _c_o_m_m_a_n_d via _s_h(1). If the first character of _c_o_m_m_a_n_d is `!', then it is replaced by text of the previous _`_!_c_o_m_m_a_n_d_'. eedd does not process _c_o_m_m_a_n_d for backslash (\) escapes. How- ever, an unescaped _`_%_' is replaced by the default filename. When the shell returns from execution, a `!' is printed to the standard output. The current line is unchanged. (.,.)# Begins a comment; the rest of the line, up to a newline, is ignored. If a line address followed by a semicolon is given, then the current address is set to that address. Otherwise, the current address is unchanged. ($)= Prints the line number of the addressed line. (.+1)newline Prints the addressed line, and sets the current address to that line. FFIILLEESS /tmp/ed.* Buffer file ed.hup The file to which eedd attempts to write the buffer if the terminal hangs up. SSEEEE AALLSSOO _v_i(1), _s_e_d(1), _r_e_g_e_x(3), _s_h(1). USD:12-13 B. W. Kernighan and P. J. Plauger, _S_o_f_t_w_a_r_e _T_o_o_l_s _i_n _P_a_s_c_a_l _, Addison- Wesley, 1981. LLIIMMIITTAATTIIOONNSS eedd processes _f_i_l_e arguments for backslash escapes, i.e., in a file- name, any characters preceded by a backslash (\) are interpreted liter- ally. If a text (non-binary) file is not terminated by a newline character, then eedd appends one on reading/writing it. In the case of a binary file, eedd does not append a newline on reading/writing. per line overhead: 4 ints DDIIAAGGNNOOSSTTIICCSS When an error occurs, if eedd's input is from a regular file or here doc- ument, then it exits, otherwise it prints a `?' and returns to command mode. An explanation of the last error can be printed with the _`_h_' (help) command. Attempting to quit eedd or edit another file before writing a modified buffer results in an error. If the command is entered a second time, it succeeds, but any changes to the buffer are lost. eedd exits with 0 if no errors occurred; otherwise >0. 10 November 1994 ED(1)