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