1.\" $OpenBSD: ed.1,v 1.61 2014/04/14 23:19:51 jmc Exp $ 2.\" 3.\" Copyright (c) 1993 Andrew Moore, Talke Studio. 4.\" All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 15.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25.\" SUCH DAMAGE. 26.\" 27.Dd $Mdocdate: April 14 2014 $ 28.Dt ED 1 29.Os 30.Sh NAME 31.Nm ed 32.Nd text editor 33.Sh SYNOPSIS 34.Nm ed 35.Op Fl 36.Op Fl s 37.Op Fl p Ar string 38.Op Ar file 39.Sh DESCRIPTION 40.Nm 41is a line-oriented text editor. 42It is used to create, display, modify, and otherwise manipulate text files. 43If invoked with a 44.Ar file 45argument, then a copy of 46.Ar file 47is read into the editor's buffer. 48Changes are made to this copy and not directly to 49.Ar file 50itself. 51Upon quitting 52.Nm ed , 53any changes not explicitly saved with a 54.Ic w 55command are lost. 56.Pp 57Editing is done in two distinct modes: 58.Em command 59and 60.Em input . 61When first invoked, 62.Nm 63is in command mode. 64In this mode, commands are read from the standard input and 65executed to manipulate the contents of the editor buffer. 66.Pp 67A typical command might look like: 68.Pp 69.Dl ,s/old/new/g 70.Pp 71which replaces all occurrences of the string 72.Qq old 73with 74.Qq new . 75.Pp 76When an input command, such as 77.Ic a 78.Pq append , 79.Ic i 80.Pq insert , 81or 82.Ic c 83.Pq change 84is given, 85.Nm 86enters input mode. 87This is the primary means of adding text to a file. 88In this mode, no commands are available; 89instead, the standard input is written directly to the editor buffer. 90Lines consist of text up to and including a newline character. 91Input mode is terminated by entering a single period 92.Pq Ql \&. 93on a line. 94.Pp 95All 96.Nm 97commands operate on whole lines or ranges of lines; e.g., 98the 99.Ic d 100command deletes lines; the 101.Ic m 102command moves lines, and so on. 103It is possible to modify only a portion of a line by means of replacement, 104as in the example above. 105However, even here, the 106.Ic s 107command is applied to whole lines at a time. 108.Pp 109In general, 110.Nm 111commands consist of zero or more line addresses, followed by a single 112character command and possibly additional parameters; i.e., 113commands have the structure: 114.Pp 115.Dl [address [,address]]command[parameters] 116.Pp 117The address(es) indicate the line or range of lines to be affected by the 118command. 119If fewer addresses are given than the command accepts, then 120default addresses are supplied. 121.Pp 122Many 123.Nm 124commands and line addresses support basic regular expressions 125.Pq BREs . 126See 127.Xr re_format 7 128for more information on regular expressions. 129.Pp 130The options are as follows: 131.Bl -tag -width "-p string" 132.It Fl 133Same as the 134.Fl s 135option 136.Pq deprecated . 137.It Fl p Ar string 138Specifies a command prompt. 139This may be toggled on and off with the 140.Ic P 141command. 142.It Fl s 143Suppress diagnostics. 144This should be used if 145.Nm 146standard input is from a script. 147.It Ar file 148Specifies the name of a file to read. 149If 150.Ar file 151is prefixed with a 152bang 153.Pq Ql \&! , 154then it is interpreted as a shell command. 155In this case, what is read is the standard output of 156.Ar file 157executed via 158.Xr sh 1 . 159To read a file whose name begins with a bang, prefix the 160name with a backslash 161.Pq Ql \e . 162The default filename is set to 163.Ar file 164only if it is not prefixed with a bang. 165.El 166.Ss LINE ADDRESSING 167An address represents the number of a line in the buffer. 168.Nm 169maintains a 170.Em current address 171which is typically supplied to commands as the default address 172when none is specified. 173When a file is first read, the current address is set to the last line 174of the file. 175In general, the current address is set to the last line affected by a command. 176.Pp 177A line address is 178constructed from one of the bases in the list below, optionally followed 179by a numeric offset. 180The offset may include any combination of digits, operators (e.g., 181.Ql + , 182.Ql - , 183and 184.Ql ^ ) , 185and whitespace. 186Addresses are read from left to right, and their values are computed 187relative to the current address. 188.Pp 189One exception to the rule that addresses represent line numbers is the 190address 191.Ad 0 192.Pq zero . 193This means 194.Dq before the first line , 195and is legal wherever it makes sense. 196.Pp 197An address range is two addresses separated either by a comma or semi-colon. 198The value of the first address in a range cannot exceed the 199value of the second. 200If only one address is given in a range, 201then the second address is set to the given address. 202If an 203.Ar n Ns -tuple 204of addresses is given where 205.Ar n 206\*(Gt 2, 207then the corresponding range is determined by the last two addresses in the 208.Ar n Ns -tuple . 209If only one address is expected, then the last address is used. 210.Pp 211Each address in a comma-delimited range is interpreted relative to the 212current address. 213In a semi-colon-delimited range, the first address is 214used to set the current address, and the second address is interpreted 215relative to the first. 216.Pp 217The following address symbols are recognized: 218.Bl -tag -width Ds 219.It \&. 220The current line 221.Pq address 222in the buffer. 223.It $ 224The last line in the buffer. 225.It Ar n 226The 227.Ar n Ns th 228line in the buffer, where 229.Ar n 230is a number in the range 231.Ad [0,$] . 232.It - or ^ 233The previous line. 234This is equivalent to 235.Ad \-1 236and may be repeated with cumulative effect. 237.It Xo 238.Pf - Ns Ar n No or\ \& 239.Pf ^ Ns Ar n 240.Xc 241The 242.Ar n Ns th 243previous line, where 244.Ar n 245is a non-negative number. 246.It + 247The next line. 248This is equivalent to 249.Ad +1 250and may be repeated with cumulative effect. 251.It + Ns Ar n 252The 253.Ar n Ns th 254next line, where 255.Ar n 256is a non-negative number. 257.It \&, or % 258The first through last lines in the buffer. 259This is equivalent to the address range 260.Ad 1,$ . 261.It \&; 262The current through last lines in the buffer. 263This is equivalent to the address range 264.Ad .,$ . 265.It / Ns Ar re Ns / 266The next line containing the regular expression 267.Ar re . 268The search wraps to the beginning of the buffer and continues down to the 269current line, if necessary. 270.Qq // 271repeats the last search. 272.It ? Ns Ar re Ns ? 273The previous line containing the regular expression 274.Ar re . 275The search wraps to the end of the buffer and continues up to the 276current line, if necessary. 277.Qq ?? 278repeats the last search. 279.It \&' Ns Ar lc 280The line previously marked by a 281.Ic k 282.Pq mark 283command, where 284.Ar lc 285is a lower case letter. 286.El 287.Ss COMMANDS 288All 289.Nm 290commands are single characters, though some require additional parameters. 291If a command's parameters extend over several lines, then 292each line except for the last must be terminated with a backslash 293.Pq Ql \e . 294.Pp 295In general, at most one command is allowed per line. 296However, most commands accept a print suffix, which is any of 297.Ic p 298.Pq print , 299.Ic l 300.Pq list , 301or 302.Ic n 303.Pq enumerate , 304to print the last line affected by the command. 305.Pp 306.Nm 307recognizes the following commands. 308The commands are shown together with 309the default address or address range supplied if none is specified 310.Pq in parentheses , 311and other possible arguments on the right. 312.Bl -tag -width Dxxs 313.It (.) Ns Ic a 314Appends text to the buffer after the addressed line. 315Text is entered in input mode. 316The current address is set to last line entered. 317.It (.,.) Ns Ic c 318Changes lines in the buffer. 319The addressed lines are deleted from the buffer, 320and text is appended in their place. 321Text is entered in input mode. 322The current address is set to last line entered. 323.It (.,.) Ns Ic d 324Deletes the addressed lines from the buffer. 325If there is a line after the deleted range, then the current address is set 326to this line. 327Otherwise the current address is set to the line before the deleted range. 328.It Ic e Ar file 329Edits 330.Ar file , 331and sets the default filename. 332If 333.Ar file 334is not specified, then the default filename is used. 335Any lines in the buffer are deleted before the new file is read. 336The current address is set to the last line read. 337.It Ic e No \&! Ns Ar command 338Edits the standard output of 339.No \&! Ns Ar command , 340(see 341.Ic \&! Ns Ar command 342below). 343The default filename is unchanged. 344Any lines in the buffer are deleted before the output of 345.Ar command 346is read. 347The current address is set to the last line read. 348.It Ic E Ar file 349Edits 350.Ar file 351unconditionally. 352This is similar to the 353.Ic e 354command, except that unwritten changes are discarded without warning. 355The current address is set to the last line read. 356.It Ic f Ar file 357Sets the default filename to 358.Ar file . 359If 360.Ar file 361is not specified, then the default unescaped filename is printed. 362.Sm off 363.It Xo 364.Pf (1,$) Ic g No / 365.Ar re No / Ar command-list 366.Xc 367.Sm on 368Applies 369.Ar command-list 370to each of the addressed lines matching a regular expression 371.Ar re . 372The current address is set to the line currently matched before 373command-list is executed. 374At the end of the 375.Ic g 376command, the current address is set to the last line affected by command-list. 377If no lines were matched, 378the current line number remains unchanged. 379.Pp 380Each command in 381.Ar command-list 382must be on a separate line, 383and every line except for the last must be terminated by a backslash 384.Pq Sq \e . 385Any commands are allowed, except for 386.Ic g , 387.Ic G , 388.Ic v , 389and 390.Ic V . 391A newline alone in command-list is equivalent to a 392.Ic p 393command. 394.Sm off 395.It (1,$) Ic G No / Ar re No / 396.Sm on 397Interactively edits the addressed lines matching a regular expression 398.Ar re . 399For each matching line, the line is printed, the current address is set, 400and the user is prompted to enter a 401.Ar command-list . 402At the end of the 403.Ic G 404command, the current address is set to the last line affected by 405.Pq the last 406command-list. 407If no lines were matched, 408the current line number remains unchanged. 409.Pp 410The format of 411.Ar command-list 412is the same as that of the 413.Ic g 414command. 415A newline alone acts as a null command list. 416A single 417.Sq & 418repeats the last non-null command list. 419.It Ic H 420Toggles the printing of error explanations. 421By default, explanations are not printed. 422It is recommended that 423.Nm 424scripts begin with this command to aid in debugging. 425.It Ic h 426Prints an explanation of the last error. 427.It (.) Ns Ic i 428Inserts text in the buffer before the current line. 429Text is entered in input mode. 430The current address is set to the last line entered. 431.It (.,.+1) Ns Ic j 432Joins the addressed lines. 433The addressed lines are deleted from the buffer and replaced by a single 434line containing their joined text. 435The current address is set to the resultant line. 436.It (.) Ns Ic k Ns Ar lc 437Marks a line with a lower case letter 438.Ar lc . 439The line can then be addressed as 440.Ic ' Ns Ar lc 441(i.e., a single quote followed by 442.Ar lc ) 443in subsequent commands. 444The mark is not cleared until the line is deleted or otherwise modified. 445.It (.,.) Ns Ic l 446Prints the addressed lines unambiguously. 447If a single line fills more than one screen (as might be the case 448when viewing a binary file, for instance), a 449.Dq --More-- 450prompt is printed on the last line. 451.Nm 452waits until the RETURN key is pressed before displaying the next screen. 453The current address is set to the last line printed. 454.It (.,.) Ns Ic m Ns (.) 455Moves lines in the buffer. 456The addressed lines are moved to after the 457right-hand destination address, which may be the address 458.Ad 0 459.Pq zero . 460The current address is set to the last line moved. 461.It (.,.) Ns Ic n 462Prints the addressed lines along with their line numbers. 463The current address is set to the last line printed. 464.It (.,.) Ns Ic p 465Prints the addressed lines. 466The current address is set to the last line printed. 467.It Ic P 468Toggles the command prompt on and off. 469Unless a prompt was specified with the command-line option 470.Fl p Ar string , 471the command prompt is by default turned off. 472.It Ic q 473Quits 474.Nm ed . 475.It Ic Q 476Quits 477.Nm 478unconditionally. 479This is similar to the 480.Ic q 481command, except that unwritten changes are discarded without warning. 482.It ($) Ns Ic r Ar file 483Reads 484.Ar file 485to after the addressed line. 486If 487.Ar file 488is not specified, then the default filename is used. 489If there was no default filename prior to the command, 490then the default filename is set to 491.Ar file . 492Otherwise, the default filename is unchanged. 493The current address is set to the last line read. 494.It ($) Ns Ic r No \&! Ns Ar command 495Reads to after the addressed line the standard output of 496.No \&! Ns Ar command , 497(see 498.Ic \&! Ns Ar command 499below). 500The default filename is unchanged. 501The current address is set to the last line read. 502.Sm off 503.It Xo 504.Pf (.,.) Ic s No / Ar re 505.No / Ar replacement No /\ \& 506.Pf (.,.) Ic s No / Ar re 507.No / Ar replacement No / Ic g\ \& 508.No (.,.) Ic s No / Ar re 509.No / Ar replacement No / Ar n 510.Xc 511.Sm on 512Replaces text in the addressed lines matching a regular expression 513.Ar re 514with 515.Ar replacement . 516By default, only the first match in each line is replaced. 517If the 518.Ic g 519.Pq global 520suffix is given, then every match is replaced. 521The 522.Ar n 523suffix, where 524.Ar n 525is a positive number, causes only the 526.Ar n Ns th 527match to be replaced. 528It is an error if no substitutions are performed on any of the addressed 529lines. 530The current address is set the last line affected. 531.Pp 532.Ar re 533and 534.Ar replacement 535may be delimited by any character other than space and newline 536(see the 537.Ic s 538command below). 539If one or two of the last delimiters is omitted, then the last line 540affected is printed as though the print suffix 541.Ic p 542were specified. 543.Pp 544An unescaped 545.Ql & 546in 547.Ar replacement 548is replaced by the currently matched text. 549The character sequence 550.Pf \e Ns Ar m , 551where 552.Ar m 553is a number in the range [1,9], is replaced by the 554.Ar m Ns th 555backreference expression of the matched text. 556If 557.Ar replacement 558consists of a single 559.Ql % , 560then 561.Ar replacement 562from the last substitution is used. 563Newlines may be embedded in 564.Ar replacement 565if they are escaped with a backslash 566.Pq Ql \e . 567.It (.,.) Ns Ic s 568Repeats the last substitution. 569This form of the 570.Ic s 571command accepts a count suffix 572.Ar n , 573or any combination of the characters 574.Ic r , 575.Ic g , 576and 577.Ic p . 578If a count suffix 579.Ar n 580is given, then only the 581.Ar n Ns th 582match is replaced. 583The 584.Ic r 585suffix causes the regular expression of the last search to be used 586instead of that of the last substitution. 587The 588.Ic g 589suffix toggles the global suffix of the last substitution. 590The 591.Ic p 592suffix toggles the print suffix of the last substitution. 593The current address is set to the last line affected. 594.It (.,.) Ns Ic t Ns (.) 595Copies 596.Pq i.e., transfers 597the addressed lines to after the right-hand destination address, 598which may be the address 599.Ad 0 600.Pq zero . 601The current address is set to the last line copied. 602.It Ic u 603Undoes the last command and restores the current address 604to what it was before the command. 605The global commands 606.Ic g , 607.Ic G , 608.Ic v , 609and 610.Ic V 611are treated as a single command by undo. 612.Ic u 613is its own inverse. 614.Sm off 615.It Xo 616.Pf (1,$) Ic v No / Ar re 617.Pf / Ar command-list 618.Xc 619.Sm on 620Applies 621.Ar command-list 622to each of the addressed lines not matching a regular expression 623.Ar re . 624This is similar to the 625.Ic g 626command. 627.Sm off 628.It Xo 629.Pf (1,$) Ic V No / 630.Ar re No / 631.Xc 632.Sm on 633Interactively edits the addressed lines not matching a regular expression 634.Ar re . 635This is similar to the 636.Ic G 637command. 638.It (1,$) Ns Ic w Ar file 639Writes the addressed lines to 640.Ar file . 641Any previous contents of 642.Ar file 643are lost without warning. 644If there is no default filename, then the default filename is set to 645.Ar file , 646otherwise it is unchanged. 647If no filename is specified, then the default filename is used. 648The current address is unchanged. 649.It (1,$) Ns Ic wq Ar file 650Writes the addressed lines to 651.Ar file , 652and then executes a 653.Ic q 654command. 655.It (1,$) Ns Ic w No \&! Ns Ar command 656Writes the addressed lines to the standard input of 657.No \&! Ns Ar command , 658(see 659.Ic \&! Ns Ar command 660below). 661The default filename and current address are unchanged. 662.It (1,$) Ns Ic W Ar file 663Appends the addressed lines to the end of 664.Ar file . 665This is similar to the 666.Ic w 667command, except that the previous contents of file are not clobbered. 668The current address is unchanged. 669.It (.+1) Ns Ic z Ns Ar n 670Scrolls 671.Ar n 672lines at a time starting at addressed line. 673If 674.Ar n 675is not specified, then the current window size is used. 676The current address is set to the last line printed. 677.It ($) Ns Ic = 678Prints the line number of the addressed line. 679.It (.+1) Ns newline 680Prints the addressed line, and sets the current address to that line. 681.It Ic \&! Ns Ar command 682Executes 683.Ar command 684via 685.Xr sh 1 . 686If the first character of 687.Ar command 688is 689.Sq !\& , 690then it is replaced by text of the previous 691.Ic \&! Ns Ar command . 692.Nm 693does not process 694.Ar command 695for 696.Sq \e 697.Pq backslash 698escapes. 699However, an unescaped 700.Sq % 701is replaced by the default filename. 702When the shell returns from execution, a 703.Sq \&! 704is printed to the standard output. 705The current line is unchanged. 706.El 707.Sh ASYNCHRONOUS EVENTS 708.Bl -tag -width "SIGWINCH" 709.It Dv SIGHUP 710If the current buffer has changed since it was last written, 711.Nm 712attempts to write the buffer to the file 713.Pa ed.hup . 714Nothing is written to the currently remembered file, and 715.Nm 716exits. 717.It Dv SIGINT 718When an interrupt occurs, 719.Nm 720prints 721.Sq ?\en 722and returns to command mode. 723If interrupted during text input, 724the text already input is written to the current buffer, 725as if text input had been normally terminated. 726.It Dv SIGQUIT 727This signal is ignored. 728.It Dv SIGWINCH 729The screen is resized. 730.El 731.Sh FILES 732.Bl -tag -width /tmp/ed.* -compact 733.It Pa /tmp/ed.* 734buffer file 735.It Pa ed.hup 736where 737.Nm 738attempts to write the buffer if the terminal hangs up 739.El 740.Sh EXIT STATUS 741.Ex -std ed 742.Sh DIAGNOSTICS 743When an error occurs, 744.Nm 745prints a 746.Sq \&? 747and either returns to command mode or exits if its input is from a script. 748An explanation of the last error can be printed with the 749.Ic h 750.Pq help 751command. 752.Pp 753Since the 754.Ic g 755.Pq global 756command masks any errors from failed searches and substitutions, 757it can be used to perform conditional operations in scripts; e.g., 758.Pp 759.Dl g/old/s//new/ 760.Pp 761replaces any occurrences of 762.Qq old 763with 764.Qq new . 765.Pp 766If the 767.Ic u 768.Pq undo 769command occurs in a global command list, 770then the command list is executed only once. 771.Pp 772If diagnostics are not disabled, attempting to quit 773.Nm 774or edit another file before writing a modified buffer results in an error. 775If the command is entered a second time, it succeeds, 776but any changes to the buffer are lost. 777.Sh SEE ALSO 778.Xr sed 1 , 779.Xr sh 1 , 780.Xr vi 1 , 781.Xr re_format 7 782.Rs 783.%A B. W. Kernighan 784.%A P. J. Plauger 785.%B Software Tools in Pascal 786.%O Addison-Wesley 787.%D 1981 788.Re 789.Sh STANDARDS 790The 791.Nm 792utility is compliant with the 793.St -p1003.1-2008 794specification. 795.Pp 796The commands 797.Cm W 798and 799.Cm z 800and the address specifier 801.Sq % 802are extensions to that specification. 803.Pp 804The 805.St -p1003.1-2008 806specification says the 807.Sq ^ 808address specifier is neither required nor prohibited; 809additionally, it says behaviour for the 810.Fl 811option is 812.Dq unspecified . 813.Sh HISTORY 814An 815.Nm 816command appeared in 817.At v1 . 818.Sh CAVEATS 819.Nm 820processes 821.Ar file 822arguments for backslash escapes, i.e., in a filename, 823any characters preceded by a backslash 824.Pq Ql \e 825are interpreted literally. 826.Pp 827If a text 828.Pq non-binary 829file is not terminated by a newline character, 830then 831.Nm 832appends one on reading/writing it. 833In the case of a binary file, 834.Nm 835does not append a newline on reading/writing. 836