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