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