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