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