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