Add diclaimer of copyright to _osname() manual page.
[unix-history] / bin / ed / ed.1
CommitLineData
54a7a3ed
AM
1.TH ED 1 "21 May 1993"
2.SH NAME
3ed, red \- text editor
4.SH SYNOPSIS
5ed [-] [-sx] [-p \fIstring\fR] [\fIfile\fR]
6.LP
7red [-] [-sx] [-p \fIstring\fR] [\fIfile\fR]
8.SH DESCRIPTION
9.B ed
10is a line-oriented text editor.
11It is used to create, display, modify and otherwise manipulate text
12files.
13.B red
14is a restricted
15.BR ed :
16it can only edit files in the current
17directory and cannot execute shell commands.
18
19If invoked with a
20.I file
21argument, then a copy of
22.I file
23is read into the editor's buffer.
24Changes are made to this copy and not directly to
25.I file
26itself.
27Upon quitting
28.BR ed ,
29any changes not explicitly saved with a
30.I `w'
31command are lost.
32
33Editing is done in two distinct modes:
34.I command
35and
36.IR input .
37When first invoked,
38.B ed
39is in command mode.
40In this mode commands are read from the standard input and
41executed to manipulate the contents of the editor buffer.
42A typical command might look like:
43.sp
44.RS
45,s/\fIold\fR/\fInew\fR/g
46.RE
47.sp
48which replaces all occurences of the string
49.I old
50with
51.IR new .
52
53When an input command, such as
54.I `a'
55(append),
56.I `i'
57(insert) or
58.I `c'
59(change), is given,
60.B ed
61enters input mode. This is the primary means
62of adding text to a file.
63In this mode, no commands are available;
64instead, the standard input is written
65directly to the editor buffer. Lines consist of text up to and
66including a
67.IR newline
68character.
69Input mode is terminated by
70entering a single period (\fI.\fR) on a line.
71
72All
73.B ed
74commands operate on whole lines or ranges of lines; e.g.,
75the
76.I `d'
77command deletes lines; the
78.I `m'
79command moves lines, and so on.
80It is possible to modify only a portion of a line by means of replacement,
81as in the example above. However even here, the
82.I `s'
83command is applied to whole lines at a time.
84
85In general,
86.B ed
87commands consist of zero or more line addresses, followed by a single
88character command and possibly additional parameters; i.e.,
89commands have the structure:
90.sp
91.RS
92.I [address [,address]]command[parameters]
93.RE
94.sp
e8bd1b9a
AM
95The address(es) indicate the line or range of lines to be affected by the
96command. If fewer addresses are given than the command accepts, then
97default addresses are supplied.
54a7a3ed
AM
98
99.SS OPTIONS
100.TP 8
101-s
102Suppresses diagnostics. This should be used if
103.BR ed 's
104standard input is from a script.
105
106.TP 8
107-x
108Prompts for an encryption key to be used in subsequent reads and writes
109(see the
110.I `x'
111command).
112
113.TP 8
114.RI \-p \ string
115Specifies a command prompt. This may be toggled on and off with the
116.I `P'
117command.
118
119.TP 8
120.I file
121Specifies the name of a file to read. If
122.I file
123is prefixed with a
124bang (!), then it is interpreted as a shell command. In this case,
125what is read is
126the standard output of
127.I file
128executed via
129.IR sh (1).
130To read a file whose name begins with a bang, prefix the
131name with a backslash (\\).
132The default filename is set to
133.I file
134only if it is not prefixed with a bang.
135
136.SS LINE ADDRESSING
84ee7b23 137An address represents the number of a line in the buffer.
54a7a3ed
AM
138.B ed
139maintains a
140.I current address
141which is
142typically supplied to commands as the default address when none is specified.
143When a file is first read, the current address is set to the last line
144of the file. In general, the current address is set to the last line
145affected by a command.
146
147A line address is
148constructed from one of the bases in the list below, optionally followed
149by a numeric offset. The offset may include any combination
150of digits, operators (i.e.,
151.IR + ,
152.I -
153and
154.IR ^ )
155and whitespace.
156Addresses are read from left to right, and their values are computed
157relative to the current address.
158
159One exception to the rule that addresses represent line numbers is the
160address
161.I 0
162(zero).
163This means "before the first line,"
164and is legal wherever it makes sense.
165
166An address range is two addresses separated either by a comma or
167semi-colon. The value of the first address in a range cannot exceed the
84ee7b23
AM
168value of the the second. If only one address is given in a range, then
169the second address is set to the given address. If an
54a7a3ed
AM
170.IR n- tuple
171of addresses is given where
172.I n > 2,
84ee7b23
AM
173then the corresponding range is determined by the last two addresses in
174the
54a7a3ed 175.IR n- tuple.
84ee7b23 176If only one address is expected, then the last address is used.
54a7a3ed
AM
177
178Each address in a comma-delimited range is interpreted relative to the
179current address. In a semi-colon-delimited range, the first address is
180used to set the current address, and the second address is interpreted
181relative to the first.
182
84ee7b23 183
54a7a3ed
AM
184The following address symbols are recognized.
185
186.TP 8
84ee7b23 187\&.
54a7a3ed
AM
188The current line (address) in the buffer.
189
190.TP 8
191$
192The last line in the buffer.
193
194.TP 8
195n
196The
197.IR n th,
198line in the buffer
199where
200.I n
201is a number in the range
202.I [0,$].
203
204.TP 8
205- or ^
206The previous line.
207This is equivalent to
208.I -1
209and may be repeated with cumulative effect.
210
211.TP 8
212-\fIn\fR or ^\fIn\fR
213The
214.IR n th
215previous line, where
216.I n
217is a non-negative number.
218
219.TP 8
220+
221The
222next line.
223This is equivalent to
224.I +1
225and may be repeated with cumulative effect.
226
227.TP 8
228+\fIn\fR or whitespace\fIn\fR
229The
230.IR n th
231next line, where
232.I n
233is a non-negative number.
234.I whitespace
235followed by a number
236.I n
237is interpreted as
238.IR +n .
239
240.TP 8
241, \fRor\fB %
242The first through last lines in the buffer. This is equivalent to
243the address range
244.I 1,$.
245
246.TP 8
247;
248The
249current through last lines in the buffer. This is equivalent to
250the address range
251.I .,$.
252
253.TP 8
254.RI / re/
255The
256next line containing the regular expression
257.IR re .
258The search wraps to the beginning of the buffer and continues down to the
259current line, if necessary.
260// repeats the last search.
261
262.TP 8
263.RI ? re?
264The
265previous line containing the regular expression
266.IR re .
267The search wraps to the end of the buffer and continues up to the
268current line, if necessary.
269?? repeats the last search.
270
271.TP 8
272.RI \' lc
273The
274line previously marked by a
275.I `k'
276(mark) command, where
277.I lc
278is a lower case letter.
279
280.SS REGULAR EXPRESSIONS
281Regular expressions are patterns used in selecting text.
282For example, the
283.B ed
284command
285.sp
286.RS
287g/\fIstring\fR/
288.RE
289.sp
290prints all lines containing
291.IR string .
292Regular expressions are also
293used by the
294.I `s'
295command for selecting old text to be replaced with new.
296
297In addition to a specifying string literals, regular expressions can
298represent
299classes of strings. Strings thus represented are said to be matched
300by the corresponding regular expression.
301If it is possible for a regular expression
302to match several strings in a line, then the left-most longest match is
303the one selected.
304
305The following symbols are used in constructing regular expressions:
306
307.TP 8
308c
309Any character
310.I c
311not listed below, including `{', '}', `(', `)', `<' and `>',
312matches itself.
313
314.TP 8
e8bd1b9a 315\fR\e\fIc\fR
54a7a3ed
AM
316Any backslash-escaped character
317.IR c ,
318except for `{', '}', `(', `)', `<' and `>',
319matches itself.
320
321.TP 8
322\fR.\fR
323Matches any single character.
324
325.TP 8
326.I [char-class]
327Matches any single character in
328.IR char-class .
329To include a `]'
330in
331.IR char-class ,
332it must be the first character.
333A range of characters may be specified by separating the end characters
334of the range with a `-', e.g., `a-z' specifies the lower case characters.
335The following literal expressions can also be used in
336.I char-class
337to specify sets of characters:
338.sp
339\ \ [:alnum:]\ \ [:cntrl:]\ \ [:lower:]\ \ [:space:]
340.PD 0
341\ \ [:alpha:]\ \ [:digit:]\ \ [:print:]\ \ [:upper:]
342.PD 0
343\ \ [:blank:]\ \ [:graph:]\ \ [:punct:]\ \ [:xdigit:]
344.sp
345If `-' appears as the first or last
346character of
347.IR char-class ,
348then it matches itself.
349All other characters in
350.I char-class
351match themselves.
352.sp
353Patterns in
354.I char-class
355of the form:
356.sp
357\ \ [.\fIcol-elm\fR.] or,
358.PD 0
359\ \ [=\fIcol-elm\fR=]
360.sp
361where
362.I col-elm
363is a
364.I collating element
365are interpreted according to
366.IR locale (5)
367(not currently supported).
368See
369.IR regex (3)
370for an explanation of these constructs.
371
372.TP 8
373[^\fIchar-class\fR]
374Matches any single character, other than newline, not in
375.IR char-class .
376.IR char-class
377is defined
378as above.
379
380.TP 8
381^
382If `^' is the first character of a regular expression, then it
383anchors the regular expression to the beginning of a line.
384Otherwise, it matches itself.
385
386.TP 8
387$
388If `$' is the last character of a regular expression, it
389anchors the regular expression to the end of a line.
390Otherwise, it matches itself.
391
392.TP 8
e8bd1b9a 393\fR\e<\fR
54a7a3ed
AM
394Anchors the single character regular expression or subexpression
395immediately following it to the beginning of a word.
396(This may not be available)
397
398.TP 8
e8bd1b9a 399\fR\e>\fR
54a7a3ed
AM
400Anchors the single character regular expression or subexpression
401immediately following it to the end of a word.
402(This may not be available)
403
404.TP 8
e8bd1b9a 405\fR\e(\fIre\fR\e)\fR
54a7a3ed
AM
406Defines a subexpression
407.IR re .
408Subexpressions may be nested.
e8bd1b9a 409A subsequent backreference of the form \fI`\en'\fR, where
54a7a3ed
AM
410.I n
411is a number in the range [1,9], expands to the text matched by the
412.IR n th
413subexpression.
e8bd1b9a 414For example, the regular expression `\e(.*\e)\e1' matches any string
54a7a3ed
AM
415consisting of identical adjacent substrings.
416Subexpressions are ordered relative to
417their left delimiter.
418
419.TP 8
420*
421Matches the single character regular expression or subexpression
422immediately preceding it zero or more times. If '*' is the first
423character of a regular expression or subexpression, then it matches
424itself. The `*' operator sometimes yields unexpected results.
425For example, the regular expression `b*' matches the beginning of
426the string `abbb' (as opposed to the substring `bbb'), since a null match
427is the only left-most match.
428
429.TP 8
e8bd1b9a 430\fR\e{\fIn,m\fR\e}\fR or \fR\e{\fIn,\fR\e}\fR or \fR\e{\fIn\fR\e}\fR
54a7a3ed
AM
431Matches the single character regular expression or subexpression
432immediately preceding it at least
433.I n
434and at most
435.I m
436times.
437If
438.I m
439is omitted, then it matches at least
440.I n
441times.
442If the comma is also omitted, then it matches exactly
443.I n
444times.
445
446.LP
447Additional regular expression operators may be defined depending on the
448particular
449.IR regex (3)
450implementation.
451
452.SS COMMANDS
453All
454.B ed
455commands are single characters, though some require additonal parameters.
456If a command's parameters extend over several lines, then
457each line except for the last
458must be terminated with a backslash (\\).
459
460In general, at most one command is allowed per line.
461However, most commands accept a print suffix, which is any of
462.I `p'
463(print),
464.I `l'
465(list) ,
466or
467.I `n'
468(enumerate),
469to print the last line affected by the command.
470
471An interrupt (typically ^C) has the effect of aborting the current command
472and returning the editor to command mode.
473
474.B ed
475recognizes the following commands. The commands are shown together with
476the default address or address range supplied if none is
477specified (in parenthesis).
478
479.TP 8
480(.)a
481Appends text to the buffer after the addressed line.
482Text is entered in input mode.
483The current address is set to last line entered.
484
485.TP 8
486(.,.)c
487Changes lines in the buffer. The addressed lines are deleted
488from the buffer, and text is appended in their place.
489Text is entered in input mode.
490The current address is set to last line entered.
491
492.TP 8
493(.,.)d
494Deletes the addressed lines from the buffer.
495If there is a line after the deleted range, then the current address is set
496to this line. Otherwise the current address is set to the line
497before the deleted range.
498
499.TP 8
500.RI e \ file
501Edits
502.IR file ,
503and sets the default filename.
504If
505.I file
506is not specified, then the default filename is used.
507Any lines in the buffer are deleted before
508the new file is read.
509The current address is set to the last line read.
510
511.TP 8
512.RI e \ !command
513Edits the standard output of
514.IR `!command' ,
84ee7b23
AM
515(see
516.RI ! command
517below).
54a7a3ed
AM
518The default filename is unchanged.
519Any lines in the buffer are deleted before the output of
520.I command
521is read.
522The current address is set to the last line read.
523
524.TP 8
525.RI E \ file
526Edits
527.I file
528unconditionally.
529This is similar to the
530.I e
531command,
532except that unwritten changes are discarded without warning.
533The current address is set to the last line read.
534
535.TP 8
536.RI f \ file
537Sets the default filename to
538.IR file .
539If
540.I file
541is not specified, then the default unescaped filename is printed.
542
543.TP 8
544.RI (1,$)g /re/command-list
545Applies
546.I command-list
547to each of the addressed lines matching a regular expression
548.IR re .
549The current address is set to the
550line currently matched before
551.I command-list
552is executed.
553At the end of the
554.I `g'
555command, the current address is set to the last line affected by
556.IR command-list .
557
558Each command in
559.I command-list
560must be on a separate line,
561and every line except for the last must be terminated by a backslash
562(\\).
563Any commands are allowed, except for
564.IR `g' ,
565.IR `G' ,
566.IR `v' ,
567and
568.IR `V' .
569A newline alone in
570.I command-list
571is equivalent to a
572.I `p'
573command.
574
575.TP 8
576.RI (1,$)G /re/
577Interactively edits the addressed lines matching a regular expression
578.IR re.
579For each matching line,
580the line is printed,
581the current address is set,
582and the user is prompted to enter a
583.IR command-list .
584At the end of the
585.I `G'
586command, the current address
587is set to the last line affected by (the last)
588.IR command-list .
589
590The format of
591.I command-list
592is the same as that of the
593.I `g'
594command. A newline alone acts as a null command list.
595A single `&' repeats the last non-null command list.
596
597.TP 8
598H
599Toggles the printing of error explanations.
600By default, explanations are not printed.
601It is recommended that ed scripts begin with this command to
602aid in debugging.
603
604.TP 8
605h
606Prints an explanation of the last error.
607
608.TP 8
609(.)i
610Inserts text in the buffer before the current line.
611Text is entered in input mode.
612The current address is set to the last line entered.
613
614.TP 8
615(.,.+1)j
616Joins the addressed lines. The addressed lines are
617deleted from the buffer and replaced by a single
618line containing their joined text.
619The current address is set to the resultant line.
620
621.TP 8
622.RI (.)k lc
623Marks a line with a lower case letter
624.IR lc .
625The line can then be addressed as
626.I 'lc
627(i.e., a single quote followed by
628.I lc
629) in subsequent commands. The mark is not cleared until the line is
630deleted or otherwise modified.
631
632.TP 8
633(.,.)l
634Prints the addressed lines unambiguously.
84ee7b23
AM
635If a single line fills for than one screen (as might be the case
636when viewing a binary file, for instance), a `--More--'
637prompt is printed on the last line.
638.B ed
639waits until the RETURN key is pressed
640before displaying the next screen.
54a7a3ed
AM
641The current address is set to the last line
642printed.
643
644.TP 8
645(.,.)m(.)
646Moves lines in the buffer. The addressed lines are moved to after the
647right-hand destination address, which may be the address
648.IR 0
649(zero).
650The current address is set to the
651last line moved.
652
653.TP 8
654(.,.)n
655Prints the addressed lines along with
656their line numbers. The current address is set to the last line
657printed.
658
659.TP 8
660(.,.)p
661Prints the addressed lines. The current address is set to the last line
662printed.
663
664.TP 8
665P
666Toggles the command prompt on and off.
667Unless a prompt was specified by with command-line option
668\fI-p string\fR, the command prompt is by default turned off.
669
670.TP 8
671q
672Quits ed.
673
674.TP 8
675Q
676Quits ed unconditionally.
677This is similar to the
678.I q
679command,
680except that unwritten changes are discarded without warning.
681
682.TP 8
683.RI ($)r \ file
684Reads
685.I file
686to after the addressed line. If
687.I file
688is not specified, then the default
689filename is used. If there was no default filename prior to the command,
690then the default filename is set to
691.IR file .
692Otherwise, the default filename is unchanged.
693The current address is set to the last line read.
694
695.TP 8
696.RI ($)r \ !command
697Reads
698to after the addressed line
699the standard output of
700.IR `!command' ,
84ee7b23
AM
701(see the
702.RI ! command
703below).
54a7a3ed
AM
704The default filename is unchanged.
705The current address is set to the last line read.
706
707.HP
708.RI (.,.)s /re/replacement/
709.PD 0
710.HP
711.RI (.,.)s /re/replacement/\fRg\fR
712.HP
713.RI (.,.)s /re/replacement/n
714.br
715Replaces text in the addressed lines
716matching a regular expression
717.I re
718with
719.IR replacement .
720By default, only the first match in each line is replaced.
84ee7b23 721If the
54a7a3ed 722.I `g'
84ee7b23 723(global) suffix is given, then every match to be replaced.
54a7a3ed
AM
724The
725.I `n'
726suffix, where
727.I n
728is a postive number, causes only the
729.IR n th
730match to be replaced.
731It is an error if no substitutions are performed on any of the addressed
732lines.
733The current address is set the last line affected.
734
735.I re
736and
737.I replacement
84ee7b23
AM
738may be delimited by any character other than space and newline
739(see the
740.I `s'
741command below).
54a7a3ed
AM
742If one or two of the last delimiters is omitted, then the last line
743affected is printed as though the print suffix
744.I `p'
745were specified.
746
747
748An unescaped `&' in
749.I replacement
750is replaced by the currently matched text.
751The character sequence
e8bd1b9a 752\fI`\em'\fR,
54a7a3ed
AM
753where
754.I m
755is a number in the range [1,9], is replaced by the
756.IR m th
757backreference expression of the matched text.
758If
759.I replacement
760consists of a single `%', then
761.I replacement
762from the last substitution is used.
763Newlines may be embedded in
764.I replacement
765if they are escaped with a backslash (\\).
766
767.TP 8
768(.,.)s
769Repeats the last substitution.
770This form of the
771.I `s'
84ee7b23
AM
772command accepts a count suffix
773.IR `n' ,
774or any combination of the characters
54a7a3ed
AM
775.IR `r' ,
776.IR `g' ,
777and
778.IR `p' .
84ee7b23
AM
779If a count suffix
780.I `n'
781is given, then only the
782.IR n th
783match is replaced.
54a7a3ed
AM
784The
785.I `r'
786suffix causes
787the regular expression of the last search to be used instead of the
788that of the last substitution.
789The
790.I `g'
791suffix toggles the global suffix of the last substitution.
792The
793.I `p'
794suffix toggles the print suffix of the last substitution
795The current address is set to the last line affected.
796
797.TP 8
798(.,.)t(.)
799Copies (i.e., transfers) the addressed lines to after the right-hand
800destination address, which may be the address
801.IR 0
802(zero).
803The current address is set to the last line
804copied.
805
806.TP 8
807u
808Undoes the last command and restores the current address
809to what it was before the command.
810The global commands
811.IR `g' ,
812.IR `G' ,
813.IR `v' ,
814and
815.IR `V' .
816are treated as a single command by undo.
817.I `u'
818is its own inverse.
819
820.TP 8
821.RI (1,$)v /pat/command-list
822Applies
823.I command-list
824to each of the addressed lines not matching a regular expression
825.IR re .
826This is similar to the
827.I `g'
828command.
829
830.TP 8
831.RI (1,$)V /re/
832Interactively edits the addressed lines not matching a regular expression
833.IR re.
834This is similar to the
835.I `G'
836command.
837
838.TP 8
839.RI (1,$)w \ file
840Writes the addressed lines to
841.IR file .
842Any previous contents of
843.I file
844is lost without warning.
845If there is no default filename, then the default filename is set to
846.IR file,
847otherwise it is unchanged. If no filename is specified, then the default
848filename is used.
849The current address is unchanged.
850
851.TP 8
852.RI (1,$)wq \ file
853Writes the addressed lines to
854.IR file ,
855and then executes a
856.I `q'
857command.
858
859.TP 8
860.RI (1,$)w \ !command
861Writes the addressed lines to the standard input of
862.IR `!command' ,
84ee7b23
AM
863(see the
864.RI ! command
865below).
54a7a3ed
AM
866The default filename and current address are unchanged.
867
868.TP 8
869.RI (1,$)W \ file
870Appends the addressed lines to the end of
871.IR file .
872This is similar to the
873.I `w'
874command, expect that the previous contents of file is not clobbered.
875The current address is unchanged.
876
877.TP 8
878x
879Prompts for an encryption key which is used in subsequent reads and
880writes. If a newline alone is entered as the key, then encryption is
881turned off. Otherwise, echoing is disabled while a key is read.
882Encryption/decryption is done using the bdes(1) algorithm.
883
884.TP 8
885.RI (.+1)z n
886Scrolls
887.I n
888lines at a time starting at addressed line. If
889.I n
890is not specified, then the current window size is used.
891The current address is set to the last line printed.
892
893.TP 8
894.RI ! command
895Executes
896.I command
897via
898.IR sh (1).
899If the first character of
900.I command
901is `!', then it is replaced by text of the
902previous
903.IR `!command' .
904.B ed
905does not process
906.I command
907for backslash (\\) escapes.
908However, an unescaped
909.I `%'
910is replaced by the default filename.
911When the shell returns from execution, a `!'
912is printed to the standard output.
913The current line is unchanged.
914
54a7a3ed
AM
915.TP 8
916($)=
917Prints the line number of the addressed line.
918
919.TP 8
920(.+1)newline
921Prints the addressed line, and sets the current address to
922that line.
923
924.SH FILES
925.TP 20
926/tmp/ed.*
927Buffer file
928.PD 0
929.TP 20
e8bd1b9a
AM
930ed.hup
931The file to which
54a7a3ed
AM
932.B ed
933attempts to write the buffer if the terminal hangs up.
934
935.SH SEE ALSO
936
937.IR vi (1),
938.IR sed (1),
939.IR regex (3),
940.IR bdes (1),
941.IR sh (1).
942
943USD:12-13
944
945B. W. Kernighan and P. J. Plauger,
946.I Software Tools in Pascal ,
947Addison-Wesley, 1981.
948
949.SH LIMITATIONS
950.B ed
951processes
952.I file
953arguments for backslash escapes, i.e., in a filename,
954any characters preceded by a backslash (\\) are
955interpreted literally.
956
957If a text (non-binary) file is not terminated by a newline character,
958then
959.B ed
960appends one on reading/writing it. In the case of a binary file,
961.B ed
962does not append a newline on reading/writing.
963
964per line overhead: 4 ints
965
966.SH DIAGNOSTICS
967When an error occurs,
968.B ed
969prints a `?' and either returns to command mode
970or exits if its input is from a script.
971An explanation of the last error can be
972printed with the
973.I `h'
974(help) command.
975
976Since the
977.I `g'
978(global) command masks any errors from failed searches and substitutions,
979it can be used to perform conditional operations in scripts; e.g.,
980.sp
981.RS
982g/\fIold\fR/s//\fInew\fR/
983.RE
984.sp
985replaces any occurrences of
986.I old
987with
988.IR new .
e8bd1b9a
AM
989If the
990.I `u'
991(undo) command occurs in a global command list, then
992the command list is executed only once.
54a7a3ed
AM
993
994If diagnostics are not disabled, attempting to quit
995.B ed
996or edit another file before writing a modified buffer
997results in an error.
998If the command is entered a second time, it succeeds,
999but any changes to the buffer are lost.