xref: /original-bsd/bin/ed/ed.1 (revision edc2ab72)
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.8.1.1 (Berkeley) 08/19/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 Ar file
18.Sh DESCRIPTION
19.Nm \&Ed
20once was
21the standard text editor in the early days of
22.Ux .
23While its life as a line editor is dated,
24.Nm \&ed
25still resides on systems produced
26by a variety of vendors for good reason.
27Because of its
28small size and simplicity,
29.Nm \&ed,
30is found on the root partition
31.Pq Pa /bin/ed
32for emergency editing when normal system
33reboots fail or single user tasks
34are performed and
35.Pa /usr
36is not mounted.
37.Nm \&Ed
38is also useful for script editing tasks,
39and although there is nothing which can
40be done by
41.Nm \&ed
42that cannot be performed by
43.Xr sed 1 ,
44.Xr ex 1
45or
46.Em Gnu Emacs ,
47.Nm \&ed
48remains the simplest and easiest to use.
49.Pp
50If a
51.Ar file
52argument is given,
53.Nm \&ed
54simulates an
55.Cm \&e
56command (see below) on the named file; that is to say,
57the file is read into
58.Nm \&ed Ns 's
59buffer so that it can be edited.
60.Pp
61Available options:
62.Bl -tag -width Ds
63.It Fl
64Suppresses the printing
65of explanatory output
66and should be used
67when the standard input is
68an editor script.
69.El
70.Pp
71.Nm \&Ed
72operates on a copy of any file it is editing; changes made
73in the copy have no effect on the file until a
74.Cm \&w
75(write) command is given.
76The copy of the text being edited resides
77in a temporary file called the
78.Ar buffer  .
79.Pp
80Commands to
81.Nm \&ed
82have a simple and regular structure: zero or
83more
84.Ad addresses
85followed by a single character
86.Cm command ,
87possibly
88followed by parameters to the command.
89These addresses specify one or more lines in the buffer.
90Missing addresses are supplied by default.
91.Pp
92In general, only one command may appear on a line.
93Certain commands allow the
94addition of text to the buffer.
95While
96.Nm \&ed
97is accepting text, it is said
98to be in
99.Ar input mode .
100In this mode, no commands are recognized;
101all input is merely collected.
102Input mode is left by typing a period
103.Sq Ad \&.
104alone at the
105beginning of a line.
106.Pp
107.Nm \&Ed
108supports a limited form of
109.Ar regular expression
110notation.
111A regular expression specifies
112a set of strings of characters.
113A member of this set of strings is said to be
114.Ar matched
115by the regular expression.
116In the following specification for regular expressions
117the word `character' means any character but newline.
118.Bl -enum
119.It
120Any character except a special character
121matches itself.
122Special characters are
123the regular expression delimiter plus
124.Ql \e\&[.
125and sometimes
126.Ql ^*$ .
127.It
128A
129.Sq Ql \&.
130matches any character.
131.It
132A
133.Ql \e
134followed by any character except a digit or
135.Li (\)
136matches that character.
137.It
138A nonempty string
139.Ar \&s
140bracketed
141.Bq Ar \&s
142(or
143.Bq Ar \&^s )
144matches any character in (or not in)
145.Ar \&s.
146In
147.Ar \&s,
148.Ql \e
149has no special meaning, and
150may only appear as
151the first letter.
152A substring
153.Ar \&a\-b ,
154with
155.Ar \&a
156and
157.Ar \&b
158in ascending
159.Tn ASCII
160order, stands for the inclusive
161range of
162.Tn ASCII
163characters.
164.It
165A regular expression of form 1\-4 followed by
166.Ql \&*
167matches a sequence of
1680 or more matches of the regular expression.
169.It
170A regular expression,
171.Ar \&x ,
172of form 1\-8, bracketed
173.No \e( Ar \&x Ns \e)
174matches what
175.Ar \&x
176matches.
177.It
178A \e followed by a digit
179.Ar \&n
180matches a copy of the string that the
181bracketed regular expression beginning with the
182.Ar \&n Ns \&th
183.No \e(
184matched.
185.It
186A regular expression of form 1\-8,
187.Ar \&x ,
188followed by a regular expression of form 1\-7,
189.Ar \&y
190matches a match for
191.Ar \&x
192followed by a match for
193.Ar \&y ,
194with the
195.Ar \&x
196match being as long as possible while still permitting a
197.Ar \&y
198match.
199.It
200A regular expression of form 1\-8 preceded by
201.Sq Li ^
202(or followed by
203.Sq Li $ ) ,
204is constrained to matches that
205begin at the left (or end at the right) end of a line.
206.It
207A regular expression of form 1\-9 picks out the
208longest among the leftmost matches in a line.
209.It
210An empty regular expression stands for a copy of the
211last regular expression encountered.
212.El
213.Pp
214Regular expressions are used in addresses to specify
215lines and in one command
216(see
217.Ar \&s
218below)
219to specify a portion of a line which is to be replaced.
220If it is desired to use one of
221the regular expression metacharacters as an ordinary
222character, that character may be preceded by
223.Sq Li \e .
224This also applies to the character bounding the regular
225expression (often
226.Sq Li \&/ )
227and to
228.Sq Li \e
229itself.
230.Pp
231To understand addressing in
232.Nm \&ed
233it is necessary to know that at any time there is a
234.Ar current line.
235Generally speaking, the current line is
236the last line affected by a command; however,
237the exact effect on the current line
238is discussed under the description of
239the command.
240Addresses are constructed as follows.
241.Bl -enum
242.It
243The character
244.Sq Ad \&.
245addresses the current line.
246.It
247The character
248.Sq Ad \&$
249addresses the last line of the buffer.
250.It
251A decimal number
252.Ar \&n
253addresses the
254.Ar \&n Ns \&th
255line of the buffer.
256.It
257.Sq \(fm Ns Ar \&x
258addresses the line marked with the name
259.Ar \&x  ,
260which must be a lower-case letter.
261Lines are marked with the
262.Ar \&k
263command described below.
264.It
265A regular expression enclosed in slashes
266.Ql \&/
267addresses
268the line found by searching forward from the current line
269and stopping at the first line containing a
270string that matches the regular expression.
271If necessary the search wraps around to the beginning of the
272buffer.
273.It
274A regular expression enclosed in queries
275.Ql ?
276addresses
277the line found by searching backward from the current line
278and stopping at the first line containing
279a string that matches the regular expression.
280If necessary
281the search wraps around to the end of the buffer.
282.It
283An address followed by a plus sign
284.Ql \&+
285or a minus sign
286.Ql \-
287followed by a decimal number specifies that address plus
288(resp. minus) the indicated number of lines.
289The plus sign may be omitted.
290.It
291If an address begins with
292.Ql \&+
293or
294.Ql \-
295the addition or subtraction is taken with respect to the current line;
296e.g.
297.Ql \-5
298is understood to mean
299.Ql .\-5 .
300.It
301If an address ends with
302.Ql \&+
303or
304.Ql \&\-
305then 1 is added (resp. subtracted).
306As a consequence of this rule and rule 8,
307the address
308.Ql \&\-
309refers to the line before the current line.
310Moreover,
311trailing
312.Ql \&+
313and
314.Ql \&\-
315characters
316have cumulative effect, so
317.Ql \&\-\-
318refers to the current
319line less 2.
320.It
321To maintain compatibility with earlier versions of the editor,
322the character
323.Ql \&^
324in addresses is
325equivalent to
326.Ql \&\-
327.El
328.Pp
329Commands may require zero, one, or two addresses.
330Commands which require no addresses regard the presence
331of an address as an error.
332Commands which accept one or two addresses
333assume default addresses when insufficient are given.
334If more addresses are given than such a command requires,
335the last one or two (depending on what is accepted) are used.
336.Pp
337Addresses are separated from each other typically by a comma
338.Ql \&,
339They may also be separated by a semicolon
340.Ql \&;
341In this case the current line
342.Ql \&.
343is set to
344the previous address before the next address is interpreted.
345This feature can be used to determine the starting
346line for forward and backward searches
347.Pf ( Ql \&/ ,
348.Ql \&? )
349The second address of any two-address sequence
350must correspond to a line following the line corresponding to the first address.
351The special form
352.Ql \&%
353is an abbreviation for the address pair
354.Ql \&1,$ .
355.Pp
356In the following list of
357.Nm \&ed
358commands, the default addresses
359are shown in parentheses.
360The parentheses are not part of
361the address, but are used to show that the given addresses are
362the default.
363.Pp
364As mentioned, it is generally illegal for more than one
365command to appear on a line.
366However, most commands may be suffixed by
367.Ql \&p
368or by
369.Ql \&l ,
370in which case
371the current line is either
372printed or listed respectively
373in the way discussed below.
374Commands may also be suffixed by
375.Ql \&n ,
376meaning the output of the command is to
377be line numbered.
378These suffixes may be combined in any order.
379.Pp
380.Bl -tag -width four -compact
381.It Xo
382.Po Ad \&.
383.Pc Ns Cm \&a
384.Xc
385.It <text>
386.It Cm \&.
387.br
388The append command reads the given text
389and appends it after the addressed line.
390.Sq Ad \&.
391is left
392on the last line input, if there
393were any, otherwise at the addressed line.
394Address `0' is legal for this command; text is placed
395at the beginning of the buffer.
396.Pp
397.It Xo
398.Po Ad \&. , Ns Ad \&.
399.Pc Ns Cm \&c
400.Xc
401.It <text>
402.It Cm \&.
403.br
404The change
405command deletes the addressed lines, then accepts input
406text which replaces these lines.
407.Sq Ad \&.
408is left at the last line input; if there were none,
409it is left at the line preceding the deleted lines.
410.Pp
411.It Xo
412.Po Ad \&. , Ns Ad \&.
413.Pc Ns Cm \&d
414.Xc
415.It <text>
416.It Cm \&.
417.br
418The delete command deletes the addressed lines from the buffer.
419The line originally after the last line deleted becomes the current line;
420if the lines deleted were originally at the end,
421the new last line becomes the current line.
422.Pp
423.It Cm \&e Ar filename
424The edit
425command causes the entire contents of the buffer to be deleted,
426and then the named file to be read in.
427.Sq Ad \&.
428is set to the last line of the buffer.
429The number of characters read is typed.
430.Ar filename
431is remembered for possible use as a default file name
432in a subsequent
433.Cm \&r
434or
435.Cm \&w
436command.
437If
438.Ar filename
439is missing, the remembered name is used.
440.Pp
441.It Cm \&E Ar filename
442This command is the same as
443.Cm \&e ,
444except that no diagnostic results when no
445.Cm \&w
446has been given since the last buffer alteration.
447.Pp
448.It Cm \&f Ar filename
449The filename command prints the currently remembered file name.
450If
451.Ar filename
452is given,
453the currently remembered file name is changed to
454.Ar filename .
455.Sm off
456.Pp
457.It Xo
458.Po Ad \&1 , Ns Ad \&$
459.Pc Cm \&g Ar "/regular\ expression/" Cm "command\ list"
460.Xc
461.Sm on
462In the global
463command, the first step is to mark every line which matches
464the given regular expression.
465Then for every such line, the
466given command list is executed with
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 Xo
811.Po Ad \&.+1
812.Pc Ns Cm \&z
813.No "	or,"
814.Xc
815.It Xo
816.Po Ad \&.+1
817.Pc Ns Cm \&z Ns Ar \&n
818.Xc
819This command scrolls through the buffer starting at the addressed line.
82022 (or
821.Ar \&n ,
822if given)
823lines are printed.
824The last line printed becomes the current line.
825The value
826.Ar \&n
827is sticky, in that it becomes the default for
828future
829.Cm \&z
830commands.
831.Pp
832.It Xo
833.Po Ad \&$
834.Pc Ns Cm \&=
835.Xc
836The line number of the addressed line is typed.
837.Sq Ad \&.
838is unchanged by this command.
839.Pp
840.It Xo
841.Ad \&! Ns Aq shell\ command
842.Xc
843The remainder of the line after the
844.Ql Cm \&!
845is sent
846to
847.Xr sh 1
848to be interpreted as a command.
849.Sq Ad \&.
850is unchanged.
851.Pp
852.It Xo
853.Po Ad \&.+1 , Ns Ad \&.+1
854.Pc Ns Aq newline
855.Xc
856An address alone on a line causes the addressed line to be printed.
857A blank line alone is equivalent to
858.Ad .+1
859it is useful
860for stepping through text.
861If two addresses are present with no
862intervening semicolon,
863.Nm \&ed
864prints the range of lines.
865If they are separated by a semicolon,
866the second line is printed.
867.El
868.Pp
869If an interrupt signal
870.Pq Tn ASCII DEL
871is sent,
872.Nm \&ed
873prints
874.Sq Li ?interrupted
875and returns to its command level.
876.Pp
877Some size limitations:
878512 characters per line,
879256 characters per global command list,
88064 characters per file name,
881and, on mini computers,
882128K characters in the temporary file.
883The limit on the number of lines depends on the amount of core:
884each line takes 2 words.
885.Pp
886When reading a file,
887.Nm \&ed
888discards
889.Tn ASCII NUL
890characters
891and all characters after the last newline.
892It refuses to read files containing
893.Ns non- Tn ASCII
894characters.
895.Sh FILES
896.Bl -tag -compact -width "/tmp/ed*"
897.It Pa /tmp/e*
898.It Pa edhup
899work is saved here if terminal hangs up
900.El
901.Sh SEE ALSO
902.Xr \&ex 1 ,
903.Xr sed 1 ,
904.Xr crypt 1
905.br
906B. W. Kernighan,
907.Em A Tutorial Introduction to the ED Text Editor
908.br
909B. W. Kernighan,
910.Em Ar Advanced editing on UNIX
911.Sh HISTORY
912The
913.Nm \&ed
914command appeared in
915.At 6
916.Sh DIAGNOSTICS
917.Sq Li name
918for inaccessible file;
919.Sq Li ?self-explanatory message
920for other errors.
921.Pp
922To protect against throwing away valuable work,
923a
924.Cm \&q
925or
926.Cm \&e
927command is considered to be in error, unless a
928.Cm \&w
929has occurred since the last buffer change.
930A second
931.Cm \&q
932or
933.Cm \&e
934will be obeyed regardless.
935.Sh BUGS
936The
937.Cm \&l
938command mishandles
939.Li DEL .
940.br
941The
942.Cm \&undo
943command causes marks to be lost on affected lines.
944.br
945The special treatment of hangups only works on
946.Ux .
947