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