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