ed.1 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814
  1. .ad l
  2. .TH ED 1
  3. .CT 1 editor
  4. .ds . (\|\f5.\fP\|)
  5. .ds .. (\|\f5.,.\fP\|)
  6. .ds 1$ (\|\f51,$\fP\|)
  7. .SH NAME
  8. ed, e \- text editor
  9. .SH SYNOPSIS
  10. .B ed
  11. [
  12. .B -
  13. ]
  14. [
  15. .B -o
  16. ]
  17. [
  18. .I file
  19. ]
  20. .SH DESCRIPTION
  21. .I Ed
  22. is the standard text editor;
  23. .I e
  24. is another name for it.
  25. .PP
  26. If a
  27. .I file
  28. argument is given,
  29. .I ed
  30. simulates an
  31. .B e
  32. command (see below) on that file:
  33. it is read into
  34. .I ed's
  35. buffer so that it can be edited.
  36. The options are
  37. .TP
  38. .B -
  39. Suppress the printing
  40. of character counts by
  41. .BR e ,
  42. .BR r ,
  43. and
  44. .B w
  45. commands and of the confirming
  46. .B !
  47. by
  48. .B !
  49. commands.
  50. .TP
  51. .B -o
  52. (for output piping)
  53. Place on the standard error file all output except writing by
  54. .B w
  55. commands.
  56. If no
  57. .I file
  58. is given, make
  59. .I /dev/stdout
  60. the remembered file; see the
  61. .B e
  62. command below.
  63. .I Ed
  64. starts in append mode (simulating an
  65. .B a
  66. command).
  67. .PP
  68. .I Ed
  69. operates on a `buffer', a copy of the file it is editing;
  70. changes made
  71. in the buffer have no effect on the file until a
  72. .B w
  73. (write)
  74. command is given.
  75. The copy of the text being edited resides
  76. in a temporary file called the
  77. .IR buffer .
  78. .PP
  79. Commands to
  80. .I ed
  81. have a simple and regular structure: zero or
  82. more
  83. .I addresses
  84. followed by a single character
  85. .I command,
  86. possibly
  87. followed by parameters to the command.
  88. These addresses specify one or more lines in the buffer.
  89. Missing addresses are supplied by default.
  90. .PP
  91. In general, only one command may appear on a line.
  92. Certain commands allow the
  93. addition of text to the buffer.
  94. While
  95. .I ed
  96. is accepting text, it is said
  97. to be in
  98. .I "input mode."
  99. In this mode, no commands are recognized;
  100. all input is merely collected.
  101. Input mode is left by typing a period
  102. .B .
  103. alone at the
  104. beginning of a line.
  105. .PP
  106. .I Ed
  107. supports a limited form of
  108. .I "regular expression"
  109. notation.
  110. A regular expression specifies
  111. a set of strings of characters.
  112. A member of this set of strings is said to be
  113. .I matched
  114. by the regular expression.
  115. In the following specification for regular expressions
  116. the word `character' means any character but newline.
  117. .IP 1.
  118. Any character except a special character
  119. matches itself.
  120. Special characters are
  121. the regular expression delimiter plus
  122. .B \e[.
  123. and sometimes
  124. .BR ^*$ .
  125. .IP 2.
  126. A
  127. .B .
  128. matches any character.
  129. .IP 3.
  130. A
  131. .B \e
  132. followed by any character except a digit,
  133. .BR ( ,
  134. or
  135. .B )
  136. matches that character.
  137. .IP 4.
  138. A nonempty string
  139. .I s
  140. bracketed
  141. .BI [ \|s\| ]
  142. (or
  143. .BI [^ s\| ])
  144. matches any character in (or not in)
  145. .I s.
  146. In
  147. .I s,
  148. .B \e
  149. has no special meaning, and
  150. .B ]
  151. may only appear as
  152. the first letter.
  153. A substring
  154. .IB a - b ,
  155. with
  156. .I a
  157. and
  158. .I b
  159. in ascending
  160. .SM Unicode
  161. order, stands for the inclusive
  162. range of
  163. .SM Unicode
  164. characters.
  165. .IP 5.
  166. A regular expression of form 1-4 followed by
  167. .B *
  168. matches a sequence of
  169. 0 or more matches of the regular expression.
  170. .IP 6.
  171. A regular expression,
  172. .I x,
  173. of form 1-8, bracketed
  174. .BI \e( \|x\| \e)
  175. matches what
  176. .I x
  177. matches.
  178. .IP 7.
  179. A
  180. .B \e
  181. followed by a digit
  182. .I n
  183. matches a copy of the string that the
  184. bracketed regular expression beginning with the
  185. .IR n th
  186. .B \e(
  187. matched.
  188. .IP 8.
  189. A regular expression of form 1-8,
  190. .I x,
  191. followed by a regular expression of form 1-7,
  192. .I y
  193. matches a match for
  194. .I x
  195. followed by a match for
  196. .I y,
  197. with the
  198. .I x
  199. match being as long as possible while still permitting a
  200. .I y
  201. match.
  202. .IP 9.
  203. A regular expression of form 1-8, or a null string, preceded by
  204. .B ^
  205. (and/or followed by
  206. .BR $ ),
  207. is constrained to matches that
  208. begin at the left (and/or end at the right) end of a line.
  209. .IP 10.
  210. A regular expression of form 1-9 picks out the
  211. longest among the leftmost matches in a line.
  212. .IP 11.
  213. An empty regular expression stands for a copy of the
  214. last regular expression encountered.
  215. .PP
  216. Regular expressions are used in addresses to specify
  217. lines and in one command
  218. (see
  219. .I s
  220. below)
  221. to specify a portion of a line which is to be replaced.
  222. If it is desired to use one of
  223. the regular expression metacharacters as an ordinary
  224. character, that character may be preceded by `\e'.
  225. This also applies to the character bounding the regular
  226. expression (often
  227. .BR / )
  228. and to
  229. .B \e
  230. itself.
  231. .PP
  232. To understand addressing in
  233. .I ed
  234. it is necessary to know that at any time there is a
  235. .I "current line."
  236. Generally speaking, the current line is
  237. the last line affected by a command; however,
  238. the exact effect on the current line
  239. is discussed under the description of
  240. the command.
  241. Addresses are constructed as follows.
  242. .TP
  243. 1.
  244. The character
  245. .BR . ,
  246. customarily called `dot',
  247. addresses the current line.
  248. .TP
  249. 2.
  250. The character
  251. .B $
  252. addresses the last line of the buffer.
  253. .TP
  254. 3.
  255. A decimal number
  256. .I n
  257. addresses the
  258. .IR n -th
  259. line of the buffer.
  260. .TP
  261. 4.
  262. .BI \'x
  263. addresses the line marked with the name
  264. .IR x ,
  265. which must be a lower-case letter.
  266. Lines are marked with the
  267. .B k
  268. command described below.
  269. .TP
  270. 5.
  271. A regular expression enclosed in slashes
  272. .B /
  273. addresses
  274. the line found by searching forward from the current line
  275. and stopping at the first line containing a
  276. string that matches the regular expression.
  277. If necessary the search wraps around to the beginning of the
  278. buffer.
  279. .TP
  280. 6.
  281. A regular expression enclosed in queries
  282. .B ?
  283. addresses
  284. the line found by searching backward from the current line
  285. and stopping at the first line containing
  286. a string that matches the regular expression.
  287. If necessary
  288. the search wraps around to the end of the buffer.
  289. .TP
  290. 7.
  291. An address followed by a plus sign
  292. .B +
  293. or a minus sign
  294. .B -
  295. followed by a decimal number specifies that address plus
  296. (resp. minus) the indicated number of lines.
  297. The plus sign may be omitted.
  298. .TP
  299. 8.
  300. An address followed by
  301. .B +
  302. (or
  303. .BR - )
  304. followed by a
  305. regular expression enclosed in slashes specifies the first
  306. matching line following (or preceding) that address.
  307. The search wraps around if necessary.
  308. The
  309. .B +
  310. may be omitted, so
  311. .B 0/x/
  312. addresses the
  313. .I first
  314. line in the buffer with an
  315. .BR x .
  316. Enclosing the regular expression in
  317. .B ?
  318. reverses the search direction.
  319. .TP
  320. 9.
  321. If an address begins with
  322. .B +
  323. or
  324. .B -
  325. the addition or subtraction is taken with respect to the current line;
  326. e.g.\&
  327. .B -5
  328. is understood to mean
  329. .BR .-5 .
  330. .TP
  331. 10.
  332. If an address ends with
  333. .B +
  334. or
  335. .BR - ,
  336. then 1 is added (resp. subtracted).
  337. As a consequence of this rule and rule 9,
  338. the address
  339. .B -
  340. refers to the line before the current line.
  341. Moreover,
  342. trailing
  343. .B +
  344. and
  345. .B -
  346. characters
  347. have cumulative effect, so
  348. .B --
  349. refers to the current
  350. line less 2.
  351. .TP
  352. 11.
  353. To maintain compatibility with earlier versions of the editor,
  354. the character
  355. .B ^
  356. in addresses is
  357. equivalent to
  358. .BR - .
  359. .PP
  360. Commands may require zero, one, or two addresses.
  361. Commands which require no addresses regard the presence
  362. of an address as an error.
  363. Commands which accept one or two addresses
  364. assume default addresses when insufficient are given.
  365. If more addresses are given than such a command requires,
  366. the last one or two (depending on what is accepted) are used.
  367. .PP
  368. Addresses are separated from each other typically by a comma
  369. .BR , .
  370. They may also be separated by a semicolon
  371. .BR ; .
  372. In this case the current line
  373. is set to
  374. the previous address before the next address is interpreted.
  375. If no address precedes a comma or semicolon, line 1 is assumed;
  376. if no address follows, the last line of the buffer is assumed.
  377. The second address of any two-address sequence
  378. must correspond to a line following the line corresponding to the first address.
  379. .PP
  380. In the following list of
  381. .I ed
  382. commands, the default addresses
  383. are shown in parentheses.
  384. The parentheses are not part of
  385. the address, but are used to show that the given addresses are
  386. the default.
  387. `Dot' means the current line.
  388. .TP
  389. .RB \*. \|a
  390. .br
  391. .ns
  392. .TP
  393. <text>
  394. .br
  395. .ns
  396. .TP
  397. .B .
  398. Read the given text
  399. and append it after the addressed line.
  400. Dot is left
  401. on the last line input, if there
  402. were any, otherwise at the addressed line.
  403. Address
  404. .B 0
  405. is legal for this command; text is placed
  406. at the beginning of the buffer.
  407. .TP
  408. .RB \*(.. \|b [ +- ][\fIpagesize\fP][ pln\fR]
  409. Browse.
  410. Print a `page', normally 20 lines.
  411. The optional
  412. .B +
  413. (default) or
  414. .B -
  415. specifies whether the next or previous
  416. page is to be printed.
  417. The optional
  418. .I pagesize
  419. is the number of lines in a page.
  420. The optional
  421. .BR p ,
  422. .BR n ,
  423. or
  424. .B l
  425. causes printing in the specified format, initially
  426. .BR p .
  427. Pagesize and format are remembered between
  428. .B b
  429. commands.
  430. Dot is left at the last line displayed.
  431. .TP
  432. .RB \*(.. \|c
  433. .br
  434. .ns
  435. .TP
  436. <text>
  437. .br
  438. .ns
  439. .TP
  440. .B .
  441. .br
  442. Change.
  443. Delete the addressed lines, then accept input
  444. text to replace these lines.
  445. Dot is left at the last line input; if there were none,
  446. it is left at the line preceding the deleted lines.
  447. .TP
  448. .RB \*(.. \|d
  449. Delete the addressed lines from the buffer.
  450. Dot is set to the line following the last line deleted, or to
  451. the last line of the buffer if the deleted lines had no successor.
  452. .TP
  453. .BI e " filename"
  454. Edit.
  455. Delete the entire contents of the buffer;
  456. then read the named file into the buffer.
  457. Dot is set to the last line of the buffer.
  458. The number of characters read is typed.
  459. The file name is remembered for possible use in later
  460. .BR e ,
  461. .BR r ,
  462. or
  463. .B w
  464. commands.
  465. If
  466. .I filename
  467. is missing, the remembered name is used.
  468. .TP
  469. .BI E " filename"
  470. Unconditional
  471. .BR e ;
  472. see `DIAGNOSTICS' below.
  473. .TP
  474. .BI f " filename"
  475. Print the currently remembered file name.
  476. If
  477. .I filename
  478. is given,
  479. the currently remembered file name is first changed to
  480. .I filename.
  481. .TP
  482. .RB \*(1$ \|g/\fIregular\ expression\fP/\fIcommand\ list\fP
  483. .PD 0
  484. .TP
  485. .RB \*(1$ \|g/\fIregular\ expression\fP/
  486. .TP
  487. .RB \*(1$ \|g/\fIregular\ expression\fP
  488. .PD
  489. Global.
  490. First mark every line which matches
  491. the given
  492. .I regular expression.
  493. Then for every such line, execute the
  494. .I command list
  495. with dot initially set to that line.
  496. A single command or the first of multiple commands
  497. appears on the same line with the global command.
  498. All lines of a multi-line list except the last line must end with
  499. .BR \e .
  500. The
  501. .RB \&` \&. \&'
  502. terminating input mode for an
  503. .BR a ,
  504. .BR i ,
  505. .B c
  506. command may be omitted if it would be on the
  507. last line of the command list.
  508. The commands
  509. .B g
  510. and
  511. .B v
  512. are not permitted in the command list.
  513. Any character other than space or newline may
  514. be used instead of
  515. .B /
  516. to delimit the regular expression.
  517. The second and third forms mean
  518. .BI g/ regular\ expression /p .
  519. .TP
  520. .RB (\| .\| ) \|i
  521. .PD 0
  522. .TP
  523. <text>
  524. .TP
  525. .B .
  526. Insert the given text before the addressed line.
  527. Dot is left at the last line input, or, if there were none,
  528. at the line before the addressed line.
  529. This command differs from the
  530. .I a
  531. command only in the placement of the
  532. text.
  533. .PD
  534. .TP
  535. .RB (\| .,.+1 \|) \|j
  536. Join the addressed lines into a single line;
  537. intermediate newlines are deleted.
  538. Dot is left at the resulting line.
  539. .TP
  540. .RB \*. \|k\fIx\fP
  541. Mark the addressed line with name
  542. .I x,
  543. which must be a lower-case letter.
  544. The address form
  545. .BI \' x
  546. then addresses this line.
  547. .ne 2.5
  548. .TP
  549. .RB \*(.. \|l
  550. List.
  551. Print the addressed lines in an unambiguous way:
  552. a tab is printed as
  553. .BR \et ,
  554. a backspace as
  555. .BR \eb ,
  556. backslashes as
  557. .BR \e\e ,
  558. and printable ASCII characters are printed as themselves.
  559. All other (Uncicode) characters are printed as a
  560. backslash and three octal digits for each byte of their UTF-8
  561. representation.
  562. Long lines are folded,
  563. with the second and subsequent sub-lines indented one tab stop.
  564. If the last character in the line is a blank,
  565. it is followed by
  566. .BR \en .
  567. An
  568. .B l
  569. may be appended, like
  570. .BR p ,
  571. to any non-I/O command.
  572. .TP
  573. .RB \*(.. \|m\fIa
  574. Move.
  575. Reposition the addressed lines after the line
  576. addressed by
  577. .IR a .
  578. Dot is left at the last moved line.
  579. .TP
  580. .RB \*(.. \|n
  581. Number.
  582. Perform
  583. .BR p ,
  584. prefixing each line with its line number and a tab.
  585. An
  586. .B n
  587. may be appended, like
  588. .BR p ,
  589. to any non-I/O command.
  590. .TP
  591. .RB \*(.. \|p
  592. Print the addressed lines.
  593. Dot is left at the last line printed.
  594. A
  595. .B p
  596. appended to any non-I/O command causes the then current line
  597. to be printed after the command is executed.
  598. .TP
  599. .RB \*(.. \|P
  600. This command is a synonym for
  601. .BR p .
  602. .TP
  603. .B q
  604. Quit the editor.
  605. No automatic write
  606. of a file is done.
  607. .TP
  608. .B Q
  609. Quit unconditionally; see `DIAGNOSTICS' below.
  610. .TP
  611. .RB ( $ )\|r\ \fIfilename\fP
  612. Read in the given file after the addressed line.
  613. If no
  614. .I filename
  615. is given, the remembered file name is used.
  616. The file name is remembered if there were no
  617. remembered file name already.
  618. If the read is successful, the number of characters
  619. read is typed.
  620. Dot is left at the last line read in from the file.
  621. .TP
  622. .RB \*(.. \|s\fIn\fP/\fIregular\ expression\fP/\fIreplacement\fP/
  623. .PD 0
  624. .TP
  625. .RB \*(.. \|s\fIn\fP/\fIregular\ expression\fP/\fIreplacement\fP/g
  626. .TP
  627. .RB \*(.. \|s\fIn\fP/\fIregular\ expression\fP/\fIreplacement\fP
  628. .PD
  629. Substitute.
  630. Search each addressed
  631. line for an occurrence of the specified regular expression.
  632. On each line in which
  633. .I n
  634. matches are found
  635. .RI ( n
  636. defaults to 1 if missing),
  637. the
  638. .IR n th
  639. matched string is replaced by the replacement specified.
  640. If the global replacement indicator
  641. .B g
  642. appears after the command,
  643. all subsequent matches on the line are also replaced.
  644. It is an error for the substitution to fail on all addressed lines.
  645. Any character other than space or newline
  646. may be used instead of
  647. .B /
  648. to delimit the regular expression
  649. and the replacement.
  650. Dot is left at the last line substituted.
  651. The third form means
  652. .BI s n / regular\ expression / replacement\fP/p\fR.
  653. The second
  654. .B /
  655. may be omitted if the replacement is
  656. empty.
  657. .IP
  658. An ampersand
  659. .B &
  660. appearing in the replacement
  661. is replaced by the string matching the regular expression.
  662. The characters
  663. .BI \e n,
  664. where
  665. .I n
  666. is a digit,
  667. are replaced by the text matched by the
  668. .IR n -th
  669. regular subexpression
  670. enclosed between
  671. .B \e(
  672. and
  673. .BR \e) .
  674. When
  675. nested, parenthesized subexpressions
  676. are present,
  677. .I n
  678. is determined by counting occurrences of
  679. .BR \e (
  680. starting from the left.
  681. .IP
  682. A literal
  683. .BR & ,
  684. .BR / ,
  685. .B \e
  686. or newline may be included in a replacement
  687. by prefixing it with
  688. .BR \e .
  689. .TP
  690. .RB \*(.. \|t\|\fIa
  691. Transfer.
  692. Copy the addressed lines
  693. after the line addressed by
  694. .I a.
  695. Dot is left at the last line of the copy.
  696. .TP
  697. .RB \*(.. \|u
  698. Undo.
  699. Restore the preceding contents
  700. of the current line, which must be the last line
  701. in which a substitution was made.
  702. .TP
  703. .RB \*(1$ \|v/\fIregular\ expression\fP/\fIcommand\ list\fP
  704. .PD 0
  705. .TP
  706. .RB \*(1$ \|v/\fIregular\ expression\fP/
  707. .TP
  708. .RB \*(1$ \|v/\fIregular\ expression\fP
  709. .PD
  710. This command is the same as the global command
  711. .B g
  712. except that the command list is executed with
  713. dot initially set to every line
  714. .I except
  715. those
  716. matching the regular expression.
  717. .TP
  718. .RB \*(1$ \|w " \fIfilename\fP"
  719. Write the addressed lines onto
  720. the given file.
  721. If the file does not exist,
  722. it is created with mode 666 (readable and writable by everyone).
  723. If no
  724. .I filename
  725. is given, the remembered file name, if any, is used.
  726. The file name is remembered if there were no
  727. remembered file name already.
  728. Dot is unchanged.
  729. If the write is successful, the number of characters written is
  730. printed.
  731. .TP
  732. .RB \*(1$ \|W " \fIfilename\fP"
  733. Perform
  734. .BR w ,
  735. but append to, instead of overwriting, any existing file contents.
  736. .TP
  737. .RB ( $ ) \|=
  738. Print the line number of the addressed line.
  739. Dot is unchanged.
  740. .TP
  741. .BI ! shell\ command
  742. Send the remainder of the line after the
  743. .B !
  744. to
  745. .IR sh (1)
  746. to be interpreted as a command.
  747. Dot is unchanged.
  748. .TP
  749. .RB (\| .+1 )\|<newline>
  750. An address without a command is taken as a
  751. .B p
  752. command.
  753. A terminal
  754. .B /
  755. may be omitted from the address.
  756. A blank line alone is equivalent to
  757. .BR .+1p ;
  758. it is useful
  759. for stepping through text.
  760. .PP
  761. If an interrupt signal
  762. .SM (ASCII DEL)
  763. is sent,
  764. .I ed
  765. prints a
  766. .B ?
  767. and returns to its command level.
  768. .PP
  769. When reading a file,
  770. .I ed
  771. discards
  772. .SM ASCII NUL
  773. characters. If there are any characters
  774. after the last newline,
  775. .I ed
  776. supplies a final newline and prints the
  777. message
  778. .I ?N appended
  779. on the terminal.
  780. It refuses to read files not conforming to the
  781. .SM UTF-8
  782. standard.
  783. .SH FILES
  784. .B /tmp/e*
  785. .br
  786. .B ed.hup
  787. \ \ work is saved here if terminal hangs up
  788. .SH "SEE ALSO"
  789. .IR qed (1),
  790. .IR sed (1),
  791. .IR vi (1)
  792. .SH DIAGNOSTICS
  793. .BI ? name
  794. for inaccessible file;
  795. .B ?TMP
  796. for temporary file overflow;
  797. .B ?N appended
  798. when a final newline has been appended;
  799. .B ?
  800. for errors in commands or other overflows.
  801. .PP
  802. To protect against throwing away valuable work,
  803. a
  804. .B q
  805. or
  806. .B e
  807. command is considered to be in error if the buffer has
  808. been modified since the last
  809. .BR w ,
  810. .BR q ,
  811. or
  812. .B e
  813. command.