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