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