blob: 7b88f11ddc0c7ff5da3a122f9af51fdefaf9a630 [file] [log] [blame]
rsccfa37a72004-04-10 18:53:55 +00001.TH ED 1
2.SH NAME
3ed \- text editor
4.SH SYNOPSIS
5.B ed
6[
7.B -
8]
9[
10.B -o
11]
12[
13.I file
14]
15.SH DESCRIPTION
16.I Ed
17is a venerable text editor.
18.PP
19If a
20.I file
21argument is given,
22.I ed
23simulates an
24.L e
25command (see below) on that file:
26it is read into
27.I ed's
28buffer so that it can be edited.
29The options are
30.TP
31.B -
32Suppress the printing
33of character counts by
34.LR e ,
35.LR r ,
36and
37.L w
38commands and of the confirming
39.L !
40by
41.L !
42commands.
43.TP
44.B -o
45(for output piping)
46Write all output to the standard error file except writing by
47.L w
48commands.
49If no
50.I file
51is given, make
52.B /fd/1
53the remembered file; see the
54.L e
55command below.
56.PP
57.I Ed
58operates on a `buffer', a copy of the file it is editing;
59changes made
60in the buffer have no effect on the file until a
61.L w
62(write)
63command is given.
64The copy of the text being edited resides
65in a temporary file called the
66.IR buffer .
67.PP
68Commands to
69.I ed
70have a simple and regular structure: zero, one, or
71two
72.I addresses
73followed by a single character
74.IR command ,
75possibly
76followed by parameters to the command.
77These addresses specify one or more lines in the buffer.
78Missing addresses are supplied by default.
79.PP
80In general, only one command may appear on a line.
81Certain commands allow the
82addition of text to the buffer.
83While
84.I ed
85is accepting text, it is said
86to be in
87.I "input mode."
88In this mode, no commands are recognized;
89all input is merely collected.
90Input mode is left by typing a period
91.L .
92alone at the
93beginning of a line.
94.PP
95.I Ed
96supports the
97.I "regular expression"
98notation described in
99.IR regexp (6).
100Regular expressions are used in addresses to specify
101lines and in one command
102(see
103.I s
104below)
105to specify a portion of a line which is to be replaced.
106If it is desired to use one of
107the regular expression metacharacters as an ordinary
108character, that character may be preceded by
109.RB ` \e '.
110This also applies to the character bounding the regular
111expression (often
112.LR / )
113and to
114.L \e
115itself.
116.PP
117To understand addressing in
118.I ed
119it is necessary to know that at any time there is a
120.I "current line."
121Generally, the current line is
122the last line affected by a command; however,
123the exact effect on the current line
124is discussed under the description of
125each command.
126Addresses are constructed as follows.
127.TP
1281.
129The character
130.LR . ,
131customarily called `dot',
132addresses the current line.
133.TP
1342.
135The character
136.L $
137addresses the last line of the buffer.
138.TP
1393.
140A decimal number
141.I n
142addresses the
143.IR n -th
144line of the buffer.
145.TP
1464.
147.BI \'x
148addresses the line marked with the name
149.IR x ,
150which must be a lower-case letter.
151Lines are marked with the
152.L k
153command.
154.TP
1555.
156A regular expression enclosed in slashes (
157.LR / )
158addresses
159the line found by searching forward from the current line
160and stopping at the first line containing a
161string that matches the regular expression.
162If necessary the search wraps around to the beginning of the
163buffer.
164.TP
1656.
166A regular expression enclosed in queries
167.L ?
168addresses
169the line found by searching backward from the current line
170and stopping at the first line containing
171a string that matches the regular expression.
172If necessary
173the search wraps around to the end of the buffer.
174.TP
1757.
176An address followed by a plus sign
177.L +
178or a minus sign
179.L -
180followed by a decimal number specifies that address plus
181(resp. minus) the indicated number of lines.
182The plus sign may be omitted.
183.TP
1848.
185An address followed by
186.L +
187(or
188.LR - )
189followed by a
190regular expression enclosed in slashes specifies the first
191matching line following (or preceding) that address.
192The search wraps around if necessary.
193The
194.L +
195may be omitted, so
196.L 0/x/
197addresses the
198.I first
199line in the buffer with an
200.LR x .
201Enclosing the regular expression in
202.L ?
203reverses the search direction.
204.TP
2059.
206If an address begins with
207.L +
208or
209.L -
210the addition or subtraction is taken with respect to the current line;
211e.g.\&
212.L -5
213is understood to mean
214.LR .-5 .
215.TP
21610.
217If an address ends with
218.L +
219or
220.LR - ,
221then 1 is added (resp. subtracted).
222As a consequence of this rule and rule 9,
223the address
224.L -
225refers to the line before the current line.
226Moreover,
227trailing
228.L +
229and
230.L -
231characters
232have cumulative effect, so
233.L --
234refers to the current
235line less 2.
236.TP
23711.
238To maintain compatibility with earlier versions of the editor,
239the character
240.L ^
241in addresses is
242equivalent to
243.LR - .
244.PP
245Commands may require zero, one, or two addresses.
246Commands which require no addresses regard the presence
247of an address as an error.
248Commands which accept one or two addresses
249assume default addresses when insufficient are given.
250If more addresses are given than a command requires,
251the last one or two (depending on what is accepted) are used.
252.PP
253Addresses are separated from each other typically by a comma
254.LR , .
255They may also be separated by a semicolon
256.LR ; .
257In this case the current line
258is set to
259the previous address before the next address is interpreted.
260If no address precedes a comma or semicolon, line 1 is assumed;
261if no address follows, the last line of the buffer is assumed.
262The second address of any two-address sequence
263must correspond to a line following the line corresponding to the first address.
264.PP
265In the following list of
266.I ed
267commands, the default addresses
268are shown in parentheses.
269The parentheses are not part of
270the address, but are used to show that the given addresses are
271the default.
272`Dot' means the current line.
273.TP
274.RB (\|\fL.\fP\|) \|a
275.br
276.ns
277.TP
278<text>
279.br
280.ns
281.TP
282.B .
283Read the given text
284and append it after the addressed line.
285Dot is left
286on the last line input, if there
287were any, otherwise at the addressed line.
288Address
289.L 0
290is legal for this command; text is placed
291at the beginning of the buffer.
292.TP
293.RB (\|\fL.,.\fP\|) \|b [ +- ][\fIpagesize\fP][ pln\fR]
294Browse.
295Print a `page', normally 20 lines.
296The optional
297.L +
298(default) or
299.L -
300specifies whether the next or previous
301page is to be printed.
302The optional
303.I pagesize
304is the number of lines in a page.
305The optional
306.LR p ,
307.LR n ,
308or
309.L l
310causes printing in the specified format, initially
311.LR p .
312Pagesize and format are remembered between
313.L b
314commands.
315Dot is left at the last line displayed.
316.TP
317.RB (\|\fL.,.\fP\|) \|c
318.br
319.ns
320.TP
321<text>
322.br
323.ns
324.TP
325.B .
326Change.
327Delete the addressed lines, then accept input
328text to replace these lines.
329Dot is left at the last line input; if there were none,
330it is left at the line preceding the deleted lines.
331.TP
332.RB (\|\fL.,.\fP\|) \|d
333Delete the addressed lines from the buffer.
334Dot is set to the line following the last line deleted, or to
335the last line of the buffer if the deleted lines had no successor.
336.TP
337.BI e " filename"
338Edit.
339Delete the entire contents of the buffer;
340then read the named file into the buffer.
341Dot is set to the last line of the buffer.
342The number of characters read is typed.
343The file name is remembered for possible use in later
344.LR e ,
345.LR r ,
346or
347.L w
348commands.
349If
350.I filename
351is missing, the remembered name is used.
352.TP
353.BI E " filename"
354Unconditional
355.LR e ;
356see
357.RL ` q '
358below.
359.TP
360.BI f " filename"
361Print the currently remembered file name.
362If
363.I filename
364is given,
365the currently remembered file name is first changed to
366.IR filename .
367.TP
368.RB (\|\fL1,$\fP\|) \|g/\fIregular\ expression\fP/\fIcommand\ list\fP
369.PD 0
370.TP
371.RB (\|\fL1,$\fP\|) \|g/\fIregular\ expression\fP/
372.TP
373.RB (\|\fL1,$\fP\|) \|g/\fIregular\ expression\fP
374.PD
375Global.
376First mark every line which matches
377the given
378.IR regular expression .
379Then for every such line, execute the
380.I command list
381with dot 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 end with
385.LR \e .
386The
387.RB \&` \&. \&'
388terminating input mode for an
389.LR a ,
390.LR i ,
391.L c
392command may be omitted if it would be on the
393last line of the command list.
394The commands
395.L g
396and
397.L v
398are not permitted in the command list.
399Any character other than space or newline may
400be used instead of
401.L /
402to delimit the regular expression.
403The second and third forms mean
404.BI g/ regular\ expression /p \f1.
405.TP
406.RB (\| .\| ) \|i
407.PD 0
408.TP
409<text>
410.TP
411.B .
412Insert the given text before the addressed line.
413Dot is left at the last line input, or, if there were none,
414at the line before the addressed line.
415This command differs from the
416.I a
417command only in the placement of the
418text.
419.PD
420.TP
421.RB (\| .,.+1 \|) \|j
422Join the addressed lines into a single line;
423intermediate newlines are deleted.
424Dot is left at the resulting line.
425.TP
426.RB (\|\fL.\fP\|) \|k\fIx\fP
427Mark the addressed line with name
428.IR x ,
429which must be a lower-case letter.
430The address form
431.BI \' x
432then addresses this line.
433.ne 2.5
434.TP
435.RB (\|\fL.,.\fP\|) \|l
436List.
437Print the addressed lines in an unambiguous way:
438a tab is printed as
439.LR \et ,
440a backspace as
441.LR \eb ,
442backslashes as
443.LR \e\e ,
444and non-printing characters as
445a backslash, an
446.LR x ,
447and four hexadecimal digits.
448Long lines are folded,
449with the second and subsequent sub-lines indented one tab stop.
450If the last character in the line is a blank,
451it is followed by
452.LR \en .
453An
454.L l
455may be appended, like
456.LR p ,
457to any non-I/O command.
458.TP
459.RB (\|\fL.,.\fP\|) \|m\fIa
460Move.
461Reposition the addressed lines after the line
462addressed by
463.IR a .
464Dot is left at the last moved line.
465.TP
466.RB (\|\fL.,.\fP\|) \|n
467Number.
468Perform
469.LR p ,
470prefixing each line with its line number and a tab.
471An
472.L n
473may be appended, like
474.LR p ,
475to any non-I/O command.
476.TP
477.RB (\|\fL.,.\fP\|) \|p
478Print the addressed lines.
479Dot is left at the last line printed.
480A
481.L p
482appended to any non-I/O command causes the then current line
483to be printed after the command is executed.
484.TP
485.RB (\|\fL.,.\fP\|) \|P
486This command is a synonym for
487.LR p .
488.TP
489.B q
490Quit the editor.
491No automatic write
492of a file is done.
493A
494.L q
495or
496.L e
497command is considered to be in error if the buffer has
498been modified since the last
499.LR w ,
500.LR q ,
501or
502.L e
503command.
504.TP
505.B Q
506Quit unconditionally.
507.TP
508.RB ( $ )\|r\ \fIfilename\fP
509Read in the given file after the addressed line.
510If no
511.I filename
512is given, the remembered file name is used.
513The file name is remembered if there were no
514remembered file name already.
515If the read is successful, the number of characters
516read is printed.
517Dot is left at the last line read from the file.
518.TP
519.RB (\|\fL.,.\fP\|) \|s\fIn\fP/\fIregular\ expression\fP/\fIreplacement\fP/
520.PD 0
521.TP
522.RB (\|\fL.,.\fP\|) \|s\fIn\fP/\fIregular\ expression\fP/\fIreplacement\fP/g
523.TP
524.RB (\|\fL.,.\fP\|) \|s\fIn\fP/\fIregular\ expression\fP/\fIreplacement\fP
525.PD
526Substitute.
527Search each addressed
528line for an occurrence of the specified regular expression.
529On each line in which
530.I n
531matches are found
532.RI ( n
533defaults to 1 if missing),
534the
535.IR n th
536matched string is replaced by the replacement specified.
537If the global replacement indicator
538.L g
539appears after the command,
540all subsequent matches on the line are also replaced.
541It is an error for the substitution to fail on all addressed lines.
542Any character other than space or newline
543may be used instead of
544.L /
545to delimit the regular expression
546and the replacement.
547Dot is left at the last line substituted.
548The third form means
549.BI s n / regular\ expression / replacement\fP/p\f1.
550The second
551.L /
552may be omitted if the replacement is
553empty.
554.IP
555An ampersand
556.L &
557appearing in the replacement
558is replaced by the string matching the regular expression.
559The characters
560.BI \e n\f1,
561where
562.I n
563is a digit,
564are replaced by the text matched by the
565.IR n -th
566regular subexpression
567enclosed between
568.L (
569and
570.LR ) .
571When
572nested parenthesized subexpressions
573are present,
574.I n
575is determined by counting occurrences of
576.L (
577starting from the left.
578.IP
579A literal
580.LR & ,
581.LR / ,
582.L \e
583or newline may be included in a replacement
584by prefixing it with
585.LR \e .
586.TP
587.RB (\|\fL.,.\fP\|) \|t\|\fIa
588Transfer.
589Copy the addressed lines
590after the line addressed by
591.IR a .
592Dot is left at the last line of the copy.
593.TP
594.RB (\|\fL.,.\fP\|) \|u
595Undo.
596Restore the preceding contents
597of the first addressed line (sic), which must be the last line
598in which a substitution was made (double sic).
599.TP
600.RB (\|\fL1,$\fP\|) \|v/\fIregular\ expression\fP/\fIcommand\ list\fP
601This command is the same as the global command
602.L g
603except that the command list is executed with
604dot initially set to every line
605.I except
606those
607matching the regular expression.
608.TP
609.RB (\|\fL1,$\fP\|) \|w " \fIfilename\fP"
610Write the addressed lines to
611the given file.
612If the file does not exist,
613it is created with mode 666 (readable and writable by everyone).
614If no
615.I filename
616is given, the remembered file name, if any, is used.
617The file name is remembered if there were no
618remembered file name already.
619Dot is unchanged.
620If the write is successful, the number of characters written is
621printed.
622.TP
623.RB (\|\fL1,$\fP\|) \|W " \fIfilename\fP"
624Perform
625.LR w ,
626but append to, instead of overwriting, any existing file contents.
627.TP
628.RB ( $ ) \|=
629Print the line number of the addressed line.
630Dot is unchanged.
631.TP
632.BI ! shell\ command
633Send the remainder of the line after the
634.L !
635to
636.IR rc (1)
637to be interpreted as a command.
638Dot is unchanged.
639.TP
640.RB (\| .+1 )\|<newline>
641An address without a command is taken as a
642.L p
643command.
644A terminal
645.L /
646may be omitted from the address.
647A blank line alone is equivalent to
648.LR .+1p ;
649it is useful
650for stepping through text.
651.PP
652If an interrupt signal
653.SM (DEL)
654is sent,
655.I ed
656prints a
657.L ?
658and returns to its command level.
659.PP
660When reading a file,
661.I ed
662discards
663.SM NUL
664characters
665and all characters after the last newline.
666.SH FILES
667.B /tmp/e*
668.br
669.B ed.hup
670\ \ work is saved here if terminal hangs up
671.SH SOURCE
rscb5fdffe2004-04-19 19:22:56 +0000672.B /usr/local/plan9/src/cmd/ed.c
rsccfa37a72004-04-10 18:53:55 +0000673.SH "SEE ALSO"
674.IR sam (1),
675.IR sed (1),
676.IR regexp (6)
677.SH DIAGNOSTICS
678.BI ? name
679for inaccessible file;
680.L ?TMP
681for temporary file overflow;
682.L ?
683for errors in commands or other overflows.