date and time created 87/12/12 18:28:39 by bostic
[unix-history] / usr / src / bin / ed / ed.1
CommitLineData
ba83de7e 1.\" @(#)ed.1 6.2 (Berkeley) %G%
fd315f2c 2.\"
7426f8ff 3.TH ED 1 ""
fd315f2c
KM
4.AT 3
5.if t .ds q \(aa
6.if n .ds q '
7.SH NAME
8ed \- text editor
9.SH SYNOPSIS
10.B ed
11[
12.B \-
fd315f2c
KM
13] [ name ]
14.SH DESCRIPTION
15.I Ed
16is the standard text editor.
17.PP
18If a
19.I name
20argument is given,
21.I ed
22simulates an
23.I e
24command (see below)\| on the named file; that is to say,
25the file is read into
26.I ed's
27buffer so that it can be edited.
fd315f2c
KM
28The optional
29.B \-
30suppresses the printing
31of explanatory output
32and should be used
33when the standard input is
34an editor script.
35.PP
36.I Ed
37operates on a copy of any file it is editing; changes made
38in the copy have no effect on the file until a
39.IR w ""
40(write)\|
41command is given.
42The copy of the text being edited resides
43in a temporary file called the
44.IR buffer .
45.PP
46Commands to
47.I ed
48have a simple and regular structure: zero or
49more
50.I addresses
51followed by a single character
52.I command,
53possibly
54followed by parameters to the command.
55These addresses specify one or more lines in the buffer.
56Missing addresses are supplied by default.
57.PP
58In general, only one command may appear on a line.
59Certain commands allow the
60addition of text to the buffer.
61While
62.I ed
63is accepting text, it is said
64to be in
65.I "input mode."
66In this mode, no commands are recognized;
67all input is merely collected.
68Input mode is left by typing a period `\fB.\fR' alone at the
69beginning of a line.
70.PP
71.I Ed
72supports a limited form of
73.I "regular expression"
74notation.
75A regular expression specifies
76a set of strings of characters.
77A member of this set of strings is said to be
78.I matched
79by the regular expression.
80In the following specification for regular expressions
81the word `character' means any character but newline.
82.IP 1.
83Any character except a special character
84matches itself.
85Special characters are
86the regular expression delimiter plus
87.RB \e\|[\| .
88and sometimes ^\|*\|$.
89.IP 2.
90A
91.B .
92matches any character.
93.IP 3.
94A \e followed by any character except a digit or (\|) matches that character.
95.IP 4.
96A nonempty string
97.I s
98bracketed
99.RI [ \|s\| ]
100(or
101.RI [^ s\| ])
102matches any character in (or not in)
103.I s.
104In
105.I s,
106\e has no special meaning, and ] may only appear as
107the first letter.
108A substring
109.I a\-b,
110with
111.I a
112and
113.I b
114in ascending ASCII order, stands for the inclusive
115range of ASCII characters.
116.IP 5.
117A regular expression of form 1-4 followed by * matches a sequence of
1180 or more matches of the regular expression.
119.IP 6.
120A regular expression,
121.I x,
122of form 1-8, bracketed
123.RI \e( \|x\| \e)
124matches what
125.I x
126matches.
127.IP 7.
128A \e followed by a digit
129.I n
130matches a copy of the string that the
131bracketed regular expression beginning with the
132.IR n th
133\e( matched.
134.IP 8.
135A regular expression of form 1-8,
136.I x,
137followed by a regular expression of form 1-7,
138.I y
139matches a match for
140.I x
141followed by a match for
142.I y,
143with the
144.I x
145match being as long as possible while still permitting a
146.I y
147match.
148.IP 9.
149A regular expression of form 1-8 preceded by ^
150(or followed by $), is constrained to matches that
151begin at the left (or end at the right) end of a line.
152.IP 10.
153A regular expression of form 1-9 picks out the
154longest among the leftmost matches in a line.
155.IP 11.
156An empty regular expression stands for a copy of the
157last regular expression encountered.
158.PP
159Regular expressions are used in addresses to specify
160lines and in one command
161(see
162.I s
163below)\|
164to specify a portion of a line which is to be replaced.
165If it is desired to use one of
166the regular expression metacharacters as an ordinary
167character, that character may be preceded by `\e'.
168This also applies to the character bounding the regular
169expression (often `/')\| and to `\e' itself.
170.PP
171To understand addressing in
172.I ed
173it is necessary to know that at any time there is a
174.I "current line."
175Generally speaking, the current line is
176the last line affected by a command; however,
177the exact effect on the current line
178is discussed under the description of
179the command.
180Addresses are constructed as follows.
181.TP
1821.
183The character `\fB.\fR' addresses the current line.
184.TP
1852.
186The character `$' addresses the last line of the buffer.
187.TP
1883.
189A decimal number
190.I n
191addresses the
192.IR n -th
193line of the buffer.
194.TP
1954.
196`\(fm\fIx\fR' addresses the line marked with the name
197.IR x ,
198which must be a lower-case letter.
199Lines are marked with the
200.I k
201command described below.
202.TP
2035.
204A regular expression enclosed in slashes `/' addresses
205the line found by searching forward from the current line
206and stopping at the first line containing a
207string that matches the regular expression.
208If necessary the search wraps around to the beginning of the
209buffer.
210.TP
2116.
212A regular expression enclosed in queries `?' addresses
213the line found by searching backward from the current line
214and stopping at the first line containing
215a string that matches the regular expression.
216If necessary
217the search wraps around to the end of the buffer.
218.TP
2197.
220An address followed by a plus sign `+'
221or a minus sign `\-' followed by a decimal number specifies that address plus
222(resp. minus)\| the indicated number of lines.
223The plus sign may be omitted.
224.TP
2258.
226If an address begins with `+' or `\-'
227the addition or subtraction is taken with respect to the current line;
228e.g. `\-5' is understood to mean `\fB.\fR\-5'.
229.TP
2309.
231If an address ends with `+' or `\-',
232then 1 is added (resp. subtracted).
233As a consequence of this rule and rule 8,
234the address `\-' refers to the line before the current line.
235Moreover,
236trailing
237`+' and `\-' characters
238have cumulative effect, so `\-\-' refers to the current
239line less 2.
240.TP
24110.
242To maintain compatibility with earlier versions of the editor,
243the character `^' in addresses is
244equivalent to `\-'.
245.PP
246Commands may require zero, one, or two addresses.
247Commands which require no addresses regard the presence
248of an address as an error.
249Commands which accept one or two addresses
250assume default addresses when insufficient are given.
251If more addresses are given than such a command requires,
252the last one or two (depending on what is accepted)\| are used.
253.PP
254Addresses are separated from each other typically by a comma
255`\fB,\fR'.
256They may also be separated by a semicolon
257`\fB;\fR'.
258In this case the current line `\fB.\fR' is set to
259the previous address before the next address is interpreted.
260This feature can be used to determine the starting
261line for forward and backward searches (`/', `?')\|.
262The second address of any two-address sequence
263must correspond to a line following the line corresponding to the first address.
264The special form `%'
265is an abbreviation for the address pair `1,$'.
266.PP
267In the following list of
268.I ed
269commands, the default addresses
270are shown in parentheses.
271The parentheses are not part of
272the address, but are used to show that the given addresses are
273the default.
274.PP
275As mentioned, it is generally illegal for more than one
276command to appear on a line.
277However, most commands may be suffixed by `p'
278or by `l', in which case
279the current line is either
280printed or listed respectively
281in the way discussed below.
282Commands may also be suffixed by `n',
283meaning the output of the command is to
284be line numbered.
285These suffixes may be combined in any order.
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
fd315f2c
KM
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 punctuation character
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.IP
512One or two trailing delimiters may be omitted,
513implying the `p' suffix.
514The special form `s' followed by
515.I no
516delimiters
517repeats the most recent substitute command
518on the addressed lines.
519The `s' may be followed by the letters
520.B r
521(use the most recent regular expression for the
522left hand side, instead of the most recent
523left hand side of a substitute command),
524.B p
525(complement the setting of the
526.I p
527suffix from the previous substitution), or
528.B g
529(complement the setting of the
530.I g
531suffix).
532These letters may be combined in any order.
533.TP 5
534.RB (\| .\| ,\ .\| )\|t\|\fIa
535This command acts just like the
536.I m
537command, except that a copy of the addressed lines is placed
538after address
539.I a
540(which may be 0).
541`\fB.\fR' is left on the last line of the copy.
542.TP 5
543.RB (\| .\| ,\ .\| )\|u
544The undo command restores the buffer to it's state
545before the most recent buffer modifying command.
546The current line is also restored.
547Buffer modifying commands are
548.I a, c, d, g, i, k, m, r, s, t,
549and
550.I v.
551For purposes of undo,
552.I g
553and
554.I v
555are considered to be a single buffer modifying command.
556Undo is its own inverse.
557.IP
558When
559.I ed
560runs out of memory
561(at about 8000 lines on any 16 bit mini-computer
562such as the PDP-11)
563This full undo is not possible, and
564.I u
565can only undo the effect of the most recent
566substitute on the current line.
567This restricted undo also applies to editor scripts
568when
569.I ed
570is invoked with the
571.B -
572option.
573.TP 5
574(1, $)\|v/regular expression/command list
575This command is the same as the global command
576.I g
577except that the command list is executed
578.I g
579with `\fB.\fR' initially set to every line
580.I except
581those
582matching the regular expression.
583.TP 5
584(1, $)\|w filename
585.br
586The write command writes the addressed lines onto
587the given file.
588If the file does not exist,
589it is created.
590The file name is remembered if there was no
591remembered file name already.
592If no file name is given,
593the remembered file name, if any, is used
594(see
595.I e
596and
597.I f
598commands)\|.
599`\fB.\fR' is unchanged.
600If the command is successful, the number of characters written is
601printed.
602.TP
603(1, $)\|W filename
604This command is the same as
605.I w,
606except that the addressed lines are appended to the file.
607.TP 5
608(1, $)\|wq filename
609This command is the same as
610.I w
611except that afterwards a
612.I q
613command is done,
614exiting the editor
615after the file is written.
616.TP 5
fd315f2c
KM
617.RB (\| .\| +1)\|z or,
618.br
619.ns
620.TP 5
621.RB (\| .\| +1)\|z\fIn
622This command scrolls through the buffer starting at the addressed line.
62322 (or
624.I n,
625if given)
626lines are printed.
627The last line printed becomes the current line.
628The value
629.I n
630is sticky, in that it becomes the default for
631future
632.I z
633commands.
634.TP 5
635($)\|=
636The line number of the addressed line is typed.
637`\fB.\fR' is unchanged by this command.
638.TP 5
639!<shell command>
640The remainder of the line after the `!' is sent
641to
642.IR sh (1)
643to be interpreted as a command.
644.RB ` . '
645is unchanged.
646.TP 5
647.RB (\| . +1,\| . +1)\|<newline>
648An address alone on a line causes the addressed line to be printed.
649A blank line alone is equivalent to `.+1p'; it is useful
650for stepping through text.
651If two addresses are present with no
652intervening semicolon,
653.I ed
654prints the range of lines.
655If they are separated by a semicolon,
656the second line is printed.
657.PP
658If an interrupt signal (ASCII DEL)\| is sent,
659.I ed
660prints `?interrupted'
661and returns to its command level.
662.PP
663Some size limitations:
664512 characters per line,
665256 characters per global command list,
66664 characters per file name,
667and, on mini computers,
668128K characters in the temporary file.
669The limit on the number of lines depends on the amount of core:
670each line takes 2 words.
671.PP
672When reading a file,
673.I ed
674discards ASCII NUL characters
675and all characters after the last newline.
676It refuses to read files containing non-ASCII characters.
677.SH FILES
678/tmp/e*
679.br
680edhup: work is saved here if terminal hangs up
681.SH "SEE ALSO"
682B. W. Kernighan,
683.I
684A Tutorial Introduction to the ED Text Editor
685.br
686B. W. Kernighan,
687.I Advanced editing on UNIX
688.br
d92b1877 689ex(1), sed(1), crypt(1)
fd315f2c
KM
690.SH DIAGNOSTICS
691`?name' for inaccessible file;
d92b1877 692`?self-explanatory message'
fd315f2c
KM
693for other errors.
694.PP
695To protect against throwing away valuable work,
696a
697.I q
698or
699.I e
700command is considered to be in error, unless a
701.I w
702has occurred since the last buffer change.
703A second
704.I q
705or
706.I e
707will be obeyed regardless.
708.SH BUGS
709The
710.I l
711command mishandles DEL.
712.br
713The
714.I undo
715command causes marks to be lost on affected lines.