xref: /openbsd/bin/ed/ed.1 (revision 4bdff4be)
1.\"	$OpenBSD: ed.1,v 1.77 2022/06/19 05:49:51 jmc Exp $
2.\"
3.\" Copyright (c) 1993 Andrew Moore, Talke Studio.
4.\" All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\"
15.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25.\" SUCH DAMAGE.
26.\"
27.Dd $Mdocdate: June 19 2022 $
28.Dt ED 1
29.Os
30.Sh NAME
31.Nm ed
32.Nd text editor
33.Sh SYNOPSIS
34.Nm ed
35.Op Fl
36.Op Fl s
37.Op Fl p Ar string
38.Op Ar file
39.Sh DESCRIPTION
40.Nm
41is a line-oriented text editor.
42It is used to create, display, modify, and otherwise manipulate text files.
43If invoked with a
44.Ar file
45argument, then a copy of
46.Ar file
47is read into the editor's buffer.
48Changes are made to this copy and not directly to
49.Ar file
50itself.
51Upon quitting
52.Nm ed ,
53any changes not explicitly saved with a
54.Ic w
55command are lost.
56.Pp
57Editing is done in two distinct modes:
58.Em command
59and
60.Em input .
61When first invoked,
62.Nm
63is in command mode.
64In this mode, commands are read from the standard input and
65executed to manipulate the contents of the editor buffer.
66.Pp
67A typical command might look like:
68.Pp
69.Dl ,s/old/new/g
70.Pp
71which replaces all occurrences of the string
72.Qq old
73with
74.Qq new .
75.Pp
76When an input command, such as
77.Ic a
78.Pq append ,
79.Ic i
80.Pq insert ,
81or
82.Ic c
83.Pq change
84is given,
85.Nm
86enters input mode.
87This is the primary means of adding text to a file.
88In this mode, no commands are available;
89instead, the standard input is written directly to the editor buffer.
90Lines consist of text up to and including a newline character.
91Input mode is terminated by entering a single period
92.Pq Ql \&.
93on a line.
94.Pp
95All
96.Nm
97commands operate on whole lines or ranges of lines; e.g.,
98the
99.Ic d
100command deletes lines; the
101.Ic m
102command moves lines, and so on.
103It is possible to modify only a portion of a line by means of replacement,
104as in the example above.
105However, even here, the
106.Ic s
107command is applied to whole lines at a time.
108.Pp
109In general,
110.Nm
111commands consist of zero or more line addresses, followed by a single
112character command and possibly additional parameters; i.e.,
113commands have the structure:
114.Pp
115.Sm off
116.D1 Oo Ar address Oo , Ar address Oc Oc Ar command Op Ar parameters
117.Sm on
118.Pp
119The address(es) indicate the line or range of lines to be affected by the
120command.
121If fewer addresses are given than the command accepts, then
122default addresses are supplied.
123.Pp
124Many
125.Nm
126commands and line addresses support basic regular expressions
127.Pq BREs .
128See
129.Xr re_format 7
130for more information on regular expressions.
131.Pp
132The options are as follows:
133.Bl -tag -width "-p string"
134.It Fl
135Same as the
136.Fl s
137option
138.Pq deprecated .
139.It Fl p Ar string
140Specifies a command prompt.
141This may be toggled on and off with the
142.Ic P
143command.
144.It Fl s
145Suppress diagnostics.
146This should be used if
147.Nm
148standard input is from a script.
149.It Ar file
150Specifies the name of a file to read.
151If
152.Ar file
153is prefixed with a
154bang
155.Pq Ql \&! ,
156then it is interpreted as a shell command.
157In this case, what is read is the standard output of
158.Ar file
159executed via
160.Xr sh 1 .
161To read a file whose name begins with a bang, prefix the
162name with a backslash
163.Pq Ql \e .
164The default filename is set to
165.Ar file
166only if it is not prefixed with a bang.
167.El
168.Ss LINE ADDRESSING
169An address represents the number of a line in the buffer.
170.Nm
171maintains a
172.Em current address
173which is typically supplied to commands as the default address
174when none is specified.
175When a file is first read, the current address is set to the last line
176of the file.
177In general, the current address is set to the last line affected by a command.
178.Pp
179A line address is
180constructed from one of the bases in the list below, optionally followed
181by a numeric offset.
182The offset may include any combination of digits, operators (e.g.,
183.Ql + ,
184.Ql - ,
185and
186.Ql ^ ) ,
187and whitespace.
188Addresses are read from left to right, and their values are computed
189relative to the current address.
190.Pp
191One exception to the rule that addresses represent line numbers is the
192address
193.Ad 0
194.Pq zero .
195This means
196.Dq before the first line ,
197and is legal wherever it makes sense.
198.Pp
199An address range is two addresses separated either by a comma or semi-colon.
200The value of the first address in a range cannot exceed the
201value of the second.
202If only one address is given in a range,
203then the second address is set to the given address.
204If an
205.Ar n Ns -tuple
206of addresses is given where
207.Ar n
208\*(Gt 2,
209then the corresponding range is determined by the last two addresses in the
210.Ar n Ns -tuple .
211If only one address is expected, then the last address is used.
212.Pp
213Each address in a comma-delimited range is interpreted relative to the
214current address.
215In a semi-colon-delimited range, the first address is
216used to set the current address, and the second address is interpreted
217relative to the first.
218.Pp
219The following address symbols are recognized:
220.Bl -tag -width Ds
221.It \&.
222The current line
223.Pq address
224in the buffer.
225.It $
226The last line in the buffer.
227.It Ar n
228The
229.Ar n Ns th
230line in the buffer, where
231.Ar n
232is a number in the range
233.Ad [0,$] .
234.It - or ^
235The previous line.
236This is equivalent to
237.Ad \-1
238and may be repeated with cumulative effect.
239.It Xo
240.Pf - Ar n No or\ \&
241.Pf ^ Ar n
242.Xc
243The
244.Ar n Ns th
245previous line, where
246.Ar n
247is a non-negative number.
248.It +
249The next line.
250This is equivalent to
251.Ad +1
252and may be repeated with cumulative effect.
253.It + Ns Ar n
254The
255.Ar n Ns th
256next line, where
257.Ar n
258is a non-negative number.
259.It \&, or %
260The first through last lines in the buffer.
261This is equivalent to the address range
262.Ad 1,$ .
263.It \&;
264The current through last lines in the buffer.
265This is equivalent to the address range
266.Ad .,$ .
267.It / Ns Ar re Ns /
268The next line containing the regular expression
269.Ar re .
270The search wraps to the beginning of the buffer and continues down to the
271current line, if necessary.
272The second slash can be omitted if it ends a line.
273.Qq //
274repeats the last search.
275.It Pf ? Ar re ?
276The previous line containing the regular expression
277.Ar re .
278The search wraps to the end of the buffer and continues up to the
279current line, if necessary.
280The second question mark can be omitted if it ends a line.
281.Qq ??
282repeats the last search.
283.It \&' Ns Ar lc
284The line previously marked by a
285.Ic k
286.Pq mark
287command, where
288.Ar lc
289is a lower case letter.
290.El
291.Ss COMMANDS
292All
293.Nm
294commands are single characters, though some require additional parameters.
295If a command's parameters extend over several lines, then
296each line except for the last must be terminated with a backslash
297.Pq Ql \e .
298.Pp
299In general, at most one command is allowed per line.
300However, most commands accept a print suffix, which is any of
301.Ic p
302.Pq print ,
303.Ic l
304.Pq list ,
305or
306.Ic n
307.Pq enumerate ,
308to print the last line affected by the command.
309.Pp
310.Nm
311recognizes the following commands.
312The commands are shown together with
313the default address or address range supplied if none is specified
314.Pq in parentheses ,
315and other possible arguments on the right.
316.Bl -tag -width Dxxs
317.It (.) Ns Ic a
318Appends text to the buffer after the addressed line.
319Text is entered in input mode.
320The current address is set to last line entered.
321.It (.,.) Ns Ic c
322Changes lines in the buffer.
323The addressed lines are deleted from the buffer,
324and text is appended in their place.
325Text is entered in input mode.
326The current address is set to last line entered.
327.It (.,.) Ns Ic d
328Deletes the addressed lines from the buffer.
329If there is a line after the deleted range, then the current address is set
330to this line.
331Otherwise the current address is set to the line before the deleted range.
332.It Ic e Ar file
333Edits
334.Ar file ,
335and sets the default filename.
336If
337.Ar file
338is not specified, then the default filename is used.
339Any lines in the buffer are deleted before the new file is read.
340The current address is set to the last line read.
341.It Ic e No \&! Ns Ar command
342Edits the standard output of
343.No \&! Ns Ar command ,
344(see
345.Ic \&! Ns Ar command
346below).
347The default filename is unchanged.
348Any lines in the buffer are deleted before the output of
349.Ar command
350is read.
351The current address is set to the last line read.
352.It Ic E Ar file
353Edits
354.Ar file
355unconditionally.
356This is similar to the
357.Ic e
358command, except that unwritten changes are discarded without warning.
359The current address is set to the last line read.
360.It Ic f Ar file
361Sets the default filename to
362.Ar file .
363If
364.Ar file
365is not specified, then the default unescaped filename is printed.
366.Sm off
367.It Xo
368.Pf (1,$) Ic g No /
369.Ar re No / Ar command-list
370.Xc
371.Sm on
372Mark each addressed line matching the regular expression
373.Ar re
374for modification.
375The current address is set to each marked line in turn, and then the
376.Ar command-list
377is executed each time.
378The command-list can change the current line number,
379and it is not changed back after the command-list ended.
380When a marked line is changed, it is unmarked
381and the command-list won't be executed for it any more.
382If no lines were matched,
383the current line number remains unchanged.
384.Pp
385Each command in
386.Ar command-list
387must be on a separate line,
388and every line except for the last must be terminated by a backslash
389.Pq Sq \e .
390Any commands are allowed, except for
391.Ic g ,
392.Ic G ,
393.Ic v ,
394and
395.Ic V .
396An empty
397.Ar command-list
398is equivalent to a
399.Ic p
400command \(em unlike for the
401.Cm G
402command, where an empty command-list does nothing, and unlike an empty
403command, which is equivalent to the command
404.Cm +p .
405If the
406.Ar command-list
407is empty, the trailing slash can be omitted.
408.Sm off
409.It (1,$) Ic G No / Ar re No /
410.Sm on
411Interactively edits the addressed lines matching a regular expression
412.Ar re .
413The trailing slash after
414.Ar re
415can be omitted.
416For each matching line, the line is printed, the current address is set,
417and the user is prompted to enter a
418.Ar command-list .
419At the end of the
420.Ic G
421command, the current address is set to the last line affected by
422.Pq the last
423command-list.
424If no lines were matched,
425the current line number remains unchanged.
426.Pp
427The format of
428.Ar command-list
429is the same as that of the
430.Ic g
431command, but an empty command list does nothing.
432A single
433.Sq &
434repeats the last non-empty command list.
435.It Ic H
436Toggles the printing of error explanations.
437By default, explanations are not printed.
438It is recommended that
439.Nm
440scripts begin with this command to aid in debugging.
441.It Ic h
442Prints an explanation of the last error.
443.It (.) Ns Ic i
444Inserts text in the buffer before the current line.
445Text is entered in input mode.
446The current address is set to the last line entered.
447.It (.,+) Ns Ic j
448Joins the addressed lines.
449The addressed lines are deleted from the buffer and replaced by a single
450line containing their joined text.
451The current address is set to the resultant line.
452.It (.) Ns Ic k Ns Ar lc
453Marks a line with a lower case letter
454.Ar lc .
455The line can then be addressed as
456.Ic ' Ns Ar lc
457(i.e., a single quote followed by
458.Ar lc )
459in subsequent commands.
460The mark is not cleared until the line is deleted or otherwise modified.
461.It (.,.) Ns Ic l
462Prints the addressed lines unambiguously.
463The current address is set to the last line printed.
464.It (.,.) Ns Ic m Ns (.)
465Moves lines in the buffer.
466The addressed lines are moved to after the
467right-hand destination address, which may be the address
468.Ad 0
469.Pq zero .
470The current address is set to the last line moved.
471.It (.,.) Ns Ic n
472Prints the addressed lines along with their line numbers.
473The current address is set to the last line printed.
474.It (.,.) Ns Ic p
475Prints the addressed lines.
476The current address is set to the last line printed.
477.It Ic P
478Toggles the command prompt on and off.
479Unless a prompt was specified with the command-line option
480.Fl p Ar string ,
481the command prompt is by default turned off.
482.It Ic q
483Quits
484.Nm ed .
485.It Ic Q
486Quits
487.Nm
488unconditionally.
489This is similar to the
490.Ic q
491command, except that unwritten changes are discarded without warning.
492.It ($) Ns Ic r Ar file
493Reads
494.Ar file
495to after the addressed line.
496If
497.Ar file
498is not specified, then the default filename is used.
499If there was no default filename prior to the command,
500then the default filename is set to
501.Ar file .
502Otherwise, the default filename is unchanged.
503The current address is set to the last line read.
504.It ($) Ns Ic r No \&! Ns Ar command
505Reads to after the addressed line the standard output of
506.No \&! Ns Ar command ,
507(see
508.Ic \&! Ns Ar command
509below).
510The default filename is unchanged.
511The current address is set to the last line read.
512.Sm off
513.It Xo
514.Pf (.,.) Ic s No / Ar re
515.No / Ar replacement No /\ \&
516.Pf (.,.) Ic s No / Ar re
517.No / Ar replacement No / Ic g\ \&
518.No (.,.) Ic s No / Ar re
519.No / Ar replacement No / Ar n
520.Xc
521.Sm on
522Replaces text in the addressed lines matching a regular expression
523.Ar re
524with
525.Ar replacement .
526By default, only the first match in each line is replaced.
527If the
528.Ic g
529.Pq global
530suffix is given, then every match is replaced.
531The
532.Ar n
533suffix, where
534.Ar n
535is a positive number, causes only the
536.Ar n Ns th
537match to be replaced.
538It is an error if no substitutions are performed on any of the addressed
539lines.
540The current address is set to the last line affected.
541.Pp
542.Ar re
543and
544.Ar replacement
545may be delimited by any character other than space and newline
546(see the
547.Ic s
548command below).
549If one or two of the last delimiters is omitted, then the last line
550affected is printed as though the print suffix
551.Ic p
552were specified.
553.Pp
554An unescaped
555.Ql &
556in
557.Ar replacement
558is replaced by the currently matched text.
559The character sequence
560.Pf \e Ar m ,
561where
562.Ar m
563is a number in the range [1,9], is replaced by the
564.Ar m Ns th
565backreference expression of the matched text.
566If
567.Ar replacement
568consists of a single
569.Ql % ,
570then
571.Ar replacement
572from the last substitution is used.
573Newlines may be embedded in
574.Ar replacement
575if they are escaped with a backslash
576.Pq Ql \e .
577.It (.,.) Ns Ic s
578Repeats the last substitution.
579This form of the
580.Ic s
581command accepts a count suffix
582.Ar n ,
583or any combination of the characters
584.Ic r ,
585.Ic g ,
586and
587.Ic p .
588If a count suffix
589.Ar n
590is given, then only the
591.Ar n Ns th
592match is replaced.
593The
594.Ic r
595suffix causes the regular expression of the last search to be used
596instead of that of the last substitution.
597The
598.Ic g
599suffix toggles the global suffix of the last substitution.
600The
601.Ic p
602suffix toggles the print suffix of the last substitution.
603The current address is set to the last line affected.
604.It (.,.) Ns Ic t Ns (.)
605Copies
606.Pq i.e., transfers
607the addressed lines to after the right-hand destination address,
608which may be the address
609.Ad 0
610.Pq zero .
611The current address is set to the last line copied.
612.It Ic u
613Undoes the last command and restores the current address
614to what it was before the command.
615The global commands
616.Ic g ,
617.Ic G ,
618.Ic v ,
619and
620.Ic V
621are treated as a single command by undo.
622.Ic u
623is its own inverse.
624.Sm off
625.It Xo
626.Pf (1,$) Ic v No / Ar re
627.Pf / Ar command-list
628.Xc
629.Sm on
630The same as the
631.Ic g
632command, except that it applies
633.Ar command-list
634to each of the addressed lines not matching the regular expression
635.Ar re .
636.Sm off
637.It Xo
638.Pf (1,$) Ic V No /
639.Ar re No /
640.Xc
641.Sm on
642The same as the
643.Ic G
644command, except that it interactively edits the addressed lines
645not matching the regular expression
646.Ar re .
647.It (1,$) Ns Ic w Ar file
648Writes the addressed lines to
649.Ar file .
650Any previous contents of
651.Ar file
652are lost without warning.
653If there is no default filename, then the default filename is set to
654.Ar file ,
655otherwise it is unchanged.
656If no filename is specified, then the default filename is used.
657The current address is unchanged.
658.It (1,$) Ns Ic wq Ar file
659Writes the addressed lines to
660.Ar file ,
661and then executes a
662.Ic q
663command.
664.It (1,$) Ns Ic w No \&! Ns Ar command
665Writes the addressed lines to the standard input of
666.No \&! Ns Ar command ,
667(see
668.Ic \&! Ns Ar command
669below).
670The default filename and current address are unchanged.
671.It (1,$) Ns Ic W Ar file
672Appends the addressed lines to the end of
673.Ar file .
674This is similar to the
675.Ic w
676command, except that the previous contents of file are not clobbered.
677The current address is unchanged.
678.It (+) Ns Ic z Ns Ar n
679Scrolls
680.Ar n
681lines at a time starting at addressed line.
682If
683.Ar n
684is not specified, then the current window size is used.
685The current address is set to the last line printed.
686.It ($) Ns Ic =
687Prints the line number of the addressed line.
688.It (+)
689An address without a command prints the addressed line
690and sets the current address to that line.
691If the address is also omitted, it defaults to the next line (+).
692.It Ic \&! Ns Ar command
693Executes
694.Ar command
695via
696.Xr sh 1 .
697If the first character of
698.Ar command
699is
700.Sq !\& ,
701then it is replaced by text of the previous
702.Ic \&! Ns Ar command .
703.Nm
704does not process
705.Ar command
706for
707.Sq \e
708.Pq backslash
709escapes.
710However, an unescaped
711.Sq %
712is replaced by the default filename.
713When the shell returns from execution, a
714.Sq \&!
715is printed to the standard output.
716The current line is unchanged.
717.El
718.Sh ASYNCHRONOUS EVENTS
719.Bl -tag -width "SIGWINCH"
720.It Dv SIGHUP
721If the current buffer has changed since it was last written,
722.Nm
723attempts to write the buffer to the file
724.Pa ed.hup .
725Nothing is written to the currently remembered file, and
726.Nm
727exits.
728.It Dv SIGINT
729When an interrupt occurs,
730.Nm
731prints
732.Sq ?\en
733and returns to command mode.
734If interrupted during text input,
735the text already input is written to the current buffer,
736as if text input had been normally terminated.
737.It Dv SIGQUIT
738This signal is ignored.
739.It Dv SIGWINCH
740The screen is resized.
741.El
742.Sh FILES
743.Bl -tag -width /tmp/ed.* -compact
744.It Pa /tmp/ed.*
745buffer file
746.It Pa ed.hup
747where
748.Nm
749attempts to write the buffer if the terminal hangs up
750.El
751.Sh EXIT STATUS
752.Ex -std ed
753.Sh DIAGNOSTICS
754When an error occurs,
755.Nm
756prints a
757.Sq \&?
758and either returns to command mode or exits if its input is from a script.
759An explanation of the last error can be printed with the
760.Ic h
761.Pq help
762command.
763.Pp
764Since the
765.Ic g
766.Pq global
767command masks any errors from failed searches and substitutions,
768it can be used to perform conditional operations in scripts; e.g.,
769.Pp
770.Dl g/old/s//new/
771.Pp
772replaces any occurrences of
773.Qq old
774with
775.Qq new .
776.Pp
777If the
778.Ic u
779.Pq undo
780command occurs in a global command list,
781then the command list is executed only once.
782.Pp
783If diagnostics are not disabled, attempting to quit
784.Nm
785or edit another file before writing a modified buffer results in an error.
786If the command is entered a second time, it succeeds,
787but any changes to the buffer are lost.
788.Sh SEE ALSO
789.Xr sed 1 ,
790.Xr sh 1 ,
791.Xr vi 1 ,
792.Xr re_format 7
793.Rs
794.\" 4.4BSD USD:9
795.%A B. W. Kernighan
796.%T A Tutorial Introduction to the UNIX Text Editor
797.Re
798.Rs
799.\" 4.4BSD USD:10
800.%A B. W. Kernighan
801.%T Advanced Editing on UNIX
802.Re
803.Rs
804.%A B. W. Kernighan
805.%A P. J. Plauger
806.%B Software Tools in Pascal
807.%O Addison-Wesley
808.%D 1981
809.Re
810.Sh STANDARDS
811The
812.Nm
813utility is compliant with the
814.St -p1003.1-2008
815specification.
816.Pp
817The commands
818.Cm s
819(to repeat the last substitution),
820.Cm W ,
821.Cm wq ,
822and
823.Cm z
824as well as the address specifier
825.Sq %
826are extensions to that specification.
827.Pp
828The
829.St -p1003.1-2008
830specification says the
831.Sq ^
832address specifier is neither required nor prohibited;
833additionally, it says behaviour for the
834.Fl
835option is
836.Dq unspecified .
837.Sh HISTORY
838An
839.Nm
840command appeared in
841.At v1 .
842.Sh CAVEATS
843.Nm
844processes
845.Ar file
846arguments for backslash escapes, i.e., in a filename,
847any characters preceded by a backslash
848.Pq Ql \e
849are interpreted literally.
850.Pp
851If a text
852.Pq non-binary
853file is not terminated by a newline character,
854then
855.Nm
856appends one on reading/writing it.
857In the case of a binary file,
858.Nm
859does not append a newline on reading/writing.
860