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