1ED(1) General Commands Manual ED(1)
2
3
4
6 ed - text editor
7
9 ed [ - ] [ -x ] [ name ]
10
12 Ed is the standard text editor.
13
14 If a name argument is given, ed simulates an e command (see below) on
15 the named file; that is to say, the file is read into ed's buffer so
16 that it can be edited. If -x is present, an x command is simulated
17 first to handle an encrypted file. The optional - suppresses the
18 printing of character counts by e, r, and w commands.
19
20 Ed operates on a copy of any file it is editing; changes made in the
21 copy have no effect on the file until a w (write) command is given.
22 The copy of the text being edited resides in a temporary file called
23 the buffer.
24
25 Commands to ed have a simple and regular structure: zero or more
26 addresses followed by a single character command, possibly followed by
27 parameters to the command. These addresses specify one or more lines
28 in the buffer. Missing addresses are supplied by default.
29
30 In general, only one command may appear on a line. Certain commands
31 allow the addition of text to the buffer. While ed is accepting text,
32 it is said to be in input mode. In this mode, no commands are recog‐
33 nized; all input is merely collected. Input mode is left by typing a
34 period `.' alone at the beginning of a line.
35
36 Ed supports a limited form of regular expression notation. A regular
37 expression specifies a set of strings of characters. A member of this
38 set of strings is said to be matched by the regular expression. In the
39 following specification for regular expressions the word `character'
40 means any character but newline.
41
42 1. Any character except a special character matches itself. Spe‐
43 cial characters are the regular expression delimiter plus \[.
44 and sometimes ^*$.
45
46 2. A . 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 s bracketed [s] (or [^s]) matches any charac‐
52 ter in (or not in) s. In s, \ has no special meaning, and ] may
53 only appear as the first letter. A substring a-b, with a and b
54 in ascending ASCII order, stands for the inclusive range of
55 ASCII characters.
56
57 5. A regular expression of form 1-4 followed by * matches a
58 sequence of 0 or more matches of the regular expression.
59
60 6. A regular expression, x, of form 1-8, bracketed \(x\) matches
61 what x matches.
62
63 7. A \ followed by a digit n matches a copy of the string that the
64 bracketed regular expression beginning with the nth \( matched.
65
66 8. A regular expression of form 1-8, x, followed by a regular
67 expression of form 1-7, y matches a match for x followed by a
68 match for y, with the x match being as long as possible while
69 still permitting a y match.
70
71 9. A regular expression of form 1-8 preceded by ^ (or followed by
72 $), is constrained to matches that begin at the left (or end at
73 the right) end of a line.
74
75 10. A regular expression of form 1-9 picks out the longest among the
76 leftmost matches in a line.
77
78 11. An empty regular expression stands for a copy of the last regu‐
79 lar expression encountered.
80
81 Regular expressions are used in addresses to specify lines and in one
82 command (see s below) to specify a portion of a line which is to be
83 replaced. If it is desired to use one of the regular expression
84 metacharacters as an ordinary character, that character may be preceded
85 by `\'. This also applies to the character bounding the regular
86 expression (often `/') and to `\' itself.
87
88 To understand addressing in ed it is necessary to know that at any time
89 there is a current line. Generally speaking, the current line is the
90 last line affected by a command; however, the exact effect on the cur‐
91 rent line is discussed under the description of the command. Addresses
92 are constructed as follows.
93
94 1. The character `.' addresses the current line.
95
96 2. The character `$' addresses the last line of the buffer.
97
98 3. A decimal number n addresses the n-th line of the buffer.
99
100 4. `′x' addresses the line marked with the name x, which must be a
101 lower-case letter. Lines are marked with the k command
102 described below.
103
104 5. A regular expression enclosed in slashes `/' addresses the line
105 found by searching forward from the current line and stopping at
106 the first line containing a string that matches the regular
107 expression. If necessary the search wraps around to the begin‐
108 ning of the buffer.
109
110 6. A regular expression enclosed in queries `?' addresses the line
111 found by searching backward from the current line and stopping
112 at the first line containing a string that matches the regular
113 expression. If necessary the search wraps around to the end of
114 the buffer.
115
116 7. An address followed by a plus sign `+' or a minus sign `-' fol‐
117 lowed by a decimal number specifies that address plus (resp.
118 minus) the indicated number of lines. The plus sign may be
119 omitted.
120
121 8. If an address begins with `+' or `-' the addition or subtraction
122 is taken with respect to the current line; e.g. `-5' is under‐
123 stood to mean `.-5'.
124
125 9. If an address ends with `+' or `-', then 1 is added (resp. sub‐
126 tracted). As a consequence of this rule and rule 8, the address
127 `-' refers to the line before the current line. Moreover,
128 trailing `+' and `-' characters have cumulative effect, so `--'
129 refers to the current line less 2.
130
131 10. To maintain compatibility with earlier versions of the editor,
132 the character `^' in addresses is equivalent to `-'.
133
134 Commands may require zero, one, or two addresses. Commands which
135 require no addresses regard the presence of an address as an error.
136 Commands which accept one or two addresses assume default addresses
137 when insufficient are given. If more addresses are given than such a
138 command requires, the last one or two (depending on what is accepted)
139 are used.
140
141 Addresses are separated from each other typically by a comma `,'. They
142 may also be separated by a semicolon `;'. In this case the current
143 line `.' is set to the previous address before the next address is
144 interpreted. This feature can be used to determine the starting line
145 for forward and backward searches (`/', `?'). The second address of
146 any two-address sequence must correspond to a line following the line
147 corresponding to the first address.
148
149 In the following list of ed commands, the default addresses are shown
150 in parentheses. The parentheses are not part of the address, but are
151 used to show that the given addresses are the default.
152
153 As mentioned, it is generally illegal for more than one command to
154 appear on a line. However, most commands may be suffixed by `p' or by
155 `l', in which case the current line is either printed or listed respec‐
156 tively in the way discussed below.
157
158 (.)a
159 <text>
160 .
161 The append command reads the given text and appends it after the
162 addressed line. `.' is left on the last line input, if there were
163 any, otherwise at the addressed line. Address `0' is legal for
164 this command; text is placed at the beginning of the buffer.
165
166 (., .)c
167 <text>
168 .
169 The change command deletes the addressed lines, then accepts input
170 text which replaces these lines. `.' is left at the last line
171 input; if there were none, it is left at the line preceding the
172 deleted lines.
173
174 (., .)d
175 The delete command deletes the addressed lines from the buffer.
176 The line originally after the last line deleted becomes the cur‐
177 rent line; if the lines deleted were originally at the end, the
178 new last line becomes the current line.
179
180 e filename
181 The edit command causes the entire contents of the buffer to be
182 deleted, and then the named file to be read in. `.' is set to the
183 last line of the buffer. The number of characters read is typed.
184 `filename' is remembered for possible use as a default file name
185 in a subsequent r or w command. If `filename' is missing, the
186 remembered name is used.
187
188 E filename
189 This command is the same as e, except that no diagnostic results
190 when no w has been given since the last buffer alteration.
191
192 f filename
193 The filename command prints the currently remembered file name.
194 If `filename' is given, the currently remembered file name is
195 changed to `filename'.
196
197 (1,$)g/regular expression/command list
198 In the global command, the first step is to mark every line which
199 matches the given regular expression. Then for every such line,
200 the given command list is executed with `.' initially set to that
201 line. A single command or the first of multiple commands appears
202 on the same line with the global command. All lines of a multi-
203 line list except the last line must be ended with `\'. A, i, and
204 c commands and associated input are permitted; the `.' terminating
205 input mode may be omitted if it would be on the last line of the
206 command list. The commands g and v are not permitted in the com‐
207 mand list.
208
209 (.)i
210
211 <text>
212 .
213 This command inserts the given text before the addressed line.
214 `.' is left at the last line input, or, if there were none, at the
215 line before the addressed line. This command differs from the a
216 command only in the placement of the text.
217
218 (., .+1)j
219 This command joins the addressed lines into a single line; inter‐
220 mediate newlines simply disappear. `.' is left at the resulting
221 line.
222
223 ( . )kx
224 The mark command marks the addressed line with name x, which must
225 be a lower-case letter. The address form `′x' then addresses this
226 line.
227
228 (., .)l
229 The list command prints the addressed lines in an unambiguous way:
230 non-graphic characters are printed in two-digit octal, and long
231 lines are folded. The l command may be placed on the same line
232 after any non-i/o command.
233
234 (., .)ma
235 The move command repositions the addressed lines after the line
236 addressed by a. The last of the moved lines becomes the current
237 line.
238
239 (., .)p
240 The print command prints the addressed lines. `.' is left at the
241 last line printed. The p command may be placed on the same line
242 after any non-i/o command.
243
244 (., .)P
245 This command is a synonym for p.
246
247 q The quit command causes ed to exit. No automatic write of a file
248 is done.
249
250 Q This command is the same as q, except that no diagnostic results
251 when no w has been given since the last buffer alteration.
252
253 ($)r filename
254 The read command reads in the given file after the addressed line.
255 If no file name is given, the remembered file name, if any, is
256 used (see e and f commands). The file name is remembered if there
257 was no remembered file name already. Address `0' is legal for r
258 and causes the file to be read at the beginning of the buffer. If
259 the read is successful, the number of characters read is typed.
260 `.' is left at the last line read in from the file.
261
262 ( ., .)s/regular expression/replacement/ or,
263 ( ., .)s/regular expression/replacement/g
264 The substitute command searches each addressed line for an occur‐
265 rence of the specified regular expression. On each line in which
266 a match is found, all matched strings are replaced by the replace‐
267 ment specified, if the global replacement indicator `g' appears
268 after the command. If the global indicator does not appear, only
269 the first occurrence of the matched string is replaced. It is an
270 error for the substitution to fail on all addressed lines. Any
271 character other than space or new-line may be used instead of `/'
272 to delimit the regular expression and the replacement. `.' is
273 left at the last line substituted.
274
275 An ampersand `&' appearing in the replacement is replaced by the
276 string matching the regular expression. The special meaning of
277 `&' in this context may be suppressed by preceding it by `\'. The
278 characters `\n' where n is a digit, are replaced by the text
279 matched by the n-th regular subexpression enclosed between `\('
280 and `\)'. When nested, parenthesized subexpressions are present,
281 n is determined by counting occurrences of `\(' starting from the
282 left.
283
284 Lines may be split by substituting new-line characters into them.
285 The new-line in the replacement string must be escaped by preced‐
286 ing it by `\'.
287
288 (., .)ta
289 This command acts just like the m command, except that a copy of
290 the addressed lines is placed after address a (which may be 0).
291 `.' is left on the last line of the copy.
292
293 (., .)u
294 The undo command restores the preceding contents of the current
295 line, which must be the last line in which a substitution was
296 made.
297
298 (1, $)v/regular expression/command list
299 This command is the same as the global command g except that the
300 command list is executed g with `.' initially set to every line
301 except those matching the regular expression.
302
303 (1, $)w filename
304 The write command writes the addressed lines onto the given file.
305 If the file does not exist, it is created mode 666 (readable and
306 writable by everyone). The file name is remembered if there was
307 no remembered file name already. If no file name is given, the
308 remembered file name, if any, is used (see e and f commands). `.'
309 is unchanged. If the command is successful, the number of charac‐
310 ters written is printed.
311
312 (1,$)W filename
313 This command is the same as w, except that the addressed lines are
314 appended to the file.
315
316 x A key string is demanded from the standard input. Later r, e and
317 w commands will encrypt and decrypt the text with this key by the
318 algorithm of crypt(1). An explicitly empty key turns off encryp‐
319 tion.
320
321 ($)= The line number of the addressed line is typed. `.' is unchanged
322 by this command.
323
324 !<shell command>
325 The remainder of the line after the `!' is sent to sh(1) to be
326 interpreted as a command. `.' is unchanged.
327
328 (.+1)<newline>
329 An address alone on a line causes the addressed line to be
330 printed. A blank line alone is equivalent to `.+1p'; it is useful
331 for stepping through text.
332
333 If an interrupt signal (ASCII DEL) is sent, ed prints a `?' and returns
334 to its command level.
335
336 Some size limitations: 512 characters per line, 256 characters per
337 global command list, 64 characters per file name, and 128K characters
338 in the temporary file. The limit on the number of lines depends on the
339 amount of core: each line takes 1 word.
340
341 When reading a file, ed discards ASCII NUL characters and all charac‐
342 ters after the last newline. It refuses to read files containing non-
343 ASCII characters.
344
346 /tmp/e*
347 ed.hup: work is saved here if terminal hangs up
348
350 B. W. Kernighan, A Tutorial Introduction to the ED Text Editor
351 B. W. Kernighan, Advanced editing on UNIX
352 sed(1), crypt(1)
353
355 `?name' for inaccessible file; `?' for errors in commands; `?TMP' for
356 temporary file overflow.
357
358 To protect against throwing away valuable work, a q or e command is
359 considered to be in error, unless a w has occurred since the last buf‐
360 fer change. A second q or e will be obeyed regardless.
361
363 The l command mishandles DEL.
364 A ! command cannot be subject to a g command.
365 Because 0 is an illegal address for a w command, it is not possible to
366 create an empty file with ed.
367
368
369
370 ED(1)