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