xref: /openbsd/regress/usr.bin/diff/t11.2 (revision 771fbea0)
1.\"	$OpenBSD: t11.2,v 1.1 2003/07/21 20:16:21 otto Exp $
2.\"
3.Dd May 2, 1993
4.Dt ED 1
5.Os
6.Sh NAME
7.Nm ed
8.Nd text editor
9.Sh SYNOPSIS
10.Nm ed
11.Op Fl
12.Op Fl sx
13.Op Fl p Ar string
14.Op Ar file
15.Sh DESCRIPTION
16.Nm
17is a line-oriented text editor.
18It is used to create, display, modify, and otherwise manipulate text files.
19If invoked with a
20.Ar file
21argument, then a copy of
22.Ar file
23is read into the editor's buffer.
24Changes are made to this copy and not directly to
25.Ar file
26itself.
27Upon quitting
28.Nm ed ,
29any changes not explicitly saved with a
30.Em w
31command are lost.
32.Pp
33Editing is done in two distinct modes:
34.Em command
35and
36.Em input .
37When first invoked,
38.Nm
39is in command mode.
40In this mode, commands are read from the standard input and
41executed to manipulate the contents of the editor buffer.
42.Pp
43A typical command might look like:
44.Bd -literal -offset indent
45,s/old/new/g
46.Ed
47.Pp
48which replaces all occurrences of the string
49.Pa old
50with
51.Pa new .
52.Pp
53When an input command, such as
54.Em a
55(append),
56.Em i
57(insert),
58or
59.Em c
60(change) is given,
61.Nm
62enters input mode.
63This is the primary means of adding text to a file.
64In this mode, no commands are available;
65instead, the standard input is written directory to the editor buffer.
66Lines consist of text up to and including a newline character.
67Input mode is terminated by entering a single period
68.Pq Ql \&.
69on a line.
70.Pp
71All
72.Nm
73commands operate on whole lines or ranges of lines; e.g.,
74the
75.Em d
76command deletes lines; the
77.Em m
78command moves lines, and so on.
79It is possible to modify only a portion of a line by means of replacement,
80as in the example above.
81However, even here, the
82.Em s
83command is applied to whole lines at a time.
84.Pp
85In general,
86.Nm
87commands consist of zero or more line addresses, followed by a single
88character command and possibly additional parameters; i.e.,
89commands have the structure:
90.Bd -literal -offset indent
91[address [,address]]command[parameters]
92.Ed
93.Pp
94The address(es) indicate the line or range of lines to be affected by the
95command.
96If fewer addresses are given than the command accepts, then
97default addresses are supplied.
98.Pp
99The options are as follows:
100.Bl -tag -width Ds
101.It Fl
102Same as the
103.Fl s
104option (deprecated).
105.It Fl s
106Suppress diagnostics.
107This should be used if
108.Nm
109standard input is from a script.
110.Fl s
111flag.
112.It Fl x
113Prompt for an encryption key to be used in subsequent reads and writes
114(see the
115.Em x
116command).
117.It Fl p Ar string
118Specifies a command prompt.
119This may be toggled on and off with the
120.Em P
121command.
122.It Ar file
123Specifies the name of a file to read.
124If
125.Ar file
126is prefixed with a
127bang
128.Pq Ql \&! ,
129then it is interpreted as a shell command.
130In this case, what is read is the standard output of
131.Ar file
132executed via
133.Xr sh 1 .
134To read a file whose name begins with a bang, prefix the
135name with a backslash
136.Pq Ql \e .
137The default filename is set to
138.Ar file
139only if it is not prefixed with a bang.
140.El
141.Ss LINE ADDRESSING
142An address represents the number of a line in the buffer.
143.Nm
144maintains a
145.Em current address
146which is typically supplied to commands as the default address
147when none is specified.
148When a file is first read, the current address is set to the last line
149of the file.
150In general, the current address is set to the last line affected by a command.
151.Pp
152A line address is
153constructed from one of the bases in the list below, optionally followed
154by a numeric offset.
155The offset may include any combination of digits, operators (i.e.,
156.Em + ,
157.Em - ,
158and
159.Em ^ ) ,
160and whitespace.
161Addresses are read from left to right, and their values are computed
162relative to the current address.
163.Pp
164One exception to the rule that addresses represent line numbers is the
165address
166.Em 0
167(zero).
168This means
169.Dq before the first line ,
170and is legal wherever it makes sense.
171.Pp
172An address range is two addresses separated either by a comma or semi-colon.
173The value of the first address in a range cannot exceed the
174value of the second.
175If only one address is given in a range,
176then the second address is set to the given address.
177If an
178.Em n Ns No -tuple
179of addresses is given where
180.Em n > 2 ,
181then the corresponding range is determined by the last two addresses in the
182.Em n Ns No -tuple.
183If only one address is expected, then the last address is used.
184.Pp
185Each address in a comma-delimited range is interpreted relative to the
186current address.
187In a semi-colon-delimited range, the first address is
188used to set the current address, and the second address is interpreted
189relative to the first.
190.Pp
191The following address symbols are recognized:
192.Bl -tag -width Ds
193.It Em \&.
194The current line (address) in the buffer.
195.It Em $
196The last line in the buffer.
197.It Em n
198The
199.Em n Ns No th
200line in the buffer where
201.Em n
202is a number in the range
203.Em [0,$] .
204.It Em - No or Em ^
205The previous line.
206This is equivalent to
207.Em -1
208and may be repeated with cumulative effect.
209.It Em -n No or Em ^n
210The
211.Em n Ns No th
212previous line, where
213.Em n
214is a non-negative number.
215.It Em +
216The next line.
217This is equivalent to
218.Em +1
219and may be repeated with cumulative effect.
220.It Em +n
221The
222.Em n Ns No th
223next line, where
224.Em n
225is a non-negative number.
226.It Em \&, No or Em %
227The first through last lines in the buffer.
228This is equivalent to the address range
229.Em 1,$ .
230.It Em \&;
231The current through last lines in the buffer.
232This is equivalent to the address range
233.Em .,$ .
234.It Em / Ns No re Ns Em /
235The next line containing the regular expression
236.Em re .
237The search wraps to the beginning of the buffer and continues down to the
238current line, if necessary.
239.Em //
240repeats the last search.
241.It Em ? Ns No re Ns Em ?
242The previous line containing the regular expression
243.Em re .
244The search wraps to the end of the buffer and continues up to the
245current line, if necessary.
246.Em ??
247repeats the last search.
248.It Em \&\' Ns No lc
249The line previously marked by a
250.Em k
251(mark) command, where
252.Em lc
253is a lower case letter.
254.El
255.Ss REGULAR EXPRESSIONS
256Regular expressions are patterns used in selecting text.
257For example, the
258.Nm
259command
260.Bd -literal -offset indent
261g/string/
262.Ed
263.Pp
264prints all lines containing
265.Em string .
266Regular expressions are also used by the
267.Em s
268command for selecting old text to be replaced with new.
269.Pp
270In addition to a specifying string literals, regular expressions can
271represent classes of strings.
272Strings thus represented are said to be matched by the
273corresponding regular expression.
274If it is possible for a regular expression to match several strings in
275a line, then the leftmost longest match is the one selected.
276.Pp
277The following symbols are used in constructing regular expressions:
278.Bl -tag -width Dsasdfsd
279.It Em c
280Any character
281.Em c
282not listed below, including
283.Em { Ns No ,
284.Em } Ns No ,
285.Em \&( Ns No ,
286.Em \&) Ns No ,
287.Em < Ns No ,
288and
289.Em >
290matches itself.
291.It Em \ec
292Any backslash-escaped character
293.Em c Ns No ,
294except for
295.Em { Ns No ,
296.Em } Ns No ,
297.Em \&( Ns No ,
298.Em \&) Ns No ,
299.Em < Ns No , and
300.Em >
301matches itself.
302.It Em \&.
303Matches any single character.
304.It Em [char-class]
305Matches any single character in
306.Em char-class .
307To include a
308.Ql \&]
309in
310.Em char-class Ns No ,
311it must be the first character.
312A range of characters may be specified by separating the end characters
313of the range with a
314.Ql - ;
315e.g.,
316.Em a-z
317specifies the lower case characters.
318The following literal expressions can also be used in
319.Em char-class
320to specify sets of characters:
321.Pp
322.Em \ \ [:alnum:]\ \ [:cntrl:]\ \ [:lower:]\ \ [:space:]
323.Em \ \ [:alpha:]\ \ [:digit:]\ \ [:print:]\ \ [:upper:]
324.Em \ \ [:blank:]\ \ [:graph:]\ \ [:punct:]\ \ [:xdigit:]
325.Pp
326If
327.Ql -
328appears as the first or last character of
329.Em char-class Ns No ,
330then it matches itself.
331All other characters in
332.Em char-class
333match themselves.
334.Pp
335Patterns in
336.Em char-class
337of the form
338.Em [.col-elm.] No or Em [=col-elm=]
339where
340.Em col-elm
341is a collating element are interpreted according to
342.Xr locale 5
343(not currently supported).
344See
345.Xr regex 3
346for an explanation of these constructs.
347.It Em [^char-class]
348Matches any single character, other than newline, not in
349.Em char-class Ns No .
350.Em char-class
351is defined as above.
352.It Em ^
353If
354.Em ^
355is the first character of a regular expression, then it
356anchors the regular expression to the beginning of a line.
357Otherwise, it matches itself.
358.It Em $
359If
360.Em $
361is the last character of a regular expression,
362it anchors the regular expression to the end of a line.
363Otherwise, it matches itself.
364.It Em \e<
365Anchors the single character regular expression or subexpression
366immediately following it to the beginning of a word.
367(This may not be available.)
368.It Em \e>
369Anchors the single character regular expression or subexpression
370immediately following it to the end of a word.
371(This may not be available.)
372.It Em \e( Ns No re Ns Em \e)
373Defines a subexpression
374.Em re .
375Subexpressions may be nested.
376A subsequent backreference of the form
377.Em \en Ns No ,
378where
379.Em n
380is a number in the range [1,9], expands to the text matched by the
381.Em n Ns No th
382subexpression.
383For example, the regular expression
384.Em \e(.*\e)\e1
385matches any string consisting of identical adjacent substrings.
386Subexpressions are ordered relative to their left delimiter.
387.It Em *
388Matches the single character regular expression or subexpression
389immediately preceding it zero or more times.
390If
391.Em *
392is the first character of a regular expression or subexpression,
393then it matches itself.
394The
395.Em *
396operator sometimes yields unexpected results.
397For example, the regular expression
398.Em b*
399matches the beginning of the string
400.Em abbb
401(as opposed to the substring
402.Em bbb Ns No ),
403since a null match is the only leftmost match.
404.Sm off
405.It Xo Em \e{ No n,m
406.Em \e}\ \e{ No n, Em \e}\
407.Em \e{ No n Em \e}
408.Xc
409.Sm on
410Matches the single character regular expression or subexpression
411immediately preceding it at least
412.Em n
413and at most
414.Em m
415times.
416If
417.Em m
418is omitted, then it matches at least
419.Em n
420times.
421If the comma is also omitted, then it matches exactly
422.Em n
423times.
424.El
425.Pp
426Additional regular expression operators may be defined depending on the
427particular
428.Xr regex 3
429implementation.
430.Ss COMMANDS
431All
432.Nm
433commands are single characters, though some require additional parameters.
434If a command's parameters extend over several lines, then
435each line except for the last must be terminated with a backslash
436.Pq Ql \e .
437.Pp
438In general, at most one command is allowed per line.
439However, most commands accept a print suffix, which is any of
440.Em p No (print),
441.Em l No (list),
442or
443.Em n No (enumerate),
444to print the last line affected by the command.
445.Pp
446An interrupt (typically ^C) has the effect of aborting the current command
447and returning the editor to command mode.
448.Pp
449.Nm
450recognizes the following commands.
451The commands are shown together with
452the default address or address range supplied if none is
453specified (in parentheses), and other possible arguments on the right.
454.Bl -tag -width Dxxs
455.It (.) Ns Em a
456Appends text to the buffer after the addressed line.
457Text is entered in input mode.
458The current address is set to last line entered.
459.It (.,.) Ns Em c
460Changes lines in the buffer.
461The addressed lines are deleted from the buffer,
462and text is appended in their place.
463Text is entered in input mode.
464The current address is set to last line entered.
465.It (.,.) Ns Em d
466Deletes the addressed lines from the buffer.
467If there is a line after the deleted range, then the current address is set
468to this line.
469Otherwise the current address is set to the line before the deleted range.
470.It Em e No file
471Edits
472.Em file Ns No ,
473and sets the default filename.
474If
475.Em file
476is not specified, then the default filename is used.
477Any lines in the buffer are deleted before the new file is read.
478The current address is set to the last line read.
479.It Em e No !command
480Edits the standard output of
481.Em !command Ns No ,
482(see
483.Em ! No command
484below).
485The default filename is unchanged.
486Any lines in the buffer are deleted before the output of
487.Em command
488is read.
489The current address is set to the last line read.
490.It Em E No file
491Edits
492.Em file
493unconditionally.
494This is similar to the
495.Em e
496command, except that unwritten changes are discarded without warning.
497The current address is set to the last line read.
498.It Em f No file
499Sets the default filename to
500.Em file Ns No .
501If
502.Em file
503is not specified, then the default unescaped filename is printed.
504.It (1,$) Ns Em g Ns No /re/command-list
505Applies
506.Em command-list
507to each of the addressed lines matching a regular expression
508.Em re Ns No .
509The current address is set to the line currently matched before
510.Em command-list
511is executed.
512At the end of the
513.Em g
514command, the current address is set to the last line affected by
515.Em command-list Ns No .
516.Pp
517Each command in
518.Em command-list
519must be on a separate line,
520and every line except for the last must be terminated by
521.Em \e No (backslash).
522Any commands are allowed, except for
523.Em g Ns No ,
524.Em G Ns No ,
525.Em v Ns No ,
526and
527.Em V Ns No .
528A newline alone in
529.Em command-list
530is equivalent to a
531.Em p
532command.
533.It (1,$) Ns Em G Ns No /re/
534Interactively edits the addressed lines matching a regular expression
535.Em re Ns No .
536For each matching line, the line is printed, the current address is set,
537and the user is prompted to enter a
538.Em command-list Ns No .
539At the end of the
540.Em g
541command, the current address is set to the last line affected by (the last)
542.Em command-list Ns No .
543.Pp
544The format of
545.Em command-list
546is the same as that of the
547.Em g
548command.
549A newline alone acts as a null command list.
550A single
551.Em &
552repeats the last non-null command list.
553.It Em H
554Toggles the printing of error explanations.
555By default, explanations are not printed.
556It is recommended that
557.Nm
558scripts begin with this command to aid in debugging.
559.It Em h
560Prints an explanation of the last error.
561.It (.) Ns Em i
562Inserts text in the buffer before the current line.
563Text is entered in input mode.
564The current address is set to the last line entered.
565.It (.,.+1) Ns Em j
566Joins the addressed lines.
567The addressed lines are deleted from the buffer and replaced by a single
568line containing their joined text.
569The current address is set to the resultant line.
570.It (.) Ns Em klc
571Marks a line with a lower case letter
572.Em lc Ns No \&.
573The line can then be addressed as
574.Em \&'lc
575(i.e., a single quote followed by
576.Em lc Ns No )
577in subsequent commands.
578The mark is not cleared until the line is deleted or otherwise modified.
579.It (.,.) Ns Em l
580Prints the addressed lines unambiguously.
581If a single line fills more than one screen (as might be the case
582when viewing a binary file, for instance), a
583.Dq --More--
584prompt is printed on the last line.
585.Nm
586waits until the RETURN key is pressed before displaying the next screen.
587The current address is set to the last line printed.
588.It (.,.) Ns Em m Ns No (.)
589Moves lines in the buffer.
590The addressed lines are moved to after the
591right-hand destination address, which may be the address
592.Em 0
593(zero).
594The current address is set to the last line moved.
595.It (.,.) Ns Em n
596Prints the addressed lines along with their line numbers.
597The current address is set to the last line printed.
598.It (.,.) Ns Em p
599Prints the addressed lines.
600The current address is set to the last line printed.
601.It Em P
602Toggles the command prompt on and off.
603Unless a prompt was specified by with command-line option
604.Fl p Ar string Ns No ,
605the command prompt is by default turned off.
606.It Em q
607Quits
608.Nm ed .
609.It Em Q
610Quits
611.Nm
612unconditionally.
613This is similar to the
614.Em q
615command, except that unwritten changes are discarded without warning.
616.It ($) Ns Em r No file
617Reads
618.Em file
619to after the addressed line.
620If
621.Em file
622is not specified, then the default filename is used.
623If there was no default filename prior to the command,
624then the default filename is set to
625.Em file Ns No .
626Otherwise, the default filename is unchanged.
627The current address is set to the last line read.
628.It ($) Ns Em r No !command
629Reads to after the addressed line the standard output of
630.Em !command Ns No ,
631(see the
632.Em !
633command below).
634The default filename is unchanged.
635The current address is set to the last line read.
636.Sm off
637.It Xo (.,.) Em s No /re/replacement/ , \ (.,.)
638.Em s No /re/replacement/ Em g , No \ (.,.)
639.Em s No /re/replacement/ Em n
640.Xc
641.Sm on
642Replaces text in the addressed lines matching a regular expression
643.Em re
644with
645.Em replacement Ns No .
646By default, only the first match in each line is replaced.
647If the
648.Em g
649(global) suffix is given, then every match to be replaced.
650The
651.Em n
652suffix, where
653.Em n
654is a positive number, causes only the
655.Em n Ns No th
656match to be replaced.
657It is an error if no substitutions are performed on any of the addressed
658lines.
659The current address is set the last line affected.
660.Pp
661.Em re
662and
663.Em replacement
664may be delimited by any character other than space and newline
665(see the
666.Em s
667command below).
668If one or two of the last delimiters is omitted, then the last line
669affected is printed as though the print suffix
670.Em p
671were specified.
672.Pp
673An unescaped
674.Ql \e
675in
676.Em replacement
677is replaced by the currently matched text.
678The character sequence
679.Em \em Ns No ,
680where
681.Em m
682is a number in the range [1,9], is replaced by the
683.Em m Ns No th
684backreference expression of the matched text.
685If
686.Em replacement
687consists of a single
688.Ql % ,
689then
690.Em replacement
691from the last substitution is used.
692Newlines may be embedded in
693.Em replacement
694if they are escaped with a backslash
695.Pq Ql \e .
696.It (.,.) Ns Em s
697Repeats the last substitution.
698This form of the
699.Em s
700command accepts a count suffix
701.Em n Ns No ,
702or any combination of the characters
703.Em r Ns No ,
704.Em g Ns No ,
705and
706.Em p Ns No .
707If a count suffix
708.Em n
709is given, then only the
710.Em n Ns No th
711match is replaced.
712The
713.Em r
714suffix causes
715the regular expression of the last search to be used instead of the
716that of the last substitution.
717The
718.Em g
719suffix toggles the global suffix of the last substitution.
720The
721.Em p
722suffix toggles the print suffix of the last substitution
723The current address is set to the last line affected.
724.It (.,.) Ns Em t Ns No (.)
725Copies (i.e., transfers) the addressed lines to after the right-hand
726destination address, which may be the address
727.Em 0
728(zero).
729The current address is set to the last line copied.
730.It Em u
731Undoes the last command and restores the current address
732to what it was before the command.
733The global commands
734.Em g Ns No ,
735.Em G Ns No ,
736.Em v Ns No ,
737and
738.Em V Ns No .
739are treated as a single command by undo.
740.Em u
741is its own inverse.
742.It (1,$) Ns Em v Ns No /re/command-list
743Applies
744.Em command-list
745to each of the addressed lines not matching a regular expression
746.Em re Ns No .
747This is similar to the
748.Em g
749command.
750.It (1,$) Ns Em V Ns No /re/
751Interactively edits the addressed lines not matching a regular expression
752.Em re Ns No .
753This is similar to the
754.Em G
755command.
756.It (1,$) Ns Em w No file
757Writes the addressed lines to
758.Em file Ns No .
759Any previous contents of
760.Em file
761is lost without warning.
762If there is no default filename, then the default filename is set to
763.Em file Ns No ,
764otherwise it is unchanged.
765If no filename is specified, then the default filename is used.
766The current address is unchanged.
767.It (1,$) Ns Em wq No file
768Writes the addressed lines to
769.Em file Ns No ,
770and then executes a
771.Em q
772command.
773.It (1,$) Ns Em w No !command
774Writes the addressed lines to the standard input of
775.Em !command Ns No ,
776(see the
777.Em !
778command below).
779The default filename and current address are unchanged.
780.It (1,$) Ns Em W No file
781Appends the addressed lines to the end of
782.Em file Ns No .
783This is similar to the
784.Em w
785command, expect that the previous contents of file is not clobbered.
786The current address is unchanged.
787.It Em x
788Prompts for an encryption key which is used in subsequent reads and writes.
789If a newline alone is entered as the key, then encryption is turned off.
790Otherwise, echoing is disabled while a key is read.
791Encryption/decryption is done using the
792.Xr bdes 1
793algorithm.
794.It (.+1) Ns Em z Ns No n
795Scrolls
796.Em n
797lines at a time starting at addressed line.
798If
799.Em n
800is not specified, then the current window size is used.
801The current address is set to the last line printed.
802.It ($) Ns Em =
803Prints the line number of the addressed line.
804.It (.+1) Ns Em newline
805Prints the addressed line, and sets the current address to that line.
806.It Em ! Ns No command
807Executes
808.Em command
809via
810.Xr sh 1 .
811If the first character of
812.Em command
813is
814.Em ! Ns No ,
815then it is replaced by text of the previous
816.Em !command Ns No .
817.Nm
818does not process
819.Em command
820for
821.Em \e
822(backslash) escapes.
823However, an unescaped
824.Em %
825is replaced by the default filename.
826When the shell returns from execution, a
827.Em !
828is printed to the standard output.
829The current line is unchanged.
830.El
831.Sh LIMITATIONS
832.Nm
833processes
834.Em file
835arguments for backslash escapes, i.e., in a filename,
836any characters preceded by a backslash
837.Pq Ql \e
838are interpreted literally.
839.Pp
840If a text (non-binary) file is not terminated by a newline character,
841then
842.Nm
843appends one on reading/writing it.
844In the case of a binary file,
845.Nm
846does not append a newline on reading/writing.
847.Sh DIAGNOSTICS
848When an error occurs,
849.Nm
850prints a
851.Dq ?
852and either returns to command mode or exits if its input is from a script.
853An explanation of the last error can be printed with the
854.Em h
855(help) command.
856.Pp
857Since the
858.Em g
859(global) command masks any errors from failed searches and substitutions,
860it can be used to perform conditional operations in scripts; e.g.,
861.Bd -literal -offset indent
862g/old/s//new/
863.Ed
864.Pp
865replaces any occurrences of
866.Em old
867with
868.Em new Ns No .
869.Pp
870If the
871.Em u
872(undo) command occurs in a global command list, then
873the command list is executed only once.
874.Pp
875If diagnostics are not disabled, attempting to quit
876.Nm
877or edit another file before writing a modified buffer results in an error.
878If the command is entered a second time, it succeeds,
879but any changes to the buffer are lost.
880.Sh FILES
881.Bl -tag -width /tmp/ed.* -compact
882.It Pa /tmp/ed.*
883buffer file
884.It Pa ed.hup
885where
886.Nm
887attempts to write the buffer if the terminal hangs up
888.El
889.Sh SEE ALSO
890.Xr bdes 1 ,
891.Xr sed 1 ,
892.Xr sh 1 ,
893.Xr vi 1 ,
894.Xr regex 3
895.Pp
896USD:12-13
897.Rs
898.%A B. W. Kernighan
899.%A P. J. Plauger
900.%B Software Tools in Pascal
901.%O Addison-Wesley
902.%D 1981
903.Re
904.Sh HISTORY
905An
906.Nm
907command appeared in
908.At v1 .
909