BSD 4_3_Reno development
[unix-history] / usr / share / man / cat1 / ed.0
CommitLineData
0e9d8a17
C
1ED(1) UNIX Reference Manual ED(1)
2
3N\bNA\bAM\bME\bE
4 e\bed\bd - text editor
5
6S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS
7 e\bed\bd [-\b-] [-\b-x\bx] [_\bf_\bi_\bl_\be]
8
9D\bDE\bES\bSC\bCR\bRI\bIP\bPT\bTI\bIO\bON\bN
10 E\bEd\bd is the standard text editor.
11
12 If a _\bf_\bi_\bl_\be argument is given, e\bed\bd simulates an e\be command (see below) on the
13 named file; that is to say, the file is read into e\bed\bd '\b's\bs buffer so that it
14 can be edited.
15
16 -\b-x\bx An x\bx command is simulated first to handle an encrypted file.
17
18 -\b- Suppresses the printing of explanatory output and should be used
19 when the standard input is an editor script.
20
21 E\bEd\bd operates on a copy of any file it is editing; changes made in the copy
22 have no effect on the file until a w\bw (write) command is given. The copy
23 of the text being edited resides in a temporary file called the _\bb_\bu_\bf_\bf_\be_\br.
24
25 Commands to e\bed\bd have a simple and regular structure: zero or more
26 _\ba_\bd_\bd_\br_\be_\bs_\bs_\be_\bs followed by a single character _\bc_\bo_\bm_\bm_\ba_\bn_\bd, possibly followed by
27 parameters to the command. These addresses specify one or more lines in
28 the buffer. Missing addresses are supplied by default.
29
30 In general, only one command may appear on a line. Certain commands al-
31 low the addition of text to the buffer. While e\bed\bd is accepting text, it
32 is said to be in _\bi_\bn_\bp_\bu_\bt _\bm_\bo_\bd_\be. In this mode, no commands are recognized;
33 all input is merely collected. Input mode is left by typing a period `.\b.'
34 alone at the beginning of a line.
35
36 E\bEd\bd supports a limited form of _\br_\be_\bg_\bu_\bl_\ba_\br _\be_\bx_\bp_\br_\be_\bs_\bs_\bi_\bo_\bn notation. A regular ex-
37 pression specifies a set of strings of characters. A member of this set
38 of strings is said to be _\bm_\ba_\bt_\bc_\bh_\be_\bd by the regular expression. In the fol-
39 lowing specification for regular expressions the word `character' means
40 any character but newline.
41
42 1. Any character except a special character matches itself. Special
43 characters are the regular expression delimiter plus `\\b\[\b[.\b.' and
44 sometimes `^\b^*\b*$\b$'.
45
46 2. A `.\b.' matches any character.
47
48 3. A `\' followed by any character except a digit or () matches that
49 character.
50
51 4. A nonempty string [_\bs] or ([^\b^_\bs]) matches any character in (or not
52 in) _\bs. In _\bs, `\' has no special meaning, and may only appear as
53 the first letter. A substring _\ba-_\bb, with _\ba and _\bb in ascending ASCII
54 order, stands for the inclusive range of ASCII characters.
55
56 5. A regular expression of form 1-4 followed by * matches a sequence
57 of 0 or more matches of the regular expression.
58
59 6. A regular expression, _\bx, of form 1-8, bracketed \(_\bx\) matches what
60 _\bx matches.
61
62 7. A \ followed by a digit _\bn matches a copy of the string that the
63 bracketed regular expression beginning with the _\bn'th \( matched.
64
65 8. A regular expression of form 1-8, _\bx, followed by a regular expres-
66 sion of form 1-7, _\by matches a match for _\bx followed by a match for
67 _\by, with the _\bx match being as long as possible while still permit-
68 ting a _\by match.
69
70 9. A regular expression of form 1-8 preceded by `^' (or followed by
71 `$'), is constrained to matches that begin at the left (or end at
72 the right) end of a line.
73
74 10. A regular expression of form 1-9 picks out the longest among the
75 leftmost matches in a line.
76
77 11. An empty regular expression stands for a copy of the last regular
78 expression encountered.
79
80 Regular expressions are used in addresses to specify lines and in one
81 command (see _\bs below) to specify a portion of a line which is to be re-
82 placed. If it is desired to use one of the regular expression metachar-
83 acters as an ordinary character, that character may be preceded by `\'.
84 This also applies to the character bounding the regular expression (often
85 `/') and to `\' itself.
86
87 To understand addressing in e\bed\bd it is necessary to know that at any time
88 there is a _\bc_\bu_\br_\br_\be_\bn_\bt _\bl_\bi_\bn_\be. Generally speaking, the current line is the
89 last line affected by a command; however, the exact effect on the current
90 line is discussed under the description of the command. Addresses are
91 constructed as follows.
92
93 1. The character `.\b.' addresses the current line.
94
95 2. The character `$\b$' addresses the last line of the buffer.
96
97 3. A decimal number _\bn addresses the _\bn'th line of the buffer.
98
99 4. `'_\bx' addresses the line marked with the name _\bx, which must be a
100 lower-case letter. Lines are marked with the _\bk command described
101 below.
102
103 5. A regular expression enclosed in slashes `/' addresses the line
104 found by searching forward from the current line and stopping at
105 the first line containing a string that matches the regular
106 expression. If necessary the search wraps around to the beginning
107 of the buffer.
108
109 6. A regular expression enclosed in queries `?' addresses the line
110 found by searching backward from the current line and stopping at
111 the first line containing a string that matches the regular
112 expression. If necessary the search wraps around to the end of the
113 buffer.
114
115 7. An address followed by a plus sign `+' or a minus sign `-' followed
116 by a decimal number specifies that address plus (resp. minus) the
117 indicated number of lines. The plus sign may be omitted.
118
119 8. If an address begins with `+' or `-' the addition or subtraction is
120 taken with respect to the current line; e.g. `-5' is understood to
121 mean `.-5' .
122
123 9. If an address ends with `+' `-', then 1 is added (resp.
124 subtracted). As a consequence of this rule and rule 8, the address
125 `-' refers to the line before the current line. Moreover, trailing
126 `+' and `-' characters have cumulative effect, so `--' refers to
127 the current line less 2.
128
129 10. To maintain compatibility with earlier versions of the editor, the
130 character `^' in addresses is equivalent to `-'.
131
132 Commands may require zero, one, or two addresses. Commands which require
133 no addresses regard the presence of an address as an error. Commands
134 which accept one or two addresses assume default addresses when
135 insufficient are given. If more addresses are given than such a command
136 requires, the last one or two (depending on what is accepted) are used.
137
138 Addresses are separated from each other typically by a comma `,' They may
139 also be separated by a semicolon `;' In this case the current line `.' .
140 is set to the previous address before the next address is interpreted.
141 This feature can be used to determine the starting line for forward and
142 backward searches (`/' `?'). The second address of any two-address
143 sequence must correspond to a line following the line corresponding to
144 the first address. The special form `%' is an abbreviation for the
145 address pair `1,$'.
146
147 In the following list of e\bed\bd commands, the default addresses are shown in
148 parentheses. The parentheses are not part of the address, but are used
149 to show that the given addresses are the default.
150
151 As mentioned, it is generally illegal for more than one command to appear
152 on a line. However, most commands may be suffixed by `p' or by `l', in
153 which case the current line is either printed or listed respectively in
154 the way discussed below. Commands may also be suffixed by `n', meaning
155 the output of the command is to be line numbered. These suffixes may be
156 combined in any order.
157
158 (.)a\ba
159
160 <text>
161
162 .\b. The append command reads the given text and appends it after the
163 addressed line. `.\b.' is left on the last line input, if there were
164 any, otherwise at the addressed line. Address `0' is legal for
165 this command; text is placed at the beginning of the buffer.
166
167 (.\b.,.\b.)c\bc
168
169 <text>
170
171 .\b. The change command deletes the addressed lines, then accepts input
172 text which replaces these lines. `.\b.' is left at the last line in-
173 put; if there were none, it is left at the line preceding the
174 deleted lines.
175
176 (.\b.,.\b.)d\bd
177 The delete command deletes the addressed lines from the buffer.
178 The line originally after the last line deleted becomes the current
179 line; if the lines deleted were originally at the end, the new last
180 line becomes the current line.
181
182 e\be _\bf_\bi_\bl_\be_\bn_\ba_\bm_\be
183 The edit command causes the entire contents of the buffer to be
184 deleted, and then the named file to be read in. `.\b.' is set to the
185 last line of the buffer. The number of characters read is typed.
186 _\bf_\bi_\bl_\be_\bn_\ba_\bm_\be is remembered for possible use as a default file name in a
187 subsequent r\br or w\bw command. If _\bf_\bi_\bl_\be_\bn_\ba_\bm_\be is missing, the remembered
188 name is used.
189
190 E\bE _\bf_\bi_\bl_\be_\bn_\ba_\bm_\be
191 This command is the same as e\be, except that no diagnostic results
192 when no w\bw has been given since the last buffer alteration.
193
194 f\bf _\bf_\bi_\bl_\be_\bn_\ba_\bm_\be
195 The filename command prints the currently remembered file name. If
196 _\bf_\bi_\bl_\be_\bn_\ba_\bm_\be is given, the currently remembered file name is changed to
197 _\bf_\bi_\bl_\be_\bn_\ba_\bm_\be.
198
199 (1,$\b$)g\bg/_\br_\be_\bg_\bu_\bl_\ba_\br _\be_\bx_\bp_\br_\be_\bs_\bs_\bi_\bo_\bn/_\bc_\bo_\bm_\bm_\ba_\bn_\bd _\bl_\bi_\bs_\bt
200 In the global command, the first step is to mark every line which
201 matches the given regular expression. Then for every such line,
202 the given command list is executed with `.\b.' initially set to that
203 line. A single command or the first of multiple commands appears
204 on the same line with the global command. All lines of a multi-
205 line list except the last line must be ended with `\\b\'. A\bA, i\bi,\b, and c\bc
206 commands and associated input are permitted; the `.\b.' terminating
207 input mode may be omitted if it would be on the last line of the
208 command list. The commands g\bg and v\bv are not permitted in the com-
209 mand list.
210
211 (.)i\bi
212
213 <text>
214
215 .\b. This command inserts the given text before the addressed line. `.\b.'
216 is left at the last line input, or, if there were none, at the line
217 before the addressed line. This command differs from the a\ba command
218 only in the placement of the text.
219
220 (.\b.,.\b.+\b+1\b1)j\bj
221 This command joins the addressed lines into a single line;
222 intermediate newlines simply disappear. `.\b.' is left at the
223 resulting line.
224
225 (.)k\bk_\bx
226 The mark command marks the addressed line with name _\bx, which must
227 be a lower-case letter. The address form `'_\bx' then addresses this
228 line.
229
230 (.\b.,.\b.)l\bl
231 The list command prints the addressed lines in an unambiguous way:
232 non-graphic characters are printed in two-digit octal, and long
233 lines are folded. The _\bl command may be placed on the same line
234 after any non-i/o command.
235
236 (.\b.,.\b.)m\bm_\ba
237 The move command repositions the addressed lines after the line
238 addressed by _\ba. The last of the moved lines becomes the current
239 line.
240
241 (.\b.,.\b.)p\bp
242 The print command prints the addressed lines. `.\b.' is left at the
243 last line printed. The p\bp command may be placed on the same line
244 after any non-i/o command.
245
246 (.\b.,.\b.)P\bP
247 This command is a synonym for p\bp.
248
249 q\bq The quit command causes e\bed\bd to exit. No automatic write of a file
250 is done.
251
252 Q\bQ This command is the same as q\bq, except that no diagnostic results
253 when no w\bw has been given since the last buffer alteration.
254
255 ($\b$)r\br _\bf_\bi_\bl_\be_\bn_\ba_\bm_\be
256 The read command reads in the given file after the addressed line.
257 If no file name is given, the remembered file name, if any, is used
258 (see e\be and f\bf commands). The file name is remembered if there was
259 no remembered file name already. Address `0' is legal for r\br and
260 causes the file to be read at the beginning of the buffer. If the
261 read is successful, the number of characters read is typed. `.\b.' is
262 left at the last line read in from the file.
263
264 (.\b.,.\b.)s\bs/_\br_\be_\bg_\bu_\bl_\ba_\br _\be_\bx_\bp_\br_\be_\bs_\bs_\bi_\bo_\bn/_\br_\be_\bp_\bl_\ba_\bc_\be_\bm_\be_\bn_\bt/ or
265
266 (.\b.,.\b.)s\bs/_\br_\be_\bg_\bu_\bl_\ba_\br _\be_\bx_\bp_\br_\be_\bs_\bs_\bi_\bo_\bn/_\br_\be_\bp_\bl_\ba_\bc_\be_\bm_\be_\bn_\bt/g\bg
267 The substitute command searches each addressed line for an
268 occurrence of the specified regular expression. On each line in
269 which a match is found, all matched strings are replaced by the
270 replacement specified, if the global replacement indicator g\bg ap-
271 pears after the command. If the global indicator does not appear,
272 only the first occurrence of the matched string is replaced. It is
273 an error for the substitution to fail on all addressed lines. Any
274 punctuation character may be used instead of `/\b/' to delimit the
275 regular expression and the replacement. `.\b.' is left at the last
276 line substituted. An ampersand `&\b&' appearing in the replacement is
277 replaced by the string matching the regular expression. The
278 special meaning of `&\b&' in this context may be suppressed by
279 preceding it by `\\b\'. The characters `\\b\_\bn' where _\bn is a digit, are
280 replaced by the text matched by the _\bn'th regular subexpression
281 enclosed between `\\b\(\b('. and `\\b\)\b)'. When nested, parenthesized
282 subexpressions are present, _\bn is determined by counting occurrences
283 of `\\b\(\b('. starting from the left. Lines may be split by
284 substituting new-line characters into them. The new-line in the
285 replacement string must be escaped by preceding it by `\\b\'. One or
286 two trailing delimiters may be omitted, implying the p\bp suffix. The
287 special form s\bs followed by _\bn_\bo delimiters repeats the most recent
288 substitute command on the addressed lines. The s\bs may be followed
289 by the letters r\br (use the most recent regular expression for the
290 left hand side, instead of the most recent left hand side of a sub-
291 stitute command), p\bp (complement the setting of the p\bp suffix from
292 the previous substitution), or g\bg (complement the setting of the g\bg
293 suffix). These letters may be combined in any order.
294
295 (.\b.,.\b.)t\bt_\ba
296 This command acts just like the m\bm command, except that a copy of
297 the addressed lines is placed after address _\ba (which may be 0).
298 `.\b.' is left on the last line of the copy.
299
300 (.\b.,.\b.)u\bu
301 The undo command restores the buffer to it's state before the most
302 recent buffer modifying command. The current line is also
303 restored. Buffer modifying commands are a\ba, c\bc, d\bd, g\bg, i\bi and v\bv. For
304 purposes of undo, g\bg and v\bv are considered to be a single buffer
305 modifying command. Undo is its own inverse. When e\bed\bd runs out of
306 memory (at about 8000 lines on any 16 bit mini-computer such as the
307 PDP-11) This full undo is not possible, and u\bu can only undo the ef-
308 fect of the most recent substitute on the current line. This res-
309 tricted undo also applies to editor scripts when e\bed\bd is invoked with
310 the -\b- option.
311
312 (1,$\b$)v\bv/_\br_\be_\bg_\bu_\bl_\ba_\br _\be_\bx_\bp_\br_\be_\bs_\bs_\bi_\bo_\bn/_\bc_\bo_\bm_\bm_\ba_\bn_\bd _\bl_\bi_\bs_\bt
313 This command is the same as the global command g\bg except that the
314 command list is executed g\bg with `.\b.' initially set to every line
315 _\be_\bx_\bc_\be_\bp_\bt those matching the regular expression. (1, $) 192.Tp Cx ( 1
316 ,$\b$)w\bw _\bf_\bi_\bl_\be_\bn_\ba_\bm_\be The write command writes the addressed lines onto the
317 given file. If the file does not exist, it is created. The file
318 name is remembered if there was no remembered file name already.
319 If no file name is given, the remembered file name, if any, is used
320 (see e\be and f\bf commands). `.\b.' is unchanged. If the command is
321 successful, the number of characters written is printed.
322
323 (1,$\b$)W\bW _\bf_\bi_\bl_\be_\bn_\ba_\bm_\be
324 This command is the same as w\bw, except that the addressed lines are
325 appended to the file.
326
327 (1,$\b$)w\bwq\bq _\bf_\bi_\bl_\be_\bn_\ba_\bm_\be
328 This command is the same as w\bw except that afterwards a q\bq command is
329 done, exiting the editor after the file is written.
330
331 x A key string is demanded from the standard input. Later r\br, e\be and w\bw
332 commands will encrypt and decrypt the text with this key by the al-
333 gorithm of crypt(1). An explicitly empty key turns off encryption.
334 1 +\b+1\b1 )z\bz or,
335
336 (1+\b+1\b1)z\bz_\bn
337 This command scrolls through the buffer starting at the addressed
338 line. 22 (or _\bn, if given) lines are printed. The last line print-
339 ed becomes the current line. The value _\bn is sticky, in that it be-
340 comes the default for future z\bz commands.
341
342 ($\b$)=\b=
343 The line number of the addressed line is typed. `.\b.' is unchanged
344 by this command.
345
346 !\b!<shell command>
347 The remainder of the line after the `!' is sent to sh(1) to be
348 interpreted as a command. `.\b.' is unchanged.
349
350 (.\b.+\b+1\b1,.\b.+\b+1\b1)<newline>
351 An address alone on a line causes the addressed line to be printed.
352 A blank line alone is equivalent to .\b.+\b+1\b1 it is useful for stepping
353 through text. If two addresses are present with no intervening
354 semicolon, e\bed\bd prints the range of lines. If they are separated by
355 a semicolon, the second line is printed.
356
357 If an interrupt signal (ASCII DEL) is sent, e\bed\bd prints `?interrupted' and
358 returns to its command level.
359
360 Some size limitations: 512 characters per line, 256 characters per global
361 command list, 64 characters per file name, and, on mini computers, 128K
362 characters in the temporary file. The limit on the number of lines
363 depends on the amount of core: each line takes 2 words.
364
365 When reading a file, e\bed\bd discards ASCII NUL characters and all characters
366 after the last newline. It refuses to read files containing non-ASCII
367 characters.
368
369F\bFI\bIL\bLE\bES\bS
370 /_\bt_\bm_\bp/_\be*
371 _\be_\bd_\bh_\bu_\bp work is saved here if terminal hangs up
372
373
374S\bSE\bEE\bE A\bAL\bLS\bSO\bO
375 ex(1), sed(1), crypt(1)
376 B. W. Kernighan, _\bA _\bT_\bu_\bt_\bo_\br_\bi_\ba_\bl _\bI_\bn_\bt_\br_\bo_\bd_\bu_\bc_\bt_\bi_\bo_\bn _\bt_\bo _\bt_\bh_\be _\bE_\bD _\bT_\be_\bx_\bt _\bE_\bd_\bi_\bt_\bo_\br
377 B. W. Kernighan, _\bA_\br _\bA_\bd_\bv_\ba_\bn_\bc_\be_\bd _\be_\bd_\bi_\bt_\bi_\bn_\bg _\bo_\bn _\bU_\bN_\bI_\bX
378
379H\bHI\bIS\bST\bTO\bOR\bRY\bY
380 The e\bed\bd command appeared in Version 6 AT&T UNIX.
381
382D\bDI\bIA\bAG\bGN\bNO\bOS\bST\bTI\bIC\bCS\bS
383 `name' for inaccessible file; `?self-explanatory message' for other er-
384 rors.
385
386 To protect against throwing away valuable work, a q\bq or e\be command is con-
387 sidered to be in error, unless a w\bw has occurred since the last buffer
388 change. A second q\bq or e\be will be obeyed regardless.
389
390B\bBU\bUG\bGS\bS
391 The l\bl command mishandles DEL.
392 The u\bun\bnd\bdo\bo command causes marks to be lost on affected lines.
393 The x\bx command, -\b-x\bx option, and special treatment of hangups only work on
394 UNIX.