1 .TH ED 1 "10 November 1994"
5 ed [-] [-Gs] [-p \fIstring\fR] [\fIfile\fR]
7 red [-] [-Gs] [-p \fIstring\fR] [\fIfile\fR]
10 is a line-oriented text editor.
11 It is used to create, display, modify and otherwise manipulate text
16 it can only edit files in the current
17 directory and cannot execute shell commands.
21 argument, then a copy of
23 is read into the editor's buffer.
24 Changes are made to this copy and not directly to
29 any changes not explicitly saved with a
33 Editing is done in two distinct modes:
40 In this mode commands are read from the standard input and
41 executed to manipulate the contents of the editor buffer.
42 A typical command might look like:
45 ,s/\fIold\fR/\fInew\fR/g
48 which replaces all occurences of the string
53 When an input command, such as
61 enters input mode. This is the primary means
62 of adding text to a file.
63 In this mode, no commands are available;
64 instead, the standard input is written
65 directly to the editor buffer. Lines consist of text up to and
69 Input mode is terminated by
70 entering a single period (\fI.\fR) on a line.
74 commands operate on whole lines or ranges of lines; e.g.,
77 command deletes lines; the
79 command moves lines, and so on.
80 It is possible to modify only a portion of a line by means of replacement,
81 as in the example above. However even here, the
83 command is applied to whole lines at a time.
87 commands consist of zero or more line addresses, followed by a single
88 character command and possibly additional parameters; i.e.,
89 commands have the structure:
92 .I [address [,address]]command[parameters]
95 The address(es) indicate the line or range of lines to be affected by the
96 command. If fewer addresses are given than the command accepts, then
97 default addresses are supplied.
102 Forces backwards compatibility. Affects the commands
113 Suppresses diagnostics. This should be used if
115 standard input is from a script.
119 Specifies a command prompt. This may be toggled on and off with the
125 Specifies the name of a file to read. If
128 bang (!), then it is interpreted as a shell command. In this case,
130 the standard output of
134 To read a file whose name begins with a bang, prefix the
135 name with a backslash (\e).
136 The default filename is set to
138 only if it is not prefixed with a bang.
141 An address represents the number of a line in the buffer.
146 typically supplied to commands as the default address when none is specified.
147 When a file is first read, the current address is set to the last line
148 of the file. In general, the current address is set to the last line
149 affected by a command.
152 constructed from one of the bases in the list below, optionally followed
153 by a numeric offset. The offset may include any combination
154 of digits, operators (i.e.,
160 Addresses are read from left to right, and their values are computed
161 relative to the current address.
163 One exception to the rule that addresses represent line numbers is the
167 This means "before the first line,"
168 and is legal wherever it makes sense.
170 An address range is two addresses separated either by a comma or
171 semicolon. The value of the first address in a range cannot exceed the
172 value of the the second. If only one address is given in a range, then
173 the second address is set to the given address. If an
175 of addresses is given where
177 then the corresponding range is determined by the last two addresses in
180 If only one address is expected, then the last address is used.
182 Each address in a comma-delimited range is interpreted relative to the
183 current address. In a semicolon-delimited range, the first address is
184 used to set the current address, and the second address is interpreted
185 relative to the first.
188 The following address symbols are recognized.
192 The current line (address) in the buffer.
196 The last line in the buffer.
205 is a number in the range
213 This is equivalent to
215 and may be repeated with cumulative effect.
225 is a non-negative number.
231 This is equivalent to
233 and may be repeated with cumulative effect.
243 is a non-negative number.
254 The first through last lines in the buffer. This is equivalent to
260 The current through last lines in the buffer. This is equivalent to
267 next line containing the regular expression
269 The search wraps to the beginning of the buffer and continues down to the
270 current line, if necessary.
271 // repeats the last search.
276 previous line containing the regular expression
278 The search wraps to the end of the buffer and continues up to the
279 current line, if necessary.
280 ?? repeats the last search.
285 line previously marked by a
287 (mark) command, where
289 is a lower case letter.
291 .SS REGULAR EXPRESSIONS
292 Regular expressions are patterns used in selecting text.
301 prints all lines containing
303 Regular expressions are also
306 command for selecting old text to be replaced with new.
308 In addition to a specifying string literals, regular expressions can
310 classes of strings. Strings thus represented are said to be matched
311 by the corresponding regular expression.
312 If it is possible for a regular expression
313 to match several strings in a line, then the left-most longest match is
316 The following symbols are used in constructing regular expressions:
322 not listed below, including `{', '}', `(', `)', `<' and `>',
327 A backslash-escaped character
329 other than `{', '}', `(', `)', `<', `>',
330 `b', 'B', `w', `W', `+', and `?'
335 Matches any single character.
339 Matches any single character in
344 it must be the first character.
345 A range of characters may be specified by separating the end characters
346 of the range with a `-', e.g., `a-z' specifies the lower case characters.
347 The following literal expressions can also be used in
349 to specify sets of characters:
351 \ \ [:alnum:]\ \ [:cntrl:]\ \ [:lower:]\ \ [:space:]
353 \ \ [:alpha:]\ \ [:digit:]\ \ [:print:]\ \ [:upper:]
355 \ \ [:blank:]\ \ [:graph:]\ \ [:punct:]\ \ [:xdigit:]
357 If `-' appears as the first or last
360 then it matches itself.
361 All other characters in
369 \ \ [.\fIcol-elm\fR.] or,
371 \ \ [=\fIcol-elm\fR=]
377 are interpreted according to
379 (not currently supported).
382 for an explanation of these constructs.
386 Matches any single character, other than newline, not in
394 If `^' is the first character of a regular expression, then it
395 anchors the regular expression to the beginning of a line.
396 Otherwise, it matches itself.
400 If `$' is the last character of a regular expression, it
401 anchors the regular expression to the end of a line.
402 Otherwise, it matches itself.
406 Defines a (possibly null) subexpression
408 Subexpressions may be nested.
409 A subsequent backreference of the form `\e\fIn\fR', where
411 is a number in the range [1,9], expands to the text matched by the
414 For example, the regular expression `\e(a.c\e)\e1' matches the
415 string `abcabc', but not `abcadc'.
416 Subexpressions are ordered relative to
417 their left delimiter.
421 Matches the single character regular expression or subexpression
422 immediately preceding it zero or more times. If '*' is the first
423 character of a regular expression or subexpression, then it matches
424 itself. The `*' operator sometimes yields unexpected results.
425 For example, the regular expression `b*' matches the beginning of
426 the string `abbb', as opposed to the substring `bbb', since a null match
427 is the only left-most match.
430 \fR\e{\fIn,m\fR\e}\fR
435 Matches the single character regular expression or subexpression
436 immediately preceding it at least
443 is omitted, then it matches at least
446 If the comma is also omitted, then it matches exactly
448 times. If any of these forms occurs first in a regular expression or
449 subexpression, then it is interpreted literally (i.e., the regular
450 expression `\e{2\e}' matches the string `{2}', and so on).
456 Anchors the single character regular expression or subexpression
457 immediately following it to the beginning (\e<) or ending
458 (\e>) of a \fIword\fR, i.e., in ASCII, a maximal string of alphanumeric
459 characters, including the underscore (_).
463 The following extended operators are preceded by a backslash (\e) to
464 distinguish them from traditional
472 Unconditionally matches the beginning (\e`) or ending (\e') of a line.
476 Optionally matches the single character regular expression or subexpression
477 immediately preceding it. For example, the regular expression `a[bd]\e?c'
478 matches the strings `abc', `adc' and `ac'. If \e? occurs at the beginning
479 of a regular expressions or subexpression, then it matches a literal `?'.
483 Matches the single character regular expression or subexpression
484 immediately preceding it one or more times. So the regular expression
485 `a+' is shorthand for `aa*'. If \e+ occurs at the beginning of a
486 regular expression or subexpression, then it matches a literal `+'.
491 Matches the beginning or ending (null string) of a word. Thus the regular
492 expression `\ebhello\eb' is equivalent to `\e<hello\e>'. However, `\eb\eb'
493 is a valid regular expression whereas `\e<\e>' is not.
497 Matches (a null string) inside a word.
501 Matches any character in a word.
505 Matches any character not in a word.
510 commands are single characters, though some require additonal parameters.
511 If a command's parameters extend over several lines, then
512 each line except for the last
513 must be terminated with a backslash (\e).
515 In general, at most one command is allowed per line.
516 However, most commands accept a print suffix, which is any of
524 to print the last line affected by the command.
526 An interrupt (typically ^C) has the effect of aborting the current command
527 and returning the editor to command mode.
530 recognizes the following commands. The commands are shown together with
531 the default address or address range supplied if none is
532 specified (in parenthesis).
536 Appends text to the buffer after the addressed line, which may be the
537 address 0 (zero). Text is entered in input mode. The current address is
538 set to last line entered.
542 Changes lines in the buffer. The addressed lines are deleted
543 from the buffer, and text is appended in their place.
544 Text is entered in input mode.
545 The current address is set to last line entered.
549 Deletes the addressed lines from the buffer.
550 If there is a line after the deleted range, then the current address is set
551 to this line. Otherwise the current address is set to the line
552 before the deleted range.
558 and sets the default filename.
561 is not specified, then the default filename is used.
562 Any lines in the buffer are deleted before
563 the new file is read.
564 The current address is set to the last line read.
568 Edits the standard output of
573 The default filename is unchanged.
574 Any lines in the buffer are deleted before the output of
577 The current address is set to the last line read.
584 This is similar to the
587 except that unwritten changes are discarded without warning.
588 The current address is set to the last line read.
592 Sets the default filename to
596 is not specified, then the default unescaped filename is printed.
599 .RI (1,$)g /re/command-list
602 to each of the addressed lines matching a regular expression
604 The current address is set to the
605 line currently matched before
610 command, the current address is set to the last line affected by
615 must be on a separate line,
616 and every line except for the last must be terminated by a backslash
618 Any commands are allowed, except for
632 Interactively edits the addressed lines matching a regular expression
634 For each matching line,
636 the current address is set,
637 and the user is prompted to enter a
641 command, the current address
642 is set to the last line affected by (the last)
647 is the same as that of the
649 command. A newline alone acts as a null command list.
650 A single `&' repeats the last non-null command list.
654 Toggles the printing of error explanations.
655 By default, explanations are not printed.
656 It is recommended that ed scripts begin with this command to
661 Prints an explanation of the last error.
665 Inserts text in the buffer before the current line.
666 Text is entered in input mode.
667 The current address is set to the last line entered.
671 Joins the addressed lines. The addressed lines are
672 deleted from the buffer and replaced by a single
673 line containing their joined text.
674 The current address is set to the resultant line.
678 Marks a line with a lower case letter
680 The line can then be addressed as
682 (i.e., a single quote followed by
684 ) in subsequent commands. The mark is not cleared until the line is
685 deleted or otherwise modified.
689 Prints the addressed lines unambiguously. If invoked from a terminal,
691 pauses at the end of each page until a newline is entered.
692 The current address is set to the last line printed.
696 Moves lines in the buffer. The addressed lines are moved to after the
697 right-hand destination address, which may be the address
700 The current address is set to the
705 Prints the addressed lines along with
706 their line numbers. The current address is set to the last line
711 Prints the addressed lines. If invoked from a terminal,
713 pauses at the end of each page until a newline is entered.
714 The current address is set to the last line
719 Toggles the command prompt on and off.
720 Unless a prompt was specified by with command-line option
721 \fI-p string\fR, the command prompt is by default turned off.
729 Quits ed unconditionally.
730 This is similar to the
733 except that unwritten changes are discarded without warning.
739 to after the addressed line. If
741 is not specified, then the default
742 filename is used. If there was no default filename prior to the command,
743 then the default filename is set to
745 Otherwise, the default filename is unchanged.
746 The current address is set to the last line read.
751 to after the addressed line
752 the standard output of
757 The default filename is unchanged.
758 The current address is set to the last line read.
761 .RI (.,.)s /re/replacement/
763 .RI (.,.)s /re/replacement/\fRg\fR
765 .RI (.,.)s /re/replacement/n
767 Replaces text in the addressed lines
768 matching a regular expression
772 By default, only the first match in each line is replaced.
775 (global) suffix is given, then every match to be replaced.
780 is a postive number, causes only the
782 match to be replaced.
783 It is an error if no substitutions are performed on any of the addressed
785 The current address is set the last line affected.
790 may be delimited by any character other than space and newline
794 If one or two of the last delimiters is omitted, then the last line
795 affected is printed as though the print suffix
802 is replaced by the currently matched text.
803 The character sequence
807 is a number in the range [1,9], is replaced by the
809 backreference expression of the matched text.
812 consists of a single `%', then
814 from the last substitution is used.
815 Newlines may be embedded in
817 if they are escaped with a backslash (\e).
821 Repeats the last substitution.
824 command accepts a count suffix
826 or any combination of the characters
833 is given, then only the
839 the regular expression of the last search to be used instead of the
840 that of the last substitution.
843 suffix toggles the global suffix of the last substitution.
846 suffix toggles the print suffix of the last substitution
847 The current address is set to the last line affected.
851 Copies (i.e., transfers) the addressed lines to after the right-hand
852 destination address, which may be the address
855 The current address is set to the last line
860 Undoes the last command and restores the current address
861 to what it was before the command.
868 are treated as a single command by undo.
873 .RI (1,$)v /re/command-list
876 to each of the addressed lines not matching a regular expression
878 This is similar to the
884 Interactively edits the addressed lines not matching a regular expression
886 This is similar to the
892 Writes the addressed lines to
894 Any previous contents of
896 is lost without warning.
897 If there is no default filename, then the default filename is set to
899 otherwise it is unchanged. If no filename is specified, then the default
901 The current address is unchanged.
905 Writes the addressed lines to
912 .RI (1,$)w \ !command
913 Writes the addressed lines to the standard input of
918 The default filename and current address are unchanged.
922 Appends the addressed lines to the end of
924 This is similar to the
926 command, expect that the previous contents of file is not clobbered.
927 The current address is unchanged.
931 Copies (puts) the contents of the cut buffer to after the addressed line.
932 The current address is set to the last line copied.
936 Copies (yanks) the addressed lines to the cut buffer.
937 The cut buffer is overwritten by subsequent
945 The current address is unchanged.
951 lines at a time starting at addressed line. If
953 is not specified, then the current window size is used.
954 The current address is set to the last line printed.
962 If the first character of
964 is `!', then it is replaced by text of the
970 for backslash (\e) escapes.
971 However, an unescaped
973 is replaced by the default filename.
974 When the shell returns from execution, a `!'
975 is printed to the standard output.
976 The current line is unchanged.
980 Begins a comment; the rest of the line, up to a newline, is ignored.
981 If a line address followed by a semicolon is given, then the
982 current address is set to that address. Otherwise, the current address
987 Prints the line number of the addressed line.
991 Prints the addressed line, and sets the current address to
1003 attempts to write the buffer if the terminal hangs up.
1014 B. W. Kernighan and P. J. Plauger,
1015 .I Software Tools in Pascal ,
1016 Addison-Wesley, 1981.
1022 arguments for backslash escapes, i.e., in a filename,
1023 any characters preceded by a backslash (\e) are
1024 interpreted literally.
1026 If a text (non-binary) file is not terminated by a newline character,
1029 appends one on reading/writing it. In the case of a binary file,
1031 does not append a newline on reading/writing.
1033 per line overhead: 4 ints
1036 When an error occurs,
1039 input is from a regular file or here document, then it
1041 prints a `?' and returns to command mode.
1042 An explanation of the last error can be
1049 or edit another file before writing a modified buffer
1050 results in an error.
1051 If the command is entered a second time, it succeeds,
1052 but any changes to the buffer are lost.
1055 exits with 0 if no errors occurred; otherwise >0.