xref: /original-bsd/bin/ed/ed.1 (revision deff14a8)
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.1.1 (Berkeley) 06/27/94
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 ones 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
467.Sq Ad \&.
468initially set to that line.
469A single command or the first of multiple commands
470appears on the same line with the global command.
471All lines of a multi-line list except the last line must be ended with
472.Sq Cm \&\e .
473.Cm \&A ,
474.Cm \&i ,
475and
476.Cm \&c
477commands and associated input are permitted;
478the
479.Sq Ad \&.
480terminating input mode may be omitted if it would be on the
481last line of the command list.
482The commands
483.Cm \&g
484and
485.Cm \&v
486are not permitted in the command list.
487.Pp
488.It Xo
489.Po Ad \&.
490.Pc Ns Cm \&i
491.Xc
492.It <text>
493.It Cm \&.
494.br
495This command inserts the given text before the addressed line.
496.Sq Ad \&.
497is left at the last line input, or, if there were none,
498at the line before the addressed line.
499This command differs from the
500.Cm \&a
501command only in the placement of the
502text.
503.Pp
504.It Xo
505.Po Ad \&. , Ns Ad \&.+1
506.Pc Ns Cm \&j
507.Xc
508This command joins the addressed lines into a single line;
509intermediate newlines simply disappear.
510.Sq Ad \&.
511is left at the resulting line.
512.Pp
513.It Xo
514.Po Ad \&.
515.Pc Ns Cm k Ns Ar \&x
516.Xc
517The mark command marks the addressed line with
518name
519.Ar \&x ,
520which must be a lower-case letter.
521The address form
522.Ar \(fmx
523then addresses this line.
524.Pp
525.It Xo
526.Po Ad \&. , Ns Ad \&.
527.Pc Ns Cm \&l
528.Xc
529The list command
530prints the addressed lines in an unambiguous way:
531non-graphic characters are
532printed in two-digit octal,
533and long lines are folded.
534The
535.Ar \&l
536command may be placed on the same line after any non-i/o
537command.
538.Pp
539.It Xo
540.Po Ad \&. , Ns Ad \&.
541.Pc Ns Cm \&m Ns Ar \&a
542.Xc
543The move command repositions the addressed lines after the line
544addressed by
545.Ql Ad \&a  .
546The last of the moved lines becomes the current line.
547.Pp
548.It Xo
549.Po Ad \&. , Ns Ad \&.
550.Pc Ns Cm \&p
551.Xc
552The print command prints the addressed lines.
553.Sq Ad \&.
554is left at the last line printed.
555The
556.Cm \&p
557command
558may
559be placed on the same line after any non-i/o command.
560.Pp
561.It Xo
562.Po Ad \&. , Ns Ad \&.
563.Pc Ns Cm \&P
564.Xc
565This command is a synonym for
566.Cm \&p .
567.Pp
568.It Cm \&q
569The quit command causes
570.Nm \&ed
571to exit.
572No automatic write
573of a file is done.
574.Pp
575.It Cm \&Q
576This command is the same as
577.Cm \&q ,
578except that no diagnostic results when no
579.Cm \&w
580has been given since the last buffer alteration.
581.Pp
582.It Xo
583.Po Ad \&$ , Ns Ad \&.
584.Pc Ns Cm \&r Ar filename
585.Xc
586The read command
587reads in the given file after the addressed line.
588If no file name is given,
589the remembered file name, if any, is used
590(see
591.Cm \&e
592and
593.Cm \&f
594commands).
595The file name is remembered if there was no
596remembered file name already.
597Address `0' is legal for
598.Cm \&r
599and causes the
600file to be read at the beginning of the buffer.
601If the read is successful, the number of characters
602read is typed.
603.Sq Ad \&.
604is left at the last line read in from the file.
605.Sm off
606.Pp
607.It Xo
608.Po Ad \&1 , Ns Ad \&$
609.Pc Cm \&g Ar "/regular\ expression/" Cm "replacement\ list"
610.No "	or,"
611.Xc
612.Sm on
613.Sm off
614.It Xo
615.Po Ad \&1 , Ns Ad \&$
616.Pc Cm \&g Ar "/regular\ expression/" Cm "replacement\ list/"
617.Ns Cm \&g
618.Xc
619.Sm on
620The substitute command searches each addressed
621line for an occurrence of the specified regular expression.
622On each line in which a match is found,
623all matched strings are replaced by the replacement specified,
624if the global replacement indicator
625.Cm \&g
626appears after the command.
627If the global indicator does not appear, only the first occurrence
628of the matched string is replaced.
629It is an error for the substitution to fail on all addressed lines.
630Any punctuation character
631may be used instead of
632.Sq Cm \&/
633to delimit the regular expression
634and the replacement.
635.Sq Ad \&.
636is left at the last line substituted.
637An ampersand
638.Sq Cm \&&
639appearing in the replacement
640is replaced by the string matching the regular expression.
641The special meaning of
642.Sq Cm \&&
643in this context may be
644suppressed by preceding it by
645.Sq Cm \&\e
646The characters
647.Sq Cm \&\e Ns Ar \&n
648where
649.Ar \&n
650is a digit,
651are replaced by the text matched by the
652.Ar \&n Ns
653regular subexpression
654enclosed between
655.Sq Cm \&\e\&(
656and
657.Sq Cm \&\e\&)
658When
659nested, parenthesized subexpressions
660are present,
661.Ar \&n
662is determined by counting occurrences of
663.Sq Cm \&\e\&(
664starting from the left.
665Lines may be split by substituting new-line characters into them.
666The new-line in the
667replacement string
668must be escaped by preceding it by
669.Sq Cm \&\e
670One or two trailing delimiters may be omitted,
671implying the
672.Cm \&p
673suffix.
674The special form
675.Cm \&s
676followed by
677.Ar \&no
678delimiters
679repeats the most recent substitute command
680on the addressed lines.
681The
682.Cm \&s
683may be followed by the letters
684.Cm \&r
685(use the most recent regular expression for the
686left hand side, instead of the most recent
687left hand side of a substitute command),
688.Cm \&p
689(complement the setting of the
690.Cm \&p
691suffix from the previous substitution), or
692.Cm \&g
693(complement the setting of the
694.Cm \&g
695suffix).
696These letters may be combined in any order.
697.Pp
698.It Xo
699.Po Ad \&. , Ns Ad \&.
700.Pc Ns Cm \&t Ns Ar \&a
701.Xc
702This command acts just like the
703.Cm \&m
704command, except that a copy of the addressed lines is placed
705after address
706.Ad \&a
707(which may be 0).
708.Sq Ad \&.
709is left on the last line of the copy.
710.Pp
711.It Xo
712.Po Ad \&. , Ns Ad \&.
713.Pc Ns Cm \&u
714.Xc
715The undo command restores the buffer to it's state
716before the most recent buffer modifying command.
717The current line is also restored.
718Buffer modifying commands are
719.Cm \&a , \&c , \&d , \&g ,
720.Cm \&i , \&k , \&m , \&r ,
721.Cm \&s , \&t ,
722and
723.Cm \&v .
724For purposes of undo,
725.Cm \&g
726and
727.Cm \&v
728are considered to be a single buffer modifying command.
729Undo is its own inverse.
730When
731.Nm \&ed
732runs out of memory
733(at about 8000 lines on any 16 bit mini-computer
734such as the PDP-11)
735this full undo is not possible, and
736.Cm \&u
737can only undo the effect of the most recent
738substitute on the current line.
739This restricted undo also applies to editor scripts
740when
741.Nm \&ed
742is invoked with the
743.Fl
744option.
745.Sm off
746.Pp
747.It Xo
748.Po Ad \&1 , Ns Ad \&$
749.Pc Cm \&v Ar "/regular\ expression/" Cm "command\ list"
750.Xc
751.Sm on
752This command is the same as the global command
753.Cm \&g
754except that the command list is executed
755.Cm \&g
756with
757.Sq Ad \&.
758initially set to every line
759.Em except
760those
761matching the regular expression.
762.Pp
763.It Xo
764.Po Ad \&1 , Ns Ad \&$
765.Pc Ns Cm \&w Ar filename
766.Xc
767The write command writes the addressed lines onto
768the given file.
769If the file does not exist,
770it is created.
771The file name is remembered if there was no
772remembered file name already.
773If no file name is given,
774the remembered file name, if any, is used
775(see
776.Cm \&e
777and
778.Cm \&f
779commands).
780.Sq Ad \&.
781is unchanged.
782If the command is successful, the number of characters written is
783printed.
784.Pp
785.It Xo
786.Po Ad \&1 , Ns Ad \&$
787.Pc Ns Cm \&W Ar filename
788.Xc
789This command is the same as
790.Cm \&w ,
791except that the addressed lines are appended to the file.
792.Pp
793.It Xo
794.Po Ad \&1 , Ns Ad \&$
795.Pc Ns Cm \&wq Ar filename
796.Xc
797This command is the same as
798.Cm \&w
799except that afterwards a
800.Cm \&q
801command is done,
802exiting the editor
803after the file is written.
804.Pp
805.It Xo
806.Po Ad \&.+1
807.Pc Ns Cm \&z
808.No "	or,"
809.Xc
810.It Xo
811.Po Ad \&.+1
812.Pc Ns Cm \&z Ns Ar \&n
813.Xc
814This command scrolls through the buffer starting at the addressed line.
81522 (or
816.Ar \&n ,
817if given)
818lines are printed.
819The last line printed becomes the current line.
820The value
821.Ar \&n
822is sticky, in that it becomes the default for
823future
824.Cm \&z
825commands.
826.Pp
827.It Xo
828.Po Ad \&$
829.Pc Ns Cm \&=
830.Xc
831The line number of the addressed line is typed.
832.Sq Ad \&.
833is unchanged by this command.
834.Pp
835.It Xo
836.Ad \&! Ns Aq shell\ command
837.Xc
838The remainder of the line after the
839.Ql Cm \&!
840is sent
841to
842.Xr sh 1
843to be interpreted as a command.
844.Sq Ad \&.
845is unchanged.
846.Pp
847.It Xo
848.Po Ad \&.+1 , Ns Ad \&.+1
849.Pc Ns Aq newline
850.Xc
851An address alone on a line causes the addressed line to be printed.
852A blank line alone is equivalent to
853.Ad .+1
854it is useful
855for stepping through text.
856If two addresses are present with no
857intervening semicolon,
858.Nm \&ed
859prints the range of lines.
860If they are separated by a semicolon,
861the second line is printed.
862.El
863.Pp
864If an interrupt signal
865.Pq Tn ASCII DEL
866is sent,
867.Nm \&ed
868prints
869.Sq Li ?interrupted
870and returns to its command level.
871.Pp
872Some size limitations:
873512 characters per line,
874256 characters per global command list,
87564 characters per file name,
876and, on mini computers,
877128K characters in the temporary file.
878The limit on the number of lines depends on the amount of core:
879each line takes 2 words.
880.Pp
881When reading a file,
882.Nm \&ed
883discards
884.Tn ASCII NUL
885characters
886and all characters after the last newline.
887It refuses to read files containing
888.Ns non- Tn ASCII
889characters.
890.Sh FILES
891.Bl -tag -compact -width "/tmp/ed*"
892.It Pa /tmp/e*
893.It Pa edhup
894work is saved here if terminal hangs up
895.El
896.Sh SEE ALSO
897.Xr \&ex 1 ,
898.Xr sed 1 ,
899.Xr crypt 1
900.br
901B. W. Kernighan,
902.Em A Tutorial Introduction to the ED Text Editor
903.br
904B. W. Kernighan,
905.Em Ar Advanced editing on UNIX
906.Sh HISTORY
907The
908.Nm \&ed
909command appeared in
910.At v6 .
911.Sh DIAGNOSTICS
912.Sq Li name
913for inaccessible file;
914.Sq Li ?self-explanatory message
915for other errors.
916.Pp
917To protect against throwing away valuable work,
918a
919.Cm \&q
920or
921.Cm \&e
922command is considered to be in error, unless a
923.Cm \&w
924has occurred since the last buffer change.
925A second
926.Cm \&q
927or
928.Cm \&e
929will be obeyed regardless.
930.Sh BUGS
931The
932.Cm \&l
933command mishandles
934.Li DEL .
935.br
936The
937.Cm \&undo
938command causes marks to be lost on affected lines.
939.br
940The special treatment of hangups only works on
941.Ux .
942