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