1 .\" $NetBSD: ed.1,v 1.30.40.1 2018/04/08 06:04:08 snj 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 Disables using of the
136 command (execuring a subshell).
137 Intended to be used by batch jobs like
140 Specifies a command prompt.
141 This may be toggled on and off with the
145 Suppress diagnostics.
146 This should be used if
148 standard input is from a script.
150 Prompt for an encryption key to be used in subsequent reads and writes
155 Specifies the name of a file to read.
161 then it is interpreted as a shell command.
162 In this case, what is read is the standard output of
166 To read a file whose name begins with a bang, prefix the
167 name with a backslash
169 The default filename is set to
171 only if it is not prefixed with a bang.
174 An address represents the number of a line in the buffer.
178 which is typically supplied to commands as the default address
179 when none is specified.
180 When a file is first read, the current address is set to the last line
182 In general, the current address is set to the last line affected by a command.
185 constructed from one of the bases in the list below, optionally followed
187 The offset may include any combination of digits, operators (i.e.,
193 Addresses are read from left to right, and their values are computed
194 relative to the current address.
196 One exception to the rule that addresses represent line numbers is the
201 .Dq before the first line ,
202 and is legal wherever it makes sense.
204 An address range is two addresses separated either by a comma or semi-colon.
205 The value of the first address in a range cannot exceed the
207 If only one address is given in a range,
208 then the second address is set to the given address.
211 of addresses is given where
213 then the corresponding range is determined by the last two addresses in the
215 If only one address is expected, then the last address is used.
217 Each address in a comma-delimited range is interpreted relative to the
219 In a semi-colon-delimited range, the first address is
220 used to set the current address, and the second address is interpreted
221 relative to the first.
223 The following address symbols are recognized:
226 The current line (address) in the buffer.
228 The last line in the buffer.
232 line in the buffer where
234 is a number in the range
238 This is equivalent to
240 and may be repeated with cumulative effect.
241 .It Em -n No or Em ^n
246 is a non-negative number.
249 This is equivalent to
251 and may be repeated with cumulative effect.
257 is a non-negative number.
258 .It Em whitespace Em n
264 .It Em \&, No or Em %
265 The first through last lines in the buffer.
266 This is equivalent to the address range
269 The current through last lines in the buffer.
270 This is equivalent to the address range
272 .It Em / Ns Ar re Ns Em /
273 The next line containing the regular expression
275 The search wraps to the beginning of the buffer and continues down to the
276 current line, if necessary.
278 repeats the last search.
279 .It Em \&? Ns Ar re Ns Em \&?
280 The previous line containing the regular expression
282 The search wraps to the end of the buffer and continues up to the
283 current line, if necessary.
285 repeats the last search.
287 The line previously marked by a
289 (mark) command, where
291 is a lower case letter.
293 .Ss REGULAR EXPRESSIONS
294 Regular expressions are patterns used in selecting text.
298 .Bd -literal -offset indent
302 prints all lines containing
304 Regular expressions are also used by the
306 command for selecting old text to be replaced with new.
308 In addition to specifying string literals, regular expressions can
309 represent classes of strings.
310 Strings thus represented are said to be matched by the
311 corresponding regular expression.
312 If it is possible for a regular expression to match several strings in
313 a line, then the leftmost longest match is the one selected.
315 The following symbols are used in constructing regular expressions:
316 .Bl -tag -width Dsasdfsd
320 not listed below, including
330 Any backslash-escaped character
342 Matches any single character.
344 Matches any single character in the character class
347 .Sx CHARACTER CLASSES
348 below for further information.
350 Matches any single character, other than newline, not in the
356 is the first character of a regular expression, then it
357 anchors the regular expression to the beginning of a line.
358 Otherwise, it matches itself.
362 is the last character of a regular expression,
363 it anchors the regular expression to the end of a line.
364 Otherwise, it matches itself.
366 Anchors the single character regular expression or subexpression
367 immediately following it to the beginning of a word.
368 (This may not be available.)
370 Anchors the single character regular expression or subexpression
371 immediately following it to the end of a word.
372 (This may not be available.)
373 .It Em \e( Ns Ar re Ns Em \e)
374 Defines a subexpression
376 Subexpressions may be nested.
377 A subsequent backreference of the form
381 is a number in the range [1,9], expands to the text matched by the
384 For example, the regular expression
386 matches any string consisting of identical adjacent substrings.
387 Subexpressions are ordered relative to their left delimiter.
389 Matches the single character regular expression or subexpression
390 immediately preceding it zero or more times.
393 is the first character of a regular expression or subexpression,
394 then it matches itself.
397 operator sometimes yields unexpected results.
398 For example, the regular expression
400 matches the beginning of the string
402 (as opposed to the substring
404 since a null match is the only leftmost match.
406 .It Em \e{ No n,m Em \e}\ \e{ No n, Em \e}\ \& Em \e{ No n Em \e}
408 Matches the single character regular expression or subexpression
409 immediately preceding it at least
416 is omitted, then it matches at least
419 If the comma is also omitted, then it matches exactly
424 Additional regular expression operators may be defined depending on the
428 .Ss CHARACTER CLASSES
429 A character class specifies a set of characters. It is written within
432 and in its most basic form contains just the characters in the set.
436 in a character class, it must be the first character.
437 A range of characters may be specified by separating the end characters
442 specifies the lower case characters.
444 The following literals can also be used within character classes as
445 shorthand for particular sets of characters:
446 .Bl -tag -offset indent -compact -width [:blahblah:]
448 Alphanumeric characters.
452 Lowercase alphabetic characters.
454 Whitespace (space, tab, newline, form feed, etc.)
456 Alphabetic characters.
458 Numeric characters (digits).
460 Printable characters.
462 Uppercase alphabetic characters.
464 Blank characters (space and tab).
466 Graphical characters (printing nonblank characters).
468 Punctuation characters.
474 appears as the first or last character of a character class, then
476 All other characters in a character class match themselves.
487 .Em collating element
488 are interpreted according to
490 (not currently supported).
493 for an explanation of these constructs.
497 commands are single characters, though some require additional parameters.
498 If a command's parameters extend over several lines, then
499 each line except for the last must be terminated with a backslash
502 In general, at most one command is allowed per line.
503 However, most commands accept a print suffix, which is any of
510 (enumerate), to print the last line affected by the command.
512 An interrupt (typically ^C) has the effect of aborting the current command
513 and returning the editor to command mode.
516 recognizes the following commands.
517 The commands are shown together with
518 the default address or address range supplied if none is
519 specified (in parentheses), and other possible arguments on the right.
522 Appends text to the buffer after the addressed line.
523 Text is entered in input mode.
524 The current address is set to last line entered.
526 Changes lines in the buffer.
527 The addressed lines are deleted from the buffer,
528 and text is appended in their place.
529 Text is entered in input mode.
530 The current address is set to last line entered.
532 Deletes the addressed lines from the buffer.
533 If there is a line after the deleted range, then the current address is set
535 Otherwise the current address is set to the line before the deleted range.
539 and sets the default filename.
542 is not specified, then the default filename is used.
543 Any lines in the buffer are deleted before the new file is read.
544 The current address is set to the last line read.
546 Edits the standard output of
551 The default filename is unchanged.
552 Any lines in the buffer are deleted before the output of
555 The current address is set to the last line read.
560 This is similar to the
562 command, except that unwritten changes are discarded without warning.
563 The current address is set to the last line read.
565 Sets the default filename to
569 is not specified, then the default unescaped filename is printed.
570 .It (1,$) Ns Ic g Ns Ar /re/command-list
573 to each of the addressed lines matching a regular expression
575 The current address is set to the line currently matched before
580 command, the current address is set to the last line affected by
585 must be on a separate line,
586 and every line except for the last must be terminated by a backslash
588 Any commands are allowed, except for
599 .It (1,$) Ns Ic G Ns Ar /re/
600 Interactively edits the addressed lines matching a regular expression
602 For each matching line, the line is printed, the current address is set,
603 and the user is prompted to enter a
607 command, the current address is set to the last line affected by (the last)
612 is the same as that of the
615 A newline alone acts as a null command list.
618 repeats the last non-null command list.
620 Toggles the printing of error explanations.
621 By default, explanations are not printed.
622 It is recommended that
624 scripts begin with this command to aid in debugging.
626 Prints an explanation of the last error.
628 Inserts text in the buffer before the current line.
629 Text is entered in input mode.
630 The current address is set to the last line entered.
632 Joins the addressed lines.
633 The addressed lines are deleted from the buffer and replaced by a single
634 line containing their joined text.
635 The current address is set to the resultant line.
636 .It (.) Ns Ic k Ns Ar lc
637 Marks a line with a lower case letter
639 The line can then be addressed as
641 (i.e., a single quote followed by
643 in subsequent commands.
644 The mark is not cleared until the line is deleted or otherwise modified.
646 Prints the addressed lines unambiguously.
647 If a single line fills more than one screen (as might be the case
648 when viewing a binary file, for instance), a
650 prompt is printed on the last line.
652 waits until the RETURN key is pressed before displaying the next screen.
653 The current address is set to the last line printed.
654 .It (.,.) Ns Ic m Ns No (.)
655 Moves lines in the buffer.
656 The addressed lines are moved to after the
657 right-hand destination address, which may be the address
660 The current address is set to the last line moved.
662 Prints the addressed lines along with their line numbers.
663 The current address is set to the last line printed.
665 Prints the addressed lines.
666 The current address is set to the last line printed.
668 Toggles the command prompt on and off.
669 Unless a prompt was specified with the command-line option
671 the command prompt is by default turned off.
679 This is similar to the
681 command, except that unwritten changes are discarded without warning.
682 .It ($) Ns Ic r Ar file
685 to after the addressed line.
688 is not specified, then the default filename is used.
689 If there was no default filename prior to the command,
690 then the default filename is set to
692 Otherwise, the default filename is unchanged.
693 The current address is set to the last line read.
694 .It ($) Ns Ic r Ar !command
695 Reads to after the addressed line the standard output of
700 The default filename is unchanged.
701 The current address is set to the last line read.
703 .It (.,.) Ic s Ar /re/replacement/ , \ (.,.) \
704 Ic s Ar /re/replacement/ Em g , Ar \ (.,.) \
705 Ic s Ar /re/replacement/ Em n
707 Replaces text in the addressed lines matching a regular expression
711 By default, only the first match in each line is replaced.
714 (global) suffix is given, then every match to be replaced.
719 is a positive number, causes only the
721 match to be replaced.
722 It is an error if no substitutions are performed on any of the addressed
724 The current address is set the last line affected.
729 may be delimited by any character other than space and newline
733 If one or two of the last delimiters is omitted, then the last line
734 affected is printed as though the print suffix
742 is replaced by the currently matched text.
743 The character sequence
747 is a number in the range [1,9], is replaced by the
749 backreference expression of the matched text.
756 from the last substitution is used.
757 Newlines may be embedded in
759 if they are escaped with a backslash
762 Repeats the last substitution.
765 command accepts a count suffix
767 or any combination of the characters
774 is given, then only the
780 the regular expression of the last search to be used instead of
781 that of the last substitution.
784 suffix toggles the global suffix of the last substitution.
787 suffix toggles the print suffix of the last substitution.
788 The current address is set to the last line affected.
789 .It (.,.) Ns Ic t Ns No (.)
790 Copies (i.e., transfers) the addressed lines to after the right-hand
791 destination address, which may be the address
794 The current address is set to the last line copied.
796 Undoes the last command and restores the current address
797 to what it was before the command.
804 are treated as a single command by undo.
807 .It (1,$) Ns Ic v Ns Ar /re/command-list
810 to each of the addressed lines not matching a regular expression
812 This is similar to the
815 .It (1,$) Ns Ic V Ns Ar /re/
816 Interactively edits the addressed lines not matching a regular expression
818 This is similar to the
821 .It (1,$) Ns Ic w Ar file
822 Writes the addressed lines to
824 Any previous contents of
826 are lost without warning.
827 If there is no default filename, then the default filename is set to
829 otherwise it is unchanged.
830 If no filename is specified, then the default filename is used.
831 The current address is unchanged.
832 .It (1,$) Ns Ic wq Ar file
833 Writes the addressed lines to
838 .It (1,$) Ns Ic w Ar !command
839 Writes the addressed lines to the standard input of
844 The default filename and current address are unchanged.
845 .It (1,$) Ns Ic W Ar file
846 Appends the addressed lines to the end of
848 This is similar to the
850 command, except that the previous contents of file are not clobbered.
851 The current address is unchanged.
853 Prompts for an encryption key which is used in subsequent reads and writes.
854 If a newline alone is entered as the key, then encryption is turned off.
855 Otherwise, echoing is disabled while a key is read.
856 Encryption/decryption is done using the
859 .It (.+1) Ns Ic z Ns Ar n
862 lines at a time starting at addressed line.
865 is not specified, then the current window size is used.
866 The current address is set to the last line printed.
868 Prints the line number of the addressed line.
869 .It (.+1) Ns Ic newline
870 Prints the addressed line, and sets the current address to that line.
871 .It Ic \&! Ns Ar command
876 If the first character of
880 then it is replaced by text of the previous
888 However, an unescaped
890 is replaced by the default filename.
891 When the shell returns from execution, a
893 is printed to the standard output.
894 The current line is unchanged.
900 arguments for backslash escapes, i.e., in a filename,
901 any characters preceded by a backslash
903 are interpreted literally.
905 If a text (non-binary) file is not terminated by a newline character,
908 appends one on reading/writing it.
909 In the case of a binary file,
911 does not append a newline on reading/writing.
913 .Bl -tag -width iTMPDIR
915 The location used to store temporary files.
918 .Bl -tag -width /tmp/ed.* -compact
924 attempts to write the buffer if the terminal hangs up
927 When an error occurs,
931 and either returns to command mode or exits if its input is from a script.
932 An explanation of the last error can be printed with the
938 (global) command masks any errors from failed searches and substitutions,
939 it can be used to perform conditional operations in scripts; e.g.,
940 .Bd -literal -offset indent
944 replaces any occurrences of
951 (undo) command occurs in a global command list, then
952 the command list is executed only once.
954 If diagnostics are not disabled, attempting to quit
956 or edit another file before writing a modified buffer results in an error.
957 If the command is entered a second time, it succeeds,
958 but any changes to the buffer are lost.
971 .%B Software Tools in Pascal