Research V7 development
[unix-history] / usr / man / man1 / ed.1
CommitLineData
9314db29
KT
1.TH ED 1
2.if t .ds q \(aa
3.if n .ds q '
4.SH NAME
5ed \- text editor
6.SH SYNOPSIS
7.B ed
8[
9.B \-
10] [
11.B \-x
12] [ name ]
13.SH DESCRIPTION
14.I Ed
15is the standard text editor.
16.PP
17If a
18.I name
19argument is given,
20.I ed
21simulates an
22.I e
23command (see below)\| on the named file; that is to say,
24the file is read into
25.I ed's
26buffer so that it can be edited.
27If
28.B \-x
29is present, an
30.I x
31command is simulated first to handle an encrypted file.
32The optional
33.B \-
34suppresses the printing
35of character counts by
36.I e,
37.I r,
38and
39.I w
40commands.
41.PP
42.I Ed
43operates on a copy of any file it is editing; changes made
44in the copy have no effect on the file until a
45.IR w ""
46(write)\|
47command is given.
48The copy of the text being edited resides
49in a temporary file called the
50.IR buffer .
51.PP
52Commands to
53.I ed
54have a simple and regular structure: zero or
55more
56.I addresses
57followed by a single character
58.I command,
59possibly
60followed by parameters to the command.
61These addresses specify one or more lines in the buffer.
62Missing addresses are supplied by default.
63.PP
64In general, only one command may appear on a line.
65Certain commands allow the
66addition of text to the buffer.
67While
68.I ed
69is accepting text, it is said
70to be in
71.I "input mode."
72In this mode, no commands are recognized;
73all input is merely collected.
74Input mode is left by typing a period `\fB.\fR' alone at the
75beginning of a line.
76.PP
77.I Ed
78supports a limited form of
79.I "regular expression"
80notation.
81A regular expression specifies
82a set of strings of characters.
83A member of this set of strings is said to be
84.I matched
85by the regular expression.
86In the following specification for regular expressions
87the word `character' means any character but newline.
88.IP 1.
89Any character except a special character
90matches itself.
91Special characters are
92the regular expression delimiter plus
93.RB \e\|[\| .
94and sometimes ^\|*\|$.
95.IP 2.
96A
97.B .
98matches any character.
99.IP 3.
100A \e followed by any character except a digit or (\|) matches that character.
101.IP 4.
102A nonempty string
103.I s
104bracketed
105.RI [ \|s\| ]
106(or
107.RI [^ s\| ])
108matches any character in (or not in)
109.I s.
110In
111.I s,
112\e has no special meaning, and ] may only appear as
113the first letter.
114A substring
115.I a\-b,
116with
117.I a
118and
119.I b
120in ascending ASCII order, stands for the inclusive
121range of ASCII characters.
122.IP 5.
123A regular expression of form 1-4 followed by * matches a sequence of
1240 or more matches of the regular expression.
125.IP 6.
126A regular expression,
127.I x,
128of form 1-8, bracketed
129.RI \e( \|x\| \e)
130matches what
131.I x
132matches.
133.IP 7.
134A \e followed by a digit
135.I n
136matches a copy of the string that the
137bracketed regular expression beginning with the
138.IR n th
139\e( matched.
140.IP 8.
141A regular expression of form 1-8,
142.I x,
143followed by a regular expression of form 1-7,
144.I y
145matches a match for
146.I x
147followed by a match for
148.I y,
149with the
150.I x
151match being as long as possible while still permitting a
152.I y
153match.
154.IP 9.
155A regular expression of form 1-8 preceded by ^
156(or followed by $), is constrained to matches that
157begin at the left (or end at the right) end of a line.
158.IP 10.
159A regular expression of form 1-9 picks out the
160longest among the leftmost matches in a line.
161.IP 11.
162An empty regular expression stands for a copy of the
163last regular expression encountered.
164.PP
165Regular expressions are used in addresses to specify
166lines and in one command
167(see
168.I s
169below)\|
170to specify a portion of a line which is to be replaced.
171If it is desired to use one of
172the regular expression metacharacters as an ordinary
173character, that character may be preceded by `\e'.
174This also applies to the character bounding the regular
175expression (often `/')\| and to `\e' itself.
176.PP
177To understand addressing in
178.I ed
179it is necessary to know that at any time there is a
180.I "current line."
181Generally speaking, the current line is
182the last line affected by a command; however,
183the exact effect on the current line
184is discussed under the description of
185the command.
186Addresses are constructed as follows.
187.TP
1881.
189The character `\fB.\fR' addresses the current line.
190.TP
1912.
192The character `$' addresses the last line of the buffer.
193.TP
1943.
195A decimal number
196.I n
197addresses the
198.IR n -th
199line of the buffer.
200.TP
2014.
202`\(fm\fIx\fR' addresses the line marked with the name
203.IR x ,
204which must be a lower-case letter.
205Lines are marked with the
206.I k
207command described below.
208.TP
2095.
210A regular expression enclosed in slashes `/' addresses
211the line found by searching forward from the current line
212and stopping at the first line containing a
213string that matches the regular expression.
214If necessary the search wraps around to the beginning of the
215buffer.
216.TP
2176.
218A regular expression enclosed in queries `?' addresses
219the line found by searching backward from the current line
220and stopping at the first line containing
221a string that matches the regular expression.
222If necessary
223the search wraps around to the end of the buffer.
224.TP
2257.
226An address followed by a plus sign `+'
227or a minus sign `\-' followed by a decimal number specifies that address plus
228(resp. minus)\| the indicated number of lines.
229The plus sign may be omitted.
230.TP
2318.
232If an address begins with `+' or `\-'
233the addition or subtraction is taken with respect to the current line;
234e.g. `\-5' is understood to mean `\fB.\fR\-5'.
235.TP
2369.
237If an address ends with `+' or `\-',
238then 1 is added (resp. subtracted).
239As a consequence of this rule and rule 8,
240the address `\-' refers to the line before the current line.
241Moreover,
242trailing
243`+' and `\-' characters
244have cumulative effect, so `\-\-' refers to the current
245line less 2.
246.TP
24710.
248To maintain compatibility with earlier versions of the editor,
249the character `^' in addresses is
250equivalent to `\-'.
251.PP
252Commands may require zero, one, or two addresses.
253Commands which require no addresses regard the presence
254of an address as an error.
255Commands which accept one or two addresses
256assume default addresses when insufficient are given.
257If more addresses are given than such a command requires,
258the last one or two (depending on what is accepted)\| are used.
259.PP
260Addresses are separated from each other typically by a comma
261`\fB,\fR'.
262They may also be separated by a semicolon
263`\fB;\fR'.
264In this case the current line `\fB.\fR' is set to
265the previous address before the next address is interpreted.
266This feature can be used to determine the starting
267line for forward and backward searches (`/', `?')\|.
268The second address of any two-address sequence
269must correspond to a line following the line corresponding to the first address.
270.PP
271In the following list of
272.I ed
273commands, the default addresses
274are shown in parentheses.
275The parentheses are not part of
276the address, but are used to show that the given addresses are
277the default.
278.PP
279As mentioned, it is generally illegal for more than one
280command to appear on a line.
281However, most commands may be suffixed by `p'
282or by `l', in which case
283the current line is either
284printed or listed respectively
285in the way discussed below.
286.TP 5
287.RB (\| .\| )\|a
288.br
289.ns
290.TP 5
291<text>
292.br
293.ns
294.TP 5
295.B .
296.br
297The append command reads the given text
298and appends it after the addressed line.
299`\fB.\fR' is left
300on the last line input, if there
301were any, otherwise at the addressed line.
302Address `0' is legal for this command; text is placed
303at the beginning of the buffer.
304.TP 5
305.RB (\| .\| ,\ .\| )\|c
306.br
307.ns
308.TP 5
309<text>
310.br
311.ns
312.TP 5
313.B .
314.br
315The change
316command deletes the addressed lines, then accepts input
317text which replaces these lines.
318`\fB.\fR' is left at the last line input; if there were none,
319it is left at the line preceding the deleted lines.
320.TP 5
321.RB (\| .\| ,\ .\| )\|d
322The delete command deletes the addressed lines from the buffer.
323The line originally after the last line deleted becomes the current line;
324if the lines deleted were originally at the end,
325the new last line becomes the current line.
326.TP 5
327e filename
328The edit
329command causes the entire contents of the buffer to be deleted,
330and then the named file to be read in.
331`\fB.\fR' is set to the last line of the buffer.
332The number of characters read is typed.
333`filename' is remembered for possible use as a default file name
334in a subsequent
335.I r
336or
337.I w
338command.
339If `filename' is missing, the remembered name is used.
340.TP 5
341E filename
342This command is the same as
343.I e,
344except that no diagnostic results when no
345.I w
346has been given since the last buffer alteration.
347.TP 5
348f filename
349The filename command prints the currently remembered file name.
350If `filename' is given,
351the currently remembered file name is changed to `filename'.
352.TP 5
353(1,$)\|g/regular expression/command list
354In the global
355command, the first step is to mark every line which matches
356the given regular expression.
357Then for every such line, the
358given command list is executed with `\fB.\fR' initially set to that line.
359A single command or the first of multiple commands
360appears on the same line with the global command.
361All lines of a multi-line list except the last line must be ended with `\e'.
362.I A,
363.I i,
364and
365.I c
366commands and associated input are permitted;
367the `\fB.\fR' terminating input mode may be omitted if it would be on the
368last line of the command list.
369The commands
370.I g
371and
372.I v
373are not permitted in the command list.
374.TP 5
375.RB (\| .\| )\|i
376.ns
377.TP 5
378<text>
379.br
380.ns
381.TP 5
382.B .
383.br
384This command inserts the given text before the addressed line.
385`\fB.\fR' is left at the last line input, or, if there were none,
386at the line before the addressed line.
387This command differs from the
388.I a
389command only in the placement of the
390text.
391.TP 5
392.RB (\| .\| ,\ . +1)\|j
393This command joins the addressed lines into a single line;
394intermediate newlines simply disappear.
395`\fB.\fR' is left at the resulting line.
396.TP 5
397( \fB. \fR)\|k\fIx\fR
398The mark command marks the addressed line with
399name
400.I x,
401which must be a lower-case letter.
402The address form `\(fm\fIx\fR' then addresses this line.
403.ne 2.5
404.TP 5
405.RB (\| .\| ,\ .\| )\|l
406The list command
407prints the addressed lines in an unambiguous way:
408non-graphic characters are
409printed in two-digit octal,
410and long lines are folded.
411The
412.I l
413command may be placed on the same line after any non-i/o
414command.
415.TP 5
416.RB (\| .\| ,\ .\| )\|m\fIa
417The move command repositions the addressed lines after the line
418addressed by
419.IR a .
420The last of the moved lines becomes the current line.
421.TP 5
422.RB (\| .\| ,\ .\| )\|p
423The print command prints the addressed lines.
424`\fB.\fR'
425is left at the last line printed.
426The
427.I p
428command
429may
430be placed on the same line after any non-i/o command.
431.TP
432.RB (\| .\| ,\ .\| )\|P
433This command is a synonym for
434.I p.
435.TP 5
436q
437The quit command causes
438.I ed
439to exit.
440No automatic write
441of a file is done.
442.TP 5
443Q
444This command is the same as
445.I q,
446except that no diagnostic results when no
447.I w
448has been given since the last buffer alteration.
449.TP 5
450($)\|r filename
451The read command
452reads in the given file after the addressed line.
453If no file name is given,
454the remembered file name, if any, is used
455(see
456.I e
457and
458.I f
459commands)\|.
460The file name is remembered if there was no
461remembered file name already.
462Address `0' is legal for
463.I r
464and causes the
465file to be read at the beginning of the buffer.
466If the read is successful, the number of characters
467read is typed.
468`\fB.\fR' is left at the last line read in from the file.
469.TP 5
470(\| \fB.\fR\|, \fB.\fR\|)\|s/regular expression/replacement/ or,
471.br
472.ns
473.TP 5
474(\| \fB.\fR\|, \fB.\fR\|)\|s/regular expression/replacement/g
475The substitute command searches each addressed
476line for an occurrence of the specified regular expression.
477On each line in which a match is found,
478all matched strings are replaced by the replacement specified,
479if the global replacement indicator `g' appears after the command.
480If the global indicator does not appear, only the first occurrence
481of the matched string is replaced.
482It is an error for the substitution to fail on all addressed lines.
483Any character other than space or new-line
484may be used instead of `/' to delimit the regular expression
485and the replacement.
486`\fB.\fR' is left at the last line substituted.
487.IP
488An ampersand `&' appearing in the replacement
489is replaced by the string matching the regular expression.
490The special meaning of `&' in this context may be
491suppressed by preceding it by `\e'.
492The characters
493.I `\|\en'
494where
495.I n
496is a digit,
497are replaced by the text matched by the
498.IR n -th
499regular subexpression
500enclosed between `\e(' and `\e)'.
501When
502nested, parenthesized subexpressions
503are present,
504.I n
505is determined by counting occurrences of `\e(' starting from the left.
506.IP
507Lines may be split by substituting new-line characters into them.
508The new-line in the
509replacement string
510must be escaped by preceding it by `\e'.
511.TP 5
512.RB (\| .\| ,\ .\| )\|t\|\fIa
513This command acts just like the
514.I m
515command, except that a copy of the addressed lines is placed
516after address
517.I a
518(which may be 0).
519`\fB.\fR' is left on the last line of the copy.
520.TP 5
521.RB (\| .\| ,\ .\| )\|u
522The undo command restores the preceding contents
523of the current line, which must be the last line
524in which a substitution was made.
525.TP 5
526(1, $)\|v/regular expression/command list
527This command is the same as the global command
528.I g
529except that the command list is executed
530.I g
531with `\fB.\fR' initially set to every line
532.I except
533those
534matching the regular expression.
535.TP 5
536(1, $)\|w filename
537.br
538The write command writes the addressed lines onto
539the given file.
540If the file does not exist,
541it is created mode 666 (readable and writable by everyone)\|.
542The file name is remembered if there was no
543remembered file name already.
544If no file name is given,
545the remembered file name, if any, is used
546(see
547.I e
548and
549.I f
550commands)\|.
551`\fB.\fR' is unchanged.
552If the command is successful, the number of characters written is
553printed.
554.TP
555(1,$)W filename
556This command is the same as
557.I w,
558except that the addressed lines are appended to the file.
559.TP 5
560x
561A key string is demanded from the standard input.
562Later
563.I r, e
564and
565.I w
566commands will encrypt and decrypt the text
567with this key by the algorithm of
568.IR crypt (1).
569An explicitly empty key turns off encryption.
570.TP 5
571($)\|=
572The line number of the addressed line is typed.
573`\fB.\fR' is unchanged by this command.
574.TP 5
575!<shell command>
576The remainder of the line after the `!' is sent
577to
578.IR sh (1)
579to be interpreted as a command.
580.RB ` . '
581is unchanged.
582.TP 5
583.RB (\| . +1)\|<newline>
584An address alone on a line causes the addressed line to be printed.
585A blank line alone is equivalent to `.+1p'; it is useful
586for stepping through text.
587.PP
588If an interrupt signal (ASCII DEL)\| is sent,
589.I ed
590prints a `?' and returns to its command level.
591.PP
592Some size limitations:
593512 characters per line,
594256 characters per global command list,
59564 characters per file name,
596and 128K characters in the temporary file.
597The limit on the number of lines depends on the amount of core:
598each line takes 1 word.
599.PP
600When reading a file,
601.I ed
602discards ASCII NUL characters
603and all characters after the last newline.
604It refuses to read files containing non-ASCII characters.
605.SH FILES
606/tmp/e*
607.br
608ed.hup: work is saved here if terminal hangs up
609.SH "SEE ALSO"
610B. W. Kernighan,
611.I
612A Tutorial Introduction to the ED Text Editor
613.br
614B. W. Kernighan,
615.I Advanced editing on UNIX
616.br
617sed(1), crypt(1)
618.SH DIAGNOSTICS
619`?name' for inaccessible file;
620`?' for
621errors in commands;
622`?TMP' for temporary file overflow.
623.PP
624To protect against throwing away valuable work,
625a
626.I q
627or
628.I e
629command is considered to be in error, unless a
630.I w
631has occurred since the last buffer change.
632A second
633.I q
634or
635.I e
636will be obeyed regardless.
637.SH BUGS
638The
639.I l
640command mishandles DEL.
641.br
642A
643.I !
644command cannot be subject to a
645.I g
646command.
647.br
648Because 0 is an illegal address for a
649.I w
650command, it is not possible to
651create an empty file with
652.I ed.