1 .\" $NetBSD: ed.1,v 1.28 2003/09/08 13:13:08 wiz Exp $
2 .\" $OpenBSD: ed.1,v 1.42 2003/07/27 13:25:43 jmc Exp $
4 .\" Copyright (c) 1993 Andrew Moore, Talke Studio.
5 .\" All rights reserved.
7 .\" Redistribution and use in source and binary forms, with or without
8 .\" modification, are permitted provided that the following conditions
10 .\" 1. Redistributions of source code must retain the above copyright
11 .\" notice, this list of conditions and the following disclaimer.
12 .\" 2. Redistributions in binary form must reproduce the above copyright
13 .\" notice, this list of conditions and the following disclaimer in the
14 .\" documentation and/or other materials provided with the distribution.
16 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 is a line-oriented text editor.
43 It is used to create, display, modify, and otherwise manipulate text files.
46 argument, then a copy of
48 is read into the editor's buffer.
49 Changes are made to this copy and not directly to
54 any changes not explicitly saved with a
58 Editing is done in two distinct modes:
65 In this mode, commands are read from the standard input and
66 executed to manipulate the contents of the editor buffer.
68 A typical command might look like:
69 .Bd -literal -offset indent
73 which replaces all occurrences of the string
78 When an input command, such as
88 This is the primary means of adding text to a file.
89 In this mode, no commands are available;
90 instead, the standard input is written directly to the editor buffer.
91 Lines consist of text up to and including a newline character.
92 Input mode is terminated by entering a single period
98 commands operate on whole lines or ranges of lines; e.g.,
101 command deletes lines; the
103 command moves lines, and so on.
104 It is possible to modify only a portion of a line by means of replacement,
105 as in the example above.
106 However, even here, the
108 command is applied to whole lines at a time.
112 commands consist of zero or more line addresses, followed by a single
113 character command and possibly additional parameters; i.e.,
114 commands have the structure:
115 .Bd -literal -offset indent
116 [address [,address]]command[parameters]
119 The address(es) indicate the line or range of lines to be affected by the
121 If fewer addresses are given than the command accepts, then
122 default addresses are supplied.
124 The options are as follows:
131 Enables the use of extended regular expressions instead of the basic
132 regular expressions that are normally used.
134 Specifies a command prompt.
135 This may be toggled on and off with the
139 Suppress diagnostics.
140 This should be used if
142 standard input is from a script.
144 Prompt for an encryption key to be used in subsequent reads and writes
149 Specifies the name of a file to read.
155 then it is interpreted as a shell command.
156 In this case, what is read is the standard output of
160 To read a file whose name begins with a bang, prefix the
161 name with a backslash
163 The default filename is set to
165 only if it is not prefixed with a bang.
168 An address represents the number of a line in the buffer.
172 which is typically supplied to commands as the default address
173 when none is specified.
174 When a file is first read, the current address is set to the last line
176 In general, the current address is set to the last line affected by a command.
179 constructed from one of the bases in the list below, optionally followed
181 The offset may include any combination of digits, operators (i.e.,
187 Addresses are read from left to right, and their values are computed
188 relative to the current address.
190 One exception to the rule that addresses represent line numbers is the
195 .Dq before the first line ,
196 and is legal wherever it makes sense.
198 An address range is two addresses separated either by a comma or semi-colon.
199 The value of the first address in a range cannot exceed the
201 If only one address is given in a range,
202 then the second address is set to the given address.
205 of addresses is given where
207 then the corresponding range is determined by the last two addresses in the
209 If only one address is expected, then the last address is used.
211 Each address in a comma-delimited range is interpreted relative to the
213 In a semi-colon-delimited range, the first address is
214 used to set the current address, and the second address is interpreted
215 relative to the first.
217 The following address symbols are recognized:
220 The current line (address) in the buffer.
222 The last line in the buffer.
226 line in the buffer where
228 is a number in the range
232 This is equivalent to
234 and may be repeated with cumulative effect.
235 .It Em -n No or Em ^n
240 is a non-negative number.
243 This is equivalent to
245 and may be repeated with cumulative effect.
251 is a non-negative number.
252 .It Em whitespace Em n
258 .It Em \&, No or Em %
259 The first through last lines in the buffer.
260 This is equivalent to the address range
263 The current through last lines in the buffer.
264 This is equivalent to the address range
266 .It Em / Ns Ar re Ns Em /
267 The 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.
272 repeats the last search.
273 .It Em ? Ns Ar re Ns Em ?
274 The previous line containing the regular expression
276 The search wraps to the end of the buffer and continues up to the
277 current line, if necessary.
279 repeats the last search.
281 The line previously marked by a
283 (mark) command, where
285 is a lower case letter.
287 .Ss REGULAR EXPRESSIONS
288 Regular expressions are patterns used in selecting text.
292 .Bd -literal -offset indent
296 prints all lines containing
298 Regular expressions are also used by the
300 command for selecting old text to be replaced with new.
302 In addition to specifying string literals, regular expressions can
303 represent classes of strings.
304 Strings thus represented are said to be matched by the
305 corresponding regular expression.
306 If it is possible for a regular expression to match several strings in
307 a line, then the leftmost longest match is the one selected.
309 The following symbols are used in constructing regular expressions:
310 .Bl -tag -width Dsasdfsd
314 not listed below, including
324 Any backslash-escaped character
336 Matches any single character.
338 Matches any single character in the character class
341 .Sx CHARACTER CLASSES
342 below for further information.
344 Matches any single character, other than newline, not in the
350 is the first character of a regular expression, then it
351 anchors the regular expression to the beginning of a line.
352 Otherwise, it matches itself.
356 is the last character of a regular expression,
357 it anchors the regular expression to the end of a line.
358 Otherwise, it matches itself.
360 Anchors the single character regular expression or subexpression
361 immediately following it to the beginning of a word.
362 (This may not be available.)
364 Anchors the single character regular expression or subexpression
365 immediately following it to the end of a word.
366 (This may not be available.)
367 .It Em \e( Ns Ar re Ns Em \e)
368 Defines a subexpression
370 Subexpressions may be nested.
371 A subsequent backreference of the form
375 is a number in the range [1,9], expands to the text matched by the
378 For example, the regular expression
380 matches any string consisting of identical adjacent substrings.
381 Subexpressions are ordered relative to their left delimiter.
383 Matches the single character regular expression or subexpression
384 immediately preceding it zero or more times.
387 is the first character of a regular expression or subexpression,
388 then it matches itself.
391 operator sometimes yields unexpected results.
392 For example, the regular expression
394 matches the beginning of the string
396 (as opposed to the substring
398 since a null match is the only leftmost match.
400 .It Em \e{ No n,m Em \e}\ \e{ No n, Em \e}\ \& Em \e{ No n Em \e}
402 Matches the single character regular expression or subexpression
403 immediately preceding it at least
410 is omitted, then it matches at least
413 If the comma is also omitted, then it matches exactly
418 Additional regular expression operators may be defined depending on the
422 .Ss CHARACTER CLASSES
423 A character class specifies a set of characters. It is written within
426 and in its most basic form contains just the characters in the set.
430 in a character class, it must be the first character.
431 A range of characters may be specified by separating the end characters
436 specifies the lower case characters.
438 The following literals can also be used within character classes as
439 shorthand for particular sets of characters:
440 .Bl -tag -offset indent -compact -width [:blahblah:]
442 Alphanumeric characters.
446 Lowercase alphabetic characters.
448 Whitespace (space, tab, newline, form feed, etc.)
450 Alphabetic characters.
452 Numeric characters (digits).
454 Printable characters.
456 Uppercase alphabetic characters.
458 Blank characters (space and tab).
460 Graphical characters (printing nonblank characters).
462 Punctuation characters.
468 appears as the first or last character of a character class, then
470 All other characters in a character class match themselves.
481 .Em collating element
482 are interpreted according to
484 (not currently supported).
487 for an explanation of these constructs.
491 commands are single characters, though some require additional parameters.
492 If a command's parameters extend over several lines, then
493 each line except for the last must be terminated with a backslash
496 In general, at most one command is allowed per line.
497 However, most commands accept a print suffix, which is any of
504 (enumerate), to print the last line affected by the command.
506 An interrupt (typically ^C) has the effect of aborting the current command
507 and returning the editor to command mode.
510 recognizes the following commands.
511 The commands are shown together with
512 the default address or address range supplied if none is
513 specified (in parentheses), and other possible arguments on the right.
516 Appends text to the buffer after the addressed line.
517 Text is entered in input mode.
518 The current address is set to last line entered.
520 Changes lines in the buffer.
521 The addressed lines are deleted from the buffer,
522 and text is appended in their place.
523 Text is entered in input mode.
524 The current address is set to last line entered.
526 Deletes the addressed lines from the buffer.
527 If there is a line after the deleted range, then the current address is set
529 Otherwise the current address is set to the line before the deleted range.
533 and sets the default filename.
536 is not specified, then the default filename is used.
537 Any lines in the buffer are deleted before the new file is read.
538 The current address is set to the last line read.
540 Edits the standard output of
545 The default filename is unchanged.
546 Any lines in the buffer are deleted before the output of
549 The current address is set to the last line read.
554 This is similar to the
556 command, except that unwritten changes are discarded without warning.
557 The current address is set to the last line read.
559 Sets the default filename to
563 is not specified, then the default unescaped filename is printed.
564 .It (1,$) Ns Ic g Ns Ar /re/command-list
567 to each of the addressed lines matching a regular expression
569 The current address is set to the line currently matched before
574 command, the current address is set to the last line affected by
579 must be on a separate line,
580 and every line except for the last must be terminated by a backslash
582 Any commands are allowed, except for
593 .It (1,$) Ns Ic G Ns Ar /re/
594 Interactively edits the addressed lines matching a regular expression
596 For each matching line, the line is printed, the current address is set,
597 and the user is prompted to enter a
601 command, the current address is set to the last line affected by (the last)
606 is the same as that of the
609 A newline alone acts as a null command list.
612 repeats the last non-null command list.
614 Toggles the printing of error explanations.
615 By default, explanations are not printed.
616 It is recommended that
618 scripts begin with this command to aid in debugging.
620 Prints an explanation of the last error.
622 Inserts text in the buffer before the current line.
623 Text is entered in input mode.
624 The current address is set to the last line entered.
626 Joins the addressed lines.
627 The addressed lines are deleted from the buffer and replaced by a single
628 line containing their joined text.
629 The current address is set to the resultant line.
630 .It (.) Ns Ic k Ns Ar lc
631 Marks a line with a lower case letter
633 The line can then be addressed as
635 (i.e., a single quote followed by
637 in subsequent commands.
638 The mark is not cleared until the line is deleted or otherwise modified.
640 Prints the addressed lines unambiguously.
641 If a single line fills more than one screen (as might be the case
642 when viewing a binary file, for instance), a
644 prompt is printed on the last line.
646 waits until the RETURN key is pressed before displaying the next screen.
647 The current address is set to the last line printed.
648 .It (.,.) Ns Ic m Ns No (.)
649 Moves lines in the buffer.
650 The addressed lines are moved to after the
651 right-hand destination address, which may be the address
654 The current address is set to the last line moved.
656 Prints the addressed lines along with their line numbers.
657 The current address is set to the last line printed.
659 Prints the addressed lines.
660 The current address is set to the last line printed.
662 Toggles the command prompt on and off.
663 Unless a prompt was specified with the command-line option
665 the command prompt is by default turned off.
673 This is similar to the
675 command, except that unwritten changes are discarded without warning.
676 .It ($) Ns Ic r Ar file
679 to after the addressed line.
682 is not specified, then the default filename is used.
683 If there was no default filename prior to the command,
684 then the default filename is set to
686 Otherwise, the default filename is unchanged.
687 The current address is set to the last line read.
688 .It ($) Ns Ic r Ar !command
689 Reads to after the addressed line the standard output of
694 The default filename is unchanged.
695 The current address is set to the last line read.
697 .It (.,.) Ic s Ar /re/replacement/ , \ (.,.) \
698 Ic s Ar /re/replacement/ Em g , Ar \ (.,.) \
699 Ic s Ar /re/replacement/ Em n
701 Replaces text in the addressed lines matching a regular expression
705 By default, only the first match in each line is replaced.
708 (global) suffix is given, then every match to be replaced.
713 is a positive number, causes only the
715 match to be replaced.
716 It is an error if no substitutions are performed on any of the addressed
718 The current address is set the last line affected.
723 may be delimited by any character other than space and newline
727 If one or two of the last delimiters is omitted, then the last line
728 affected is printed as though the print suffix
736 is replaced by the currently matched text.
737 The character sequence
741 is a number in the range [1,9], is replaced by the
743 backreference expression of the matched text.
750 from the last substitution is used.
751 Newlines may be embedded in
753 if they are escaped with a backslash
756 Repeats the last substitution.
759 command accepts a count suffix
761 or any combination of the characters
768 is given, then only the
774 the regular expression of the last search to be used instead of
775 that of the last substitution.
778 suffix toggles the global suffix of the last substitution.
781 suffix toggles the print suffix of the last substitution.
782 The current address is set to the last line affected.
783 .It (.,.) Ns Ic t Ns No (.)
784 Copies (i.e., transfers) the addressed lines to after the right-hand
785 destination address, which may be the address
788 The current address is set to the last line copied.
790 Undoes the last command and restores the current address
791 to what it was before the command.
798 are treated as a single command by undo.
801 .It (1,$) Ns Ic v Ns Ar /re/command-list
804 to each of the addressed lines not matching a regular expression
806 This is similar to the
809 .It (1,$) Ns Ic V Ns Ar /re/
810 Interactively edits the addressed lines not matching a regular expression
812 This is similar to the
815 .It (1,$) Ns Ic w Ar file
816 Writes the addressed lines to
818 Any previous contents of
820 are lost without warning.
821 If there is no default filename, then the default filename is set to
823 otherwise it is unchanged.
824 If no filename is specified, then the default filename is used.
825 The current address is unchanged.
826 .It (1,$) Ns Ic wq Ar file
827 Writes the addressed lines to
832 .It (1,$) Ns Ic w Ar !command
833 Writes the addressed lines to the standard input of
838 The default filename and current address are unchanged.
839 .It (1,$) Ns Ic W Ar file
840 Appends the addressed lines to the end of
842 This is similar to the
844 command, except that the previous contents of file are not clobbered.
845 The current address is unchanged.
847 Prompts for an encryption key which is used in subsequent reads and writes.
848 If a newline alone is entered as the key, then encryption is turned off.
849 Otherwise, echoing is disabled while a key is read.
850 Encryption/decryption is done using the
853 .It (.+1) Ns Ic z Ns Ar n
856 lines at a time starting at addressed line.
859 is not specified, then the current window size is used.
860 The current address is set to the last line printed.
862 Prints the line number of the addressed line.
863 .It (.+1) Ns Ic newline
864 Prints the addressed line, and sets the current address to that line.
865 .It Ic \&! Ns Ar command
870 If the first character of
874 then it is replaced by text of the previous
882 However, an unescaped
884 is replaced by the default filename.
885 When the shell returns from execution, a
887 is printed to the standard output.
888 The current line is unchanged.
894 arguments for backslash escapes, i.e., in a filename,
895 any characters preceded by a backslash
897 are interpreted literally.
899 If a text (non-binary) file is not terminated by a newline character,
902 appends one on reading/writing it.
903 In the case of a binary file,
905 does not append a newline on reading/writing.
907 .Bl -tag -width iTMPDIR
909 The location used to store temporary files.
912 .Bl -tag -width /tmp/ed.* -compact
918 attempts to write the buffer if the terminal hangs up
921 When an error occurs,
925 and either returns to command mode or exits if its input is from a script.
926 An explanation of the last error can be printed with the
932 (global) command masks any errors from failed searches and substitutions,
933 it can be used to perform conditional operations in scripts; e.g.,
934 .Bd -literal -offset indent
938 replaces any occurrences of
945 (undo) command occurs in a global command list, then
946 the command list is executed only once.
948 If diagnostics are not disabled, attempting to quit
950 or edit another file before writing a modified buffer results in an error.
951 If the command is entered a second time, it succeeds,
952 but any changes to the buffer are lost.
964 .%B Software Tools in Pascal