.Xr botch fix
[unix-history] / usr / src / bin / ed / ed.1
CommitLineData
dd9e2e8d
CL
1.\" Copyright (c) 1990 Regents of the University of California.
2.\" All rights reserved. The Berkeley software License Agreement
3.\" specifies the terms and conditions for redistribution.
fd315f2c 4.\"
5325ced3 5.\" @(#)ed.1 6.5 (Berkeley) %G%
b5dc1377
CL
6.\"
7.Dd
8.Dt ED 1
9.Os ATT 7th
fd315f2c
KM
10.if t .ds q \(aa
11.if n .ds q '
b5dc1377
CL
12.Sh NAME
13.Nm ed
14.Nd text editor
15.Sh SYNOPSIS
16.Nm ed
17.Op Fl
18.Op Fl x
19.Op Ar file
20.Sh DESCRIPTION
21.Nm Ed
fd315f2c 22is the standard text editor.
b5dc1377 23.Pp
fd315f2c 24If a
b5dc1377 25.Ar file
fd315f2c 26argument is given,
b5dc1377 27.Nm ed
fd315f2c 28simulates an
b5dc1377
CL
29.Ic e
30command (see below) on the named file; that is to say,
fd315f2c 31the file is read into
b5dc1377 32.Nm ed 's
fd315f2c 33buffer so that it can be edited.
5325ced3 34.Tw Ds
b5dc1377
CL
35.Tp Fl
36Suppresses the printing
fd315f2c
KM
37of explanatory output
38and should be used
39when the standard input is
40an editor script.
b5dc1377
CL
41.Tp
42.Pp
43.Nm Ed
fd315f2c
KM
44operates on a copy of any file it is editing; changes made
45in the copy have no effect on the file until a
b5dc1377
CL
46.Ic w
47(write) command is given.
fd315f2c 48The copy of the text being edited resides
b5dc1377
CL
49in a temporary file called the
50.Ar buffer .
51.Pp
fd315f2c 52Commands to
b5dc1377 53.Nm ed
fd315f2c
KM
54have a simple and regular structure: zero or
55more
b5dc1377 56.Ar addresses
fd315f2c 57followed by a single character
b5dc1377 58.Ar command ,
fd315f2c
KM
59possibly
60followed by parameters to the command.
61These addresses specify one or more lines in the buffer.
62Missing addresses are supplied by default.
b5dc1377 63.Pp
fd315f2c 64In general, only one command may appear on a line.
b5dc1377 65Certain commands allow the
fd315f2c
KM
66addition of text to the buffer.
67While
b5dc1377 68.Nm ed
fd315f2c
KM
69is accepting text, it is said
70to be in
b5dc1377 71.Ar input mode .
fd315f2c
KM
72In this mode, no commands are recognized;
73all input is merely collected.
b5dc1377
CL
74Input mode is left by typing a period
75.Sq Ic \&.
76alone at the
fd315f2c 77beginning of a line.
b5dc1377
CL
78.Pp
79.Nm Ed
fd315f2c 80supports a limited form of
b5dc1377 81.Ar regular expression
fd315f2c
KM
82notation.
83A regular expression specifies
84a set of strings of characters.
85A member of this set of strings is said to be
b5dc1377 86.Ar matched
fd315f2c
KM
87by the regular expression.
88In the following specification for regular expressions
89the word `character' means any character but newline.
b5dc1377
CL
90.Tw Ds
91.Tp 1.
fd315f2c
KM
92Any character except a special character
93matches itself.
94Special characters are
95the regular expression delimiter plus
5325ced3 96.Sq Cm \e\[.
b5dc1377 97and sometimes
5325ced3 98.Sq Cm ^*$ .
b5dc1377 99.Tp 2.
fd315f2c 100A
5325ced3 101.Sq Cm \&.
fd315f2c 102matches any character.
b5dc1377
CL
103.Tp 3.
104A
105.Sq Li \e
106followed by any character except a digit or
107.Li (\)
108matches that character.
109.Tp 4.
fd315f2c 110A nonempty string
b5dc1377
CL
111.Op Ar s
112or
5325ced3 113.Oo
b5dc1377 114.Cx \&(
5325ced3 115.Op Cm \&^ Ar s
b5dc1377
CL
116.Cx \&)
117.Cx
5325ced3 118.Oo
fd315f2c 119matches any character in (or not in)
b5dc1377
CL
120.Ar s .
121In
122.Ar s ,
123.Sq Li \e
124has no special meaning, and
125may only appear as
fd315f2c 126the first letter.
b5dc1377
CL
127A substring
128.Ar a\-b ,
fd315f2c 129with
b5dc1377 130.Ar a
fd315f2c 131and
b5dc1377 132.Ar b
fd315f2c
KM
133in ascending ASCII order, stands for the inclusive
134range of ASCII characters.
b5dc1377
CL
135.Tp 5.
136A regular expression of form 1\-4 followed by * matches a sequence of
fd315f2c 1370 or more matches of the regular expression.
b5dc1377 138.Tp 6.
fd315f2c 139A regular expression,
b5dc1377
CL
140.Ar x ,
141of form 1\-8, bracketed
142.Cx Li \e(
143.Ar x
144.Li \e)
145.Cx
fd315f2c 146matches what
b5dc1377 147.Ar x
fd315f2c 148matches.
b5dc1377
CL
149.Tp 7.
150A \e followed by a digit
151.Ar n
fd315f2c
KM
152matches a copy of the string that the
153bracketed regular expression beginning with the
b5dc1377
CL
154.Cx Ar n
155.Cx \'th
156.Cx
157.Li \e(
158matched.
159.Tp 8.
160A regular expression of form 1\-8,
161.Ar x ,
162followed by a regular expression of form 1\-7,
163.Ar y
fd315f2c 164matches a match for
b5dc1377 165.Ar x
fd315f2c 166followed by a match for
b5dc1377 167.Ar y ,
fd315f2c 168with the
b5dc1377 169.Ar x
fd315f2c 170match being as long as possible while still permitting a
b5dc1377 171.Ar y
fd315f2c 172match.
b5dc1377
CL
173.Tp 9.
174A regular expression of form 1\-8 preceded by
175.Sq Li ^
176(or followed by
177.Sq Li $ ) ,
178is constrained to matches that
fd315f2c 179begin at the left (or end at the right) end of a line.
b5dc1377
CL
180.Tp 10.
181A regular expression of form 1\-9 picks out the
fd315f2c 182longest among the leftmost matches in a line.
b5dc1377 183.Tp 11.
fd315f2c
KM
184An empty regular expression stands for a copy of the
185last regular expression encountered.
b5dc1377
CL
186.Tp
187.Pp
fd315f2c
KM
188Regular expressions are used in addresses to specify
189lines and in one command
190(see
b5dc1377
CL
191.Ar s
192below)
fd315f2c
KM
193to specify a portion of a line which is to be replaced.
194If it is desired to use one of
195the regular expression metacharacters as an ordinary
b5dc1377
CL
196character, that character may be preceded by
197.Sq Li \e .
fd315f2c 198This also applies to the character bounding the regular
b5dc1377
CL
199expression (often
200.Sq Li \&/ )
201and to
202.Sq Li \e
203itself.
204.Pp
fd315f2c 205To understand addressing in
b5dc1377 206.Nm ed
fd315f2c 207it is necessary to know that at any time there is a
b5dc1377 208.Ar current line.
fd315f2c
KM
209Generally speaking, the current line is
210the last line affected by a command; however,
211the exact effect on the current line
212is discussed under the description of
213the command.
214Addresses are constructed as follows.
b5dc1377
CL
215.Tw Ds
216.Tp 1.
217The character
218.Sq Ic \&.
219addresses the current line.
220.Tp 2.
221The character
222.Cx `
223.Ic $
224.Cx \'
225.Cx
226addresses the last line of the buffer.
227.Tp 3.
fd315f2c 228A decimal number
b5dc1377 229.Ar n
fd315f2c 230addresses the
b5dc1377
CL
231.Cx Ar n
232.Cx \'th
233.Cx
fd315f2c 234line of the buffer.
b5dc1377
CL
235.Tp 4.
236.Cx `\(fm
237.Ar x
238.Cx \'
239.Cx
240addresses the line marked with the name
241.Ar x ,
fd315f2c
KM
242which must be a lower-case letter.
243Lines are marked with the
b5dc1377 244.Ar k
fd315f2c 245command described below.
b5dc1377
CL
246.Tp 5.
247A regular expression enclosed in slashes
248.Cx `
249.Li /
250.Cx \'
251.Cx
252addresses
fd315f2c
KM
253the line found by searching forward from the current line
254and stopping at the first line containing a
255string that matches the regular expression.
256If necessary the search wraps around to the beginning of the
257buffer.
b5dc1377
CL
258.Tp 6.
259A regular expression enclosed in queries
260.Cx `
261.Li ?
262.Cx \'
263.Cx
264addresses
fd315f2c
KM
265the line found by searching backward from the current line
266and stopping at the first line containing
267a string that matches the regular expression.
268If necessary
269the search wraps around to the end of the buffer.
b5dc1377
CL
270.Tp 7.
271An address followed by a plus sign
272.Cx `
273.Li +
274.Cx \'
275.Cx
276or a minus sign
277.Cx `
278.Li \-
279.Cx \'
280.Cx
281followed by a decimal number specifies that address plus
282(resp. minus) the indicated number of lines.
fd315f2c 283The plus sign may be omitted.
b5dc1377
CL
284.Tp 8.
285If an address begins with
286.Cx `
287.Li +
288.Cx \'
289.Cx
290or
291.Cx `
292.Li \-
293.Cx \'
294.Cx
fd315f2c 295the addition or subtraction is taken with respect to the current line;
b5dc1377
CL
296e.g.
297.Cx `
298.Li \-5
299.Cx \'
300.Cx
301is understood to mean
302.Cx `
303.Li .\-5
304.Cx \'
305.Cx
306.Nm .
307.Tp 9.
308If an address ends with
309.Cx `
310.Li +
311.Cx \'
312.Cx
313.Cx `
314.Li \-
315.Cx \',
316.Cx
fd315f2c
KM
317then 1 is added (resp. subtracted).
318As a consequence of this rule and rule 8,
b5dc1377
CL
319the address
320.Cx `
321.Li \-
322.Cx \'
323.Cx
324refers to the line before the current line.
fd315f2c
KM
325Moreover,
326trailing
b5dc1377
CL
327.Cx `
328.Li +
329.Cx \'
330.Cx
331and
332.Cx `
333.Li \-
334.Cx \'
335.Cx
336characters
337have cumulative effect, so
338.Cx `
339.Li \-\-
340.Cx \'
341.Cx
342refers to the current
fd315f2c 343line less 2.
b5dc1377 344.Tp 10.
fd315f2c 345To maintain compatibility with earlier versions of the editor,
b5dc1377
CL
346the character
347.Cx `
348.Li ^
349.Cx \'
350.Cx
351in addresses is
352equivalent to
353.Cx `
354.Li \-
355.Cx \'.
356.Cx
357.Tp
358.Pp
fd315f2c
KM
359Commands may require zero, one, or two addresses.
360Commands which require no addresses regard the presence
361of an address as an error.
362Commands which accept one or two addresses
363assume default addresses when insufficient are given.
364If more addresses are given than such a command requires,
b5dc1377
CL
365the last one or two (depending on what is accepted) are used.
366.Pp
fd315f2c 367Addresses are separated from each other typically by a comma
b5dc1377
CL
368.Cx `
369.Li ,
370.Cx \'
371.Cx
fd315f2c 372They may also be separated by a semicolon
b5dc1377
CL
373.Cx `
374.Li ;
375.Cx \'
376.Cx
377In this case the current line
378.Cx `
379.Li .
380.Cx \'
381.Cx
382.Nm .
383is set to
fd315f2c
KM
384the previous address before the next address is interpreted.
385This feature can be used to determine the starting
b5dc1377
CL
386line for forward and backward searches
387.Cx \&(`
388.Li /
389.Cx \'
390.Cx
391.Cx `
392.Li ?
393.Cx \').
394.Cx
fd315f2c
KM
395The second address of any two-address sequence
396must correspond to a line following the line corresponding to the first address.
b5dc1377
CL
397The special form
398.Cx `
399.Li \&%
400.Cx \'
401.Cx
402is an abbreviation for the address pair
403.Cx `
404.Li 1,$
405.Cx \'.
406.Cx
407.Pp
fd315f2c 408In the following list of
b5dc1377 409.Nm ed
fd315f2c
KM
410commands, the default addresses
411are shown in parentheses.
412The parentheses are not part of
413the address, but are used to show that the given addresses are
414the default.
b5dc1377 415.Pp
fd315f2c
KM
416As mentioned, it is generally illegal for more than one
417command to appear on a line.
418However, most commands may be suffixed by `p'
419or by `l', in which case
420the current line is either
421printed or listed respectively
422in the way discussed below.
423Commands may also be suffixed by `n',
424meaning the output of the command is to
425be line numbered.
426These suffixes may be combined in any order.
b5dc1377
CL
427.Tw Ds
428.Tp Cx \&(
429.Ic .
430.Cx \&)
431.Ic a
432.Cx
433.Tp <text>
434.Tp Ic \&.
fd315f2c
KM
435The append command reads the given text
436and appends it after the addressed line.
b5dc1377
CL
437.Sq Ic \&.
438is left
fd315f2c
KM
439on the last line input, if there
440were any, otherwise at the addressed line.
441Address `0' is legal for this command; text is placed
442at the beginning of the buffer.
b5dc1377
CL
443.Tp Cx \&(
444.Ic \&.
445.Cx \&,
446.Ic \&.
447.Cx \&)
448.Ic \&c
449.Cx
450.Tp <text>
451.Tp Ic \&.
fd315f2c
KM
452The change
453command deletes the addressed lines, then accepts input
454text which replaces these lines.
b5dc1377
CL
455.Sq Ic \&.
456is left at the last line input; if there were none,
fd315f2c 457it is left at the line preceding the deleted lines.
b5dc1377
CL
458.Tp Cx \&(
459.Ic \&.
460.Cx \&,
461.Ic \&.
462.Cx \&)
463.Ic \&d
464.Cx
fd315f2c
KM
465The delete command deletes the addressed lines from the buffer.
466The line originally after the last line deleted becomes the current line;
467if the lines deleted were originally at the end,
468the new last line becomes the current line.
b5dc1377
CL
469.Tp Cx Ic \&e
470.Cx \&\ \&
471.Ar filename
472.Cx
fd315f2c
KM
473The edit
474command causes the entire contents of the buffer to be deleted,
475and then the named file to be read in.
b5dc1377
CL
476.Sq Ic \&.
477is set to the last line of the buffer.
fd315f2c 478The number of characters read is typed.
b5dc1377
CL
479.Ar filename
480is remembered for possible use as a default file name
fd315f2c 481in a subsequent
b5dc1377 482.Ic r
fd315f2c 483or
b5dc1377 484.Ic w
fd315f2c 485command.
b5dc1377
CL
486If
487.Ar filename
488is missing, the remembered name is used.
489.Tp Cx Ic E
490.Cx \&\ \&
491.Ar filename
492.Cx
fd315f2c 493This command is the same as
b5dc1377 494.Ic e ,
fd315f2c 495except that no diagnostic results when no
b5dc1377 496.Ic w
fd315f2c 497has been given since the last buffer alteration.
b5dc1377
CL
498.Tp Cx Ic f
499.Cx \&\ \&
500.Ar filename
501.Cx
fd315f2c 502The filename command prints the currently remembered file name.
b5dc1377
CL
503If
504.Ar filename
505is given,
506the currently remembered file name is changed to
507.Ar filename .
508.Tp Cx \&(
509.Ic \&1
510.Cx \&,
511.Ic \&$
512.Cx \&)
513.Ic \&g
514.Ar/regular expression/command list
515.Cx
fd315f2c
KM
516In the global
517command, the first step is to mark every line which matches
518the given regular expression.
519Then for every such line, the
b5dc1377
CL
520given command list is executed with
521.Cx `
522.Ic \&.
523.Cx \'
524.Cx
525initially set to that line.
fd315f2c
KM
526A single command or the first of multiple commands
527appears on the same line with the global command.
b5dc1377
CL
528All lines of a multi-line list except the last line must be ended with
529.Cx `
530.Ic \&\e
531.Cx \'.
532.Cx
533.Ic A ,
534.Ic i,
fd315f2c 535and
b5dc1377 536.Ic c
fd315f2c 537commands and associated input are permitted;
b5dc1377
CL
538the
539.Cx `
540.Ic \&.
541.Cx \'
542.Cx
543terminating input mode may be omitted if it would be on the
fd315f2c
KM
544last line of the command list.
545The commands
b5dc1377 546.Ic g
fd315f2c 547and
b5dc1377 548.Ic v
fd315f2c 549are not permitted in the command list.
b5dc1377
CL
550.Tp Cx \&(
551.Ic .
552.Cx \&)
553.Ic i
554.Cx
555.Tp <text>
556.Tp Ic \&.
fd315f2c 557This command inserts the given text before the addressed line.
b5dc1377
CL
558.Cx `
559.Ic \&.
560.Cx \'
561.Cx
562is left at the last line input, or, if there were none,
fd315f2c
KM
563at the line before the addressed line.
564This command differs from the
b5dc1377 565.Ic a
fd315f2c
KM
566command only in the placement of the
567text.
b5dc1377
CL
568.Tp Cx \&(
569.Ic \&.
570.Cx \&,
571.Ic \&.+1
572.Cx \&)
573.Ic \&j
574.Cx
fd315f2c
KM
575This command joins the addressed lines into a single line;
576intermediate newlines simply disappear.
b5dc1377
CL
577.Cx `
578.Ic \&.
579.Cx \'
580.Cx
581is left at the resulting line.
582.Tp Cx \&(
583.Ic .
584.Cx \&)
585.Ic k
586.Ar x
587.Cx
fd315f2c
KM
588The mark command marks the addressed line with
589name
b5dc1377 590.Ar x ,
fd315f2c 591which must be a lower-case letter.
b5dc1377
CL
592The address form
593.Cx `\(fm
594.Ar x
595.Cx \'
596.Cx
597then addresses this line.
598.Tp Cx \&(
599.Ic \&.
600.Cx \&,
601.Ic \&.
602.Cx \&)
603.Ic \&l
604.Cx
fd315f2c
KM
605The list command
606prints the addressed lines in an unambiguous way:
607non-graphic characters are
608printed in two-digit octal,
609and long lines are folded.
610The
b5dc1377 611.Ar l
fd315f2c
KM
612command may be placed on the same line after any non-i/o
613command.
b5dc1377
CL
614.Tp Cx \&(
615.Ic \&.
616.Cx \&,
617.Ic \&.
618.Cx \&)
619.Ic \&m
620.Ar a
621.Cx
fd315f2c
KM
622The move command repositions the addressed lines after the line
623addressed by
b5dc1377 624.Ar a .
fd315f2c 625The last of the moved lines becomes the current line.
b5dc1377
CL
626.Tp Cx \&(
627.Ic \&.
628.Cx \&,
629.Ic \&.
630.Cx \&)
631.Ic \&p
632.Cx
fd315f2c 633The print command prints the addressed lines.
b5dc1377
CL
634.Cx `
635.Ic \&.
636.Cx \'
637.Cx
fd315f2c
KM
638is left at the last line printed.
639The
b5dc1377 640.Ic p
fd315f2c
KM
641command
642may
643be placed on the same line after any non-i/o command.
b5dc1377
CL
644.Tp Cx \&(
645.Ic \&.
646.Cx \&,
647.Ic \&.
648.Cx \&)
649.Ic \&P
650.Cx
fd315f2c 651This command is a synonym for
b5dc1377
CL
652.Ic p .
653.Tp Ic q
fd315f2c 654The quit command causes
b5dc1377 655.Nm ed
fd315f2c
KM
656to exit.
657No automatic write
658of a file is done.
b5dc1377 659.Tp Ic Q
fd315f2c 660This command is the same as
b5dc1377 661.Ic q ,
fd315f2c 662except that no diagnostic results when no
b5dc1377 663.Ic w
fd315f2c 664has been given since the last buffer alteration.
b5dc1377
CL
665.Tp Cx \&(
666.Ic $
667.Cx \&)
668.Ic r
669.Cx \&\ \&
670.Ar filename
671.Cx
fd315f2c
KM
672The read command
673reads in the given file after the addressed line.
674If no file name is given,
675the remembered file name, if any, is used
676(see
b5dc1377 677.Ic e
fd315f2c 678and
b5dc1377
CL
679.Ic f
680commands).
fd315f2c
KM
681The file name is remembered if there was no
682remembered file name already.
683Address `0' is legal for
b5dc1377 684.Ic r
fd315f2c
KM
685and causes the
686file to be read at the beginning of the buffer.
687If the read is successful, the number of characters
688read is typed.
b5dc1377
CL
689.Cx `
690.Ic \&.
691.Cx \'
692.Cx
693is left at the last line read in from the file.
694.Tp Cx \&(
695.Ic \&.
696.Cx \&,
697.Ic \&.
698.Cx \&)
699.Ic \&s
700.Ar/regular expression/replacement/
701.Cx \&\tor
702.Cx
703.Tp Cx \&(
704.Ic \&.
705.Cx \&,
706.Ic \&.
707.Cx \&)
708.Ic \&s
709.Ar/regular expression/replacement/
710.Ic \&g
711.Cx
fd315f2c
KM
712The substitute command searches each addressed
713line for an occurrence of the specified regular expression.
714On each line in which a match is found,
715all matched strings are replaced by the replacement specified,
b5dc1377
CL
716if the global replacement indicator
717.Ic \&g
718appears after the command.
fd315f2c
KM
719If the global indicator does not appear, only the first occurrence
720of the matched string is replaced.
721It is an error for the substitution to fail on all addressed lines.
722Any punctuation character
b5dc1377
CL
723may be used instead of
724.Cx `
725.Ic \&/
726.Cx \'
727.Cx
728to delimit the regular expression
fd315f2c 729and the replacement.
b5dc1377
CL
730.Cx `
731.Ic \&.
732.Cx \'
733.Cx
734is left at the last line substituted.
735An ampersand
736.Cx `
737.Ic \&&
738.Cx \'
739.Cx
740appearing in the replacement
fd315f2c 741is replaced by the string matching the regular expression.
b5dc1377
CL
742The special meaning of
743.Cx `
744.Ic \&&
745.Cx \'
746.Cx
747in this context may be
748suppressed by preceding it by
749.Cx `
750.Ic \&\e
751.Cx \'.
752.Cx
fd315f2c 753The characters
b5dc1377
CL
754.Cx `
755.Ic \&\e
756.Ar n
757.Cx \'
758.Cx
fd315f2c 759where
b5dc1377 760.Ar n
fd315f2c
KM
761is a digit,
762are replaced by the text matched by the
b5dc1377
CL
763.Cx Ar n
764.Cx \'th
765.Cx
fd315f2c 766regular subexpression
b5dc1377
CL
767enclosed between
768.Cx `
769.Ic \&\e\&(
770.Cx \'.
771.Cx
772and
773.Cx `
774.Ic \&\e\&)
775.Cx \'.
776.Cx
fd315f2c
KM
777When
778nested, parenthesized subexpressions
779are present,
b5dc1377
CL
780.Ar n
781is determined by counting occurrences of
782.Cx `
783.Ic \&\e\&(
784.Cx \'.
785.Cx
786starting from the left.
fd315f2c
KM
787Lines may be split by substituting new-line characters into them.
788The new-line in the
789replacement string
b5dc1377
CL
790must be escaped by preceding it by
791.Cx `
792.Ic \&\e
793.Cx \'.
794.Cx
fd315f2c 795One or two trailing delimiters may be omitted,
b5dc1377
CL
796implying the
797.Ic p
798suffix.
799The special form
800.Ic s
801followed by
802.Ar no
fd315f2c
KM
803delimiters
804repeats the most recent substitute command
805on the addressed lines.
b5dc1377
CL
806The
807.Ic s
808may be followed by the letters
809.Ic r
fd315f2c
KM
810(use the most recent regular expression for the
811left hand side, instead of the most recent
812left hand side of a substitute command),
b5dc1377 813.Ic p
fd315f2c 814(complement the setting of the
b5dc1377 815.Ic p
fd315f2c 816suffix from the previous substitution), or
b5dc1377 817.Ic g
fd315f2c 818(complement the setting of the
b5dc1377 819.Ic g
fd315f2c
KM
820suffix).
821These letters may be combined in any order.
b5dc1377
CL
822.Tp Cx \&(
823.Ic \&.
824.Cx \&,
825.Ic \&.
826.Cx \&)
827.Ic \&t
828.Ar a
829.Cx
fd315f2c 830This command acts just like the
b5dc1377 831.Ic m
fd315f2c
KM
832command, except that a copy of the addressed lines is placed
833after address
b5dc1377 834.Ad a
fd315f2c 835(which may be 0).
b5dc1377
CL
836.Cx `
837.Ic \&.
838.Cx \'
839.Cx
840is left on the last line of the copy.
841.Tp Cx \&(
842.Ic \&.
843.Cx \&,
844.Ic \&.
845.Cx \&)
846.Ic \&u
847.Cx
fd315f2c
KM
848The undo command restores the buffer to it's state
849before the most recent buffer modifying command.
850The current line is also restored.
851Buffer modifying commands are
b5dc1377 852.Ic a , c , d , g , i , k , m , r , s , t ,
fd315f2c 853and
b5dc1377 854.Ic v .
fd315f2c 855For purposes of undo,
b5dc1377 856.Ic g
fd315f2c 857and
b5dc1377 858.Ic v
fd315f2c
KM
859are considered to be a single buffer modifying command.
860Undo is its own inverse.
fd315f2c 861When
b5dc1377 862.Nm ed
fd315f2c
KM
863runs out of memory
864(at about 8000 lines on any 16 bit mini-computer
865such as the PDP-11)
866This full undo is not possible, and
b5dc1377 867.Ic u
fd315f2c
KM
868can only undo the effect of the most recent
869substitute on the current line.
870This restricted undo also applies to editor scripts
871when
b5dc1377 872.Nm ed
fd315f2c 873is invoked with the
b5dc1377 874.Fl
fd315f2c 875option.
b5dc1377
CL
876.Tp Cx \&(
877.Ic \&1
878.Cx \&,
879.Ic \&$
880.Cx \&)
881.Ic \&v
882.Ar/regular expression/command list
883.Cx
fd315f2c 884This command is the same as the global command
b5dc1377 885.Ic g
fd315f2c 886except that the command list is executed
b5dc1377
CL
887.Ic g
888with
889.Cx `
890.Ic \&.
891.Cx \'
892.Cx
893initially set to every line
894.Em except
fd315f2c
KM
895those
896matching the regular expression.
b5dc1377
CL
897(1, $)\w filename
898.Tp Cx \&(
899.Ic \&1
900.Cx \&,
901.Ic \&$
902.Cx \&)
903.Ic \&w
904.Cx \&\ \&
905.Ar filename
906.Cx
fd315f2c
KM
907The write command writes the addressed lines onto
908the given file.
909If the file does not exist,
910it is created.
b5dc1377 911The file name is remembered if there was no
fd315f2c
KM
912remembered file name already.
913If no file name is given,
914the remembered file name, if any, is used
915(see
b5dc1377 916.Ic e
fd315f2c 917and
b5dc1377
CL
918.Ic f
919commands).
920.Cx `
921.Ic \&.
922.Cx \'
923.Cx
924is unchanged.
fd315f2c
KM
925If the command is successful, the number of characters written is
926printed.
b5dc1377
CL
927.Tp Cx \&(
928.Ic \&1
929.Cx \&,
930.Ic \&$
931.Cx \&)
932.Ic \&W
933.Cx \&\ \&
934.Ar filename
935.Cx
fd315f2c 936This command is the same as
b5dc1377 937.Ic w ,
fd315f2c 938except that the addressed lines are appended to the file.
b5dc1377
CL
939.Tp Cx \&(
940.Ic \&1
941.Cx \&,
942.Ic \&$
943.Cx \&)
944.Ic \&wq
945.Cx \&\ \&
946.Ar filename
947.Cx
fd315f2c 948This command is the same as
b5dc1377 949.Ic w
fd315f2c 950except that afterwards a
b5dc1377 951.Ic q
fd315f2c
KM
952command is done,
953exiting the editor
954after the file is written.
b5dc1377
CL
955.Ic \&1
956.Ic \&+1
957.Cx \&)
958.Ic \&z
959.Cx \&\ \ \&or,
960.Cx
961.Tp Cx \&(
962.Ic \&1
963.Ic \&+1
964.Cx \&)
965.Ic \&z
966.Ar n
967.Cx
fd315f2c
KM
968This command scrolls through the buffer starting at the addressed line.
96922 (or
b5dc1377 970.Ar n ,
fd315f2c
KM
971if given)
972lines are printed.
973The last line printed becomes the current line.
974The value
b5dc1377 975.Ar n
fd315f2c
KM
976is sticky, in that it becomes the default for
977future
b5dc1377 978.Ic z
fd315f2c 979commands.
b5dc1377
CL
980.Tp Cx \&(
981.Ic \&$
982.Cx \&)
983.Ic \&=
984.Cx
fd315f2c 985The line number of the addressed line is typed.
b5dc1377
CL
986.Cx `
987.Ic \&.
988.Cx \'
989.Cx
990is unchanged by this command.
991.Tp Cx Ic \&!
992.Cx <shell\ command>
993.Cx
fd315f2c
KM
994The remainder of the line after the `!' is sent
995to
b5dc1377 996.Xr sh 1
fd315f2c 997to be interpreted as a command.
b5dc1377
CL
998.Cx `
999.Ic \&.
1000.Cx \'
1001.Cx
fd315f2c 1002is unchanged.
b5dc1377
CL
1003.Tp Cx \&(
1004.Ic \&.+1
1005.Cx \&,
1006.Ic \&.+1
1007.Cx \&)
1008.Cx <newline>
1009.Cx
fd315f2c 1010An address alone on a line causes the addressed line to be printed.
b5dc1377
CL
1011A blank line alone is equivalent to
1012.Ic .+1
1013it is useful
fd315f2c
KM
1014for stepping through text.
1015If two addresses are present with no
1016intervening semicolon,
b5dc1377 1017.Nm ed
fd315f2c
KM
1018prints the range of lines.
1019If they are separated by a semicolon,
1020the second line is printed.
b5dc1377
CL
1021.Tp
1022.Pp
1023If an interrupt signal (ASCII DEL) is sent,
1024.Nm ed
1025prints
1026.Sq Li ?interrupted
fd315f2c 1027and returns to its command level.
b5dc1377 1028.Pp
fd315f2c
KM
1029Some size limitations:
1030512 characters per line,
1031256 characters per global command list,
103264 characters per file name,
1033and, on mini computers,
1034128K characters in the temporary file.
1035The limit on the number of lines depends on the amount of core:
1036each line takes 2 words.
b5dc1377 1037.Pp
fd315f2c 1038When reading a file,
b5dc1377 1039.Nm ed
fd315f2c
KM
1040discards ASCII NUL characters
1041and all characters after the last newline.
1042It refuses to read files containing non-ASCII characters.
b5dc1377
CL
1043.Sh FILES
1044.Dw edhup
1045.Di L
1046.Dp Pa /tmp/e*
1047.Dp Pa edhup
1048work is saved here if terminal hangs up
1049.Dp
1050.Sh SEE ALSO
1051.Xr ex 1 ,
1052.Xr sed 1 ,
1053.Xr crypt 1
fd315f2c 1054.br
fd315f2c 1055B. W. Kernighan,
b5dc1377 1056.Em A Tutorial Introduction to the ED Text Editor
fd315f2c
KM
1057.br
1058B. W. Kernighan,
b5dc1377
CL
1059.Em Ar Advanced editing on UNIX
1060.Sh HISTORY
1061The
1062.Nm ed
1063command appeared in Version 6 AT&T UNIX.
1064.Sh DIAGNOSTICS
1065.Sq Li name
1066for inaccessible file;
1067.Sq Li ?self-explanatory message
fd315f2c 1068for other errors.
b5dc1377 1069.Pp
fd315f2c
KM
1070To protect against throwing away valuable work,
1071a
b5dc1377 1072.Ic q
fd315f2c 1073or
b5dc1377 1074.Ic e
fd315f2c 1075command is considered to be in error, unless a
b5dc1377 1076.Ic w
fd315f2c
KM
1077has occurred since the last buffer change.
1078A second
b5dc1377 1079.Ic q
fd315f2c 1080or
b5dc1377 1081.Ic e
fd315f2c 1082will be obeyed regardless.
b5dc1377
CL
1083.Sh BUGS
1084The
1085.Ic l
1086command mishandles
1087.Li DEL .
fd315f2c
KM
1088.br
1089The
b5dc1377 1090.Ic undo
fd315f2c 1091command causes marks to be lost on affected lines.