1ED(1) General Commands Manual ED(1)
2
3
4
6 ed, red - text editor
7
9 ed [-GVhs] [-p string] [file]
10
11 red [-GVhs] [-p string] [file]
12
14 ed is a line-oriented text editor. It is used to create, display, mod‐
15 ify and otherwise manipulate text files. red is a restricted ed: it
16 can only edit files in the current directory and cannot execute shell
17 commands.
18
19 If invoked with a file argument, then a copy of file is read into the
20 editor's buffer. Changes are made to this copy and not directly to
21 file itself. Upon quitting ed, any changes not explicitly saved with
22 a `w' command are lost.
23
24 Editing is done in two distinct modes: command and input. When first
25 invoked, ed is in command mode. In this mode commands are read from
26 the standard input and executed to manipulate the contents of the edi‐
27 tor buffer. A typical command might look like:
28
29 ,s/old/new/g
30
31 which replaces all occurences of the string old with new.
32
33 When an input command, such as `a' (append), `i' (insert) or `c'
34 (change), is given, ed enters input mode. This is the primary means of
35 adding text to a file. In this mode, no commands are available;
36 instead, the standard input is written directly to the editor buffer.
37 Lines consist of text up to and including a newline character. Input
38 mode is terminated by entering a single period (.) on a line.
39
40 All ed commands operate on whole lines or ranges of lines; e.g., the
41 `d' command deletes lines; the `m' command moves lines, and so on. It
42 is possible to modify only a portion of a line by means of replacement,
43 as in the example above. However even here, the `s' command is applied
44 to whole lines at a time.
45
46 In general, ed commands consist of zero or more line addresses, fol‐
47 lowed by a single character command and possibly additional parameters;
48 i.e., commands have the structure:
49
50 [address [,address]]command[parameters]
51
52 The address(es) indicate the line or range of lines to be affected by
53 the command. If fewer addresses are given than the command accepts,
54 then default addresses are supplied.
55
56
57 OPTIONS
58 -G Forces backwards compatibility. Affects the commands `G', `V',
59 `f', `l', `m', `t', and `!!'.
60
61 -s Suppresses diagnostics. This should be used if ed's standard
62 input is from a script.
63
64
65 -p string
66 Specifies a command prompt. This may be toggled on and off
67 with the `P' command.
68
69
70 file Specifies the name of a file to read. If file is prefixed with
71 a bang (!), then it is interpreted as a shell command. In this
72 case, what is read is the standard output of file executed via
73 sh(1). To read a file whose name begins with a bang, prefix
74 the name with a backslash (\). The default filename is set to
75 file only if it is not prefixed with a bang.
76
77
78 LINE ADDRESSING
79 An address represents the number of a line in the buffer. ed maintains
80 a current address which is typically supplied to commands as the
81 default address when none is specified. When a file is first read,
82 the current address is set to the last line of the file. In general,
83 the current address is set to the last line affected by a command.
84
85 A line address is constructed from one of the bases in the list below,
86 optionally followed by a numeric offset. The offset may include any
87 combination of digits, operators (i.e. + and -) and whitespace.
88 Addresses are read from left to right, and their values are computed
89 relative to the current address.
90
91 One exception to the rule that addresses represent line numbers is the
92 address 0 (zero). This means "before the first line," and is legal
93 wherever it makes sense.
94
95 An address range is two addresses separated either by a comma or semi‐
96 colon. The value of the first address in a range cannot exceed the
97 value of the the second. If only one address is given in a range, then
98 the second address is set to the given address. If an n-tuple of
99 addresses is given where n > 2, then the corresponding range is deter‐
100 mined by the last two addresses in the n-tuple. If only one address is
101 expected, then the last address is used.
102
103 Each address in a comma-delimited range is interpreted relative to the
104 current address. In a semicolon-delimited range, the first address is
105 used to set the current address, and the second address is interpreted
106 relative to the first.
107
108
109 The following address symbols are recognized.
110
111
112 . The current line (address) in the buffer.
113
114
115 $ The last line in the buffer.
116
117
118 n The nth, line in the buffer where n is a number in the range
119 [0,$].
120
121
122 - The previous line. This is equivalent to -1 and may be
123 repeated with cumulative effect.
124
125
126 -n The nth previous line, where n is a non-negative number.
127
128
129 + The next line. This is equivalent to +1 and may be repeated
130 with cumulative effect.
131
132
133 whitespace n
134
135 +n The nth next line, where n is a non-negative number. White‐
136 space followed by a number n is interpreted as +n.
137
138
139 , The first through last lines in the buffer. This is equivalent
140 to the address range 1,$.
141
142
143 ; The current through last lines in the buffer. This is equiva‐
144 lent to the address range .,$.
145
146
147 /re/ The next line containing the regular expression re. The search
148 wraps to the beginning of the buffer and continues down to the
149 current line, if necessary. // repeats the last search.
150
151
152 ?re? The previous line containing the regular expression re. The
153 search wraps to the end of the buffer and continues up to the
154 current line, if necessary. ?? repeats the last search.
155
156
157 ´lc The line previously marked by a `k' (mark) command, where lc is
158 a lower case letter.
159
160
161 REGULAR EXPRESSIONS
162 Regular expressions are patterns used in selecting text. For example,
163 the ed command
164
165 g/string/
166
167 prints all lines containing string. Regular expressions are also used
168 by the `s' command for selecting old text to be replaced with new.
169
170 In addition to a specifying string literals, regular expressions can
171 represent classes of strings. Strings thus represented are said to be
172 matched by the corresponding regular expression. If it is possible for
173 a regular expression to match several strings in a line, then the left-
174 most longest match is the one selected.
175
176 The following symbols are used in constructing regular expressions:
177
178
179 c Any character c not listed below, including `{', '}', `(', `)',
180 `<' and `>', matches itself.
181
182
183 \c A backslash-escaped character c other than `{', '}', `(', `)',
184 `<', `>', `b', 'B', `w', `W', `+', and `?' matches itself.
185
186
187 . Matches any single character.
188
189
190 [char-class]
191 Matches any single character in char-class. To include a `]'
192 in char-class, it must be the first character. A range of
193 characters may be specified by separating the end characters of
194 the range with a `-', e.g., `a-z' specifies the lower case
195 characters. The following literal expressions can also be used
196 in char-class to specify sets of characters:
197
198 [:alnum:] [:cntrl:] [:lower:] [:space:]
199 [:alpha:] [:digit:] [:print:] [:upper:]
200 [:blank:] [:graph:] [:punct:] [:xdigit:]
201
202 If `-' appears as the first or last character of char-class,
203 then it matches itself. All other characters in char-class
204 match themselves.
205
206 Patterns in char-class of the form:
207
208 [.col-elm.] or, [=col-elm=]
209
210 where col-elm is a collating element are interpreted according
211 to locale(5) (not currently supported). See regex(3) for an
212 explanation of these constructs.
213
214 [^char-class]
215 Matches any single character, other than newline, not in char-
216 class. char-class is defined as above.
217
218 ^ If `^' is the first character of a regular expression, then it
219 anchors the regular expression to the beginning of a line.
220 Otherwise, it matches itself.
221
222 $ If `$' is the last character of a regular expression, it
223 anchors the regular expression to the end of a line. Other‐
224 wise, it matches itself.
225
226 \(re\) Defines a (possibly null) subexpression re. Subexpressions may
227 be nested. A subsequent backreference of the form `\n', where
228 n is a number in the range [1,9], expands to the text matched
229 by the nth subexpression. For example, the regular expression
230 `\(a.c\)\1' matches the string `abcabc', but not `abcadc'.
231 Subexpressions are ordered relative to their left delimiter.
232
233 * Matches the single character regular expression or subexpres‐
234 sion immediately preceding it zero or more times. If '*' is
235 the first character of a regular expression or subexpression,
236 then it matches itself. The `*' operator sometimes yields
237 unexpected results. For example, the regular expression `b*'
238 matches the beginning of the string `abbb', as opposed to the
239 substring `bbb', since a null match is the only left-most
240 match.
241
242 \{n,m\}
243 \{n,\}
244 \{n\} Matches the single character regular expression or subexpres‐
245 sion immediately preceding it at least n and at most m times.
246 If m is omitted, then it matches at least n times. If the
247 comma is also omitted, then it matches exactly n times. If
248 any of these forms occurs first in a regular expression or sub‐
249 expression, then it is interpreted literally (i.e., the regular
250 expression `\{2\}' matches the string `{2}', and so on).
251
252 \<
253 \> Anchors the single character regular expression or subexpres‐
254 sion immediately following it to the beginning (\<) or ending
255 (\>) of a word, i.e., in ASCII, a maximal string of alphanu‐
256 meric characters, including the underscore (_).
257
258
259 The following extended operators are preceded by a backslash (\) to
260 distinguish them from traditional ed syntax.
261
262 \`
263 \' Unconditionally matches the beginning (\`) or ending (\') of a
264 line.
265
266 \? Optionally matches the single character regular expression or
267 subexpression immediately preceding it. For example, the regu‐
268 lar expression `a[bd]\?c' matches the strings `abc', `adc' and
269 `ac'. If \? occurs at the beginning of a regular expressions
270 or subexpression, then it matches a literal `?'.
271
272 \+ Matches the single character regular expression or subexpres‐
273 sion immediately preceding it one or more times. So the regu‐
274 lar expression `a+' is shorthand for `aa*'. If \+ occurs at
275 the beginning of a regular expression or subexpression, then it
276 matches a literal `+'.
277
278
279 \b Matches the beginning or ending (null string) of a word. Thus
280 the regular expression `\bhello\b' is equivalent to
281 `\<hello\>'. However, `\b\b' is a valid regular expression
282 whereas `\<\>' is not.
283
284 \B Matches (a null string) inside a word.
285
286 \w Matches any character in a word.
287
288 \W Matches any character not in a word.
289
290 COMMANDS
291 All ed commands are single characters, though some require additonal
292 parameters. If a command's parameters extend over several lines, then
293 each line except for the last must be terminated with a backslash (\).
294
295 In general, at most one command is allowed per line. However, most
296 commands accept a print suffix, which is any of `p' (print), `l' (list)
297 , or `n' (enumerate), to print the last line affected by the command.
298
299 An interrupt (typically ^C) has the effect of aborting the current com‐
300 mand and returning the editor to command mode.
301
302 ed recognizes the following commands. The commands are shown together
303 with the default address or address range supplied if none is specified
304 (in parenthesis).
305
306 (.)a Appends text to the buffer after the addressed line, which may
307 be the address 0 (zero). Text is entered in input mode. The
308 current address is set to last line entered.
309
310 (.,.)c Changes lines in the buffer. The addressed lines are deleted
311 from the buffer, and text is appended in their place. Text is
312 entered in input mode. The current address is set to last line
313 entered.
314
315 (.,.)d Deletes the addressed lines from the buffer. If there is a
316 line after the deleted range, then the current address is set
317 to this line. Otherwise the current address is set to the line
318 before the deleted range.
319
320 e file Edits file, and sets the default filename. If file is not
321 specified, then the default filename is used. Any lines in
322 the buffer are deleted before the new file is read. The cur‐
323 rent address is set to the last line read.
324
325 e !command
326 Edits the standard output of `!command', (see !command below).
327 The default filename is unchanged. Any lines in the buffer are
328 deleted before the output of command is read. The current
329 address is set to the last line read.
330
331 E file Edits file unconditionally. This is similar to the e command,
332 except that unwritten changes are discarded without warning.
333 The current address is set to the last line read.
334
335 f file Sets the default filename to file. If file is not specified,
336 then the default unescaped filename is printed.
337
338 (1,$)g/re/command-list
339 Applies command-list to each of the addressed lines matching a
340 regular expression re. The current address is set to the line
341 currently matched before command-list is executed. At the end
342 of the `g' command, the current address is set to the last line
343 affected by command-list.
344
345 Each command in command-list must be on a separate line, and
346 every line except for the last must be terminated by a back‐
347 slash (\). Any commands are allowed, except for `g', `G', `v',
348 and `V'. A newline alone in command-list is equivalent to a
349 `p' command.
350
351 (1,$)G/re/
352 Interactively edits the addressed lines matching a regular
353 expression re. For each matching line, the line is printed,
354 the current address is set, and the user is prompted to enter a
355 command-list. At the end of the `G' command, the current
356 address is set to the last line affected by (the last) command-
357 list.
358
359 The format of command-list is the same as that of the `g' com‐
360 mand. A newline alone acts as a null command list. A single
361 `&' repeats the last non-null command list.
362
363 H Toggles the printing of error explanations. By default, expla‐
364 nations are not printed. It is recommended that ed scripts
365 begin with this command to aid in debugging.
366
367 h Prints an explanation of the last error.
368
369 (.)i Inserts text in the buffer before the current line. Text is
370 entered in input mode. The current address is set to the last
371 line entered.
372
373 (.,.+1)j
374 Joins the addressed lines. The addressed lines are deleted
375 from the buffer and replaced by a single line containing their
376 joined text. The current address is set to the resultant line.
377
378 (.)klc Marks a line with a lower case letter lc. The line can then
379 be addressed as 'lc (i.e., a single quote followed by lc ) in
380 subsequent commands. The mark is not cleared until the line is
381 deleted or otherwise modified.
382
383 (.,.)l Prints the addressed lines unambiguously. If invoked from a
384 terminal, ed pauses at the end of each page until a newline is
385 entered. The current address is set to the last line printed.
386
387 (.,.)m(.)
388 Moves lines in the buffer. The addressed lines are moved to
389 after the right-hand destination address, which may be the
390 address 0 (zero). The current address is set to the last line
391 moved.
392
393 (.,.)n Prints the addressed lines along with their line numbers. The
394 current address is set to the last line printed.
395
396 (.,.)p Prints the addressed lines. If invoked from a terminal, ed
397 pauses at the end of each page until a newline is entered. The
398 current address is set to the last line printed.
399
400 P Toggles the command prompt on and off. Unless a prompt was
401 specified by with command-line option -p string, the command
402 prompt is by default turned off.
403
404 q Quits ed.
405
406 Q Quits ed unconditionally. This is similar to the q command,
407 except that unwritten changes are discarded without warning.
408
409 ($)r file
410 Reads file to after the addressed line. If file is not speci‐
411 fied, then the default filename is used. If there was no
412 default filename prior to the command, then the default file‐
413 name is set to file. Otherwise, the default filename is
414 unchanged. The current address is set to the last line read.
415
416 ($)r !command
417 Reads to after the addressed line the standard output of `!com‐
418 mand', (see the !command below). The default filename is
419 unchanged. The current address is set to the last line read.
420
421 (.,.)s/re/replacement/
422 (.,.)s/re/replacement/g
423 (.,.)s/re/replacement/n
424 Replaces text in the addressed lines matching a regular expres‐
425 sion re with replacement. By default, only the first match in
426 each line is replaced. If the `g' (global) suffix is given,
427 then every match to be replaced. The `n' suffix, where n is a
428 postive number, causes only the nth match to be replaced. It
429 is an error if no substitutions are performed on any of the
430 addressed lines. The current address is set the last line
431 affected.
432
433 re and replacement may be delimited by any character other than
434 space and newline (see the `s' command below). If one or two
435 of the last delimiters is omitted, then the last line affected
436 is printed as though the print suffix `p' were specified.
437
438
439 An unescaped `&' in replacement is replaced by the currently
440 matched text. The character sequence `\m', where m is a number
441 in the range [1,9], is replaced by the mth backreference
442 expression of the matched text. If replacement consists of a
443 single `%', then replacement from the last substitution is
444 used. Newlines may be embedded in replacement if they are
445 escaped with a backslash (\).
446
447 (.,.)s Repeats the last substitution. This form of the `s' command
448 accepts a count suffix `n', or any combination of the charac‐
449 ters `r', `g', and `p'. If a count suffix `n' is given, then
450 only the nth match is replaced. The `r' suffix causes the reg‐
451 ular expression of the last search to be used instead of the
452 that of the last substitution. The `g' suffix toggles the
453 global suffix of the last substitution. The `p' suffix toggles
454 the print suffix of the last substitution The current address
455 is set to the last line affected.
456
457 (.,.)t(.)
458 Copies (i.e., transfers) the addressed lines to after the
459 right-hand destination address, which may be the address 0
460 (zero). The current address is set to the last line copied.
461
462 u Undoes the last command and restores the current address to
463 what it was before the command. The global commands `g', `G',
464 `v', and `V'. are treated as a single command by undo. `u' is
465 its own inverse.
466
467 (1,$)v/re/command-list
468 Applies command-list to each of the addressed lines not match‐
469 ing a regular expression re. This is similar to the `g' com‐
470 mand.
471
472 (1,$)V/re/
473 Interactively edits the addressed lines not matching a regular
474 expression re. This is similar to the `G' command.
475
476 (1,$)w file
477 Writes the addressed lines to file. Any previous contents of
478 file is lost without warning. If there is no default filename,
479 then the default filename is set to file, otherwise it is
480 unchanged. If no filename is specified, then the default file‐
481 name is used. The current address is unchanged.
482
483 (1,$)wq file
484 Writes the addressed lines to file, and then executes a `q'
485 command.
486
487 (1,$)w !command
488 Writes the addressed lines to the standard input of `!command',
489 (see the !command below). The default filename and current
490 address are unchanged.
491
492 (1,$)W file
493 Appends the addressed lines to the end of file. This is simi‐
494 lar to the `w' command, expect that the previous contents of
495 file is not clobbered. The current address is unchanged.
496
497 (.)x Copies (puts) the contents of the cut buffer to after the
498 addressed line. The current address is set to the last line
499 copied.
500
501 (.,.)y Copies (yanks) the addressed lines to the cut buffer. The cut
502 buffer is overwritten by subsequent `y', `s', `j', `d', or `c'
503 commands. The current address is unchanged.
504
505 (.+1)zn Scrolls n lines at a time starting at addressed line. If n is
506 not specified, then the current window size is used. The cur‐
507 rent address is set to the last line printed.
508
509 !command
510 Executes command via sh(1). If the first character of command
511 is `!', then it is replaced by text of the previous `!command'.
512 ed does not process command for backslash (\) escapes. How‐
513 ever, an unescaped `%' is replaced by the default filename.
514 When the shell returns from execution, a `!' is printed to the
515 standard output. The current line is unchanged.
516
517 (.,.)# Begins a comment; the rest of the line, up to a newline, is
518 ignored. If a line address followed by a semicolon is given,
519 then the current address is set to that address. Otherwise,
520 the current address is unchanged.
521
522 ($)= Prints the line number of the addressed line.
523
524 (.+1)newline
525 Prints the addressed line, and sets the current address to that
526 line.
527
529 ed.hup The file to which ed attempts to write the buffer if the ter‐
530 minal hangs up.
531
533 vi(1), sed(1), regex(3), sh(1).
534
535 USD:12-13
536
537 B. W. Kernighan and P. J. Plauger, Software Tools in Pascal , Addison-
538 Wesley, 1981.
539
541 ed processes file arguments for backslash escapes, i.e., in a file‐
542 name, any characters preceded by a backslash (\) are interpreted liter‐
543 ally.
544
545 If a text (non-binary) file is not terminated by a newline character,
546 then ed appends one on reading/writing it. In the case of a binary
547 file, ed does not append a newline on reading/writing.
548
549 per line overhead: 4 ints
550
552 When an error occurs, if ed's input is from a regular file or here doc‐
553 ument, then it exits, otherwise it prints a `?' and returns to command
554 mode. An explanation of the last error can be printed with the `h'
555 (help) command.
556
557 Attempting to quit ed or edit another file before writing a modified
558 buffer results in an error. If the command is entered a second time,
559 it succeeds, but any changes to the buffer are lost.
560
561 ed exits with 0 if no errors occurred; otherwise >0.
562
563
564
565 5 October 2006 ED(1)