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