1ED(1)                       General Commands Manual                      ED(1)
2
3
4

NAME

6       ed - text editor
7

SYNOPSIS

9       ed [ - ] [ name ]
10

DESCRIPTION

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.   The  optional - suppresses the printing of
17       explanatory output and should be used when the  standard  input  is  an
18       editor script.
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.  The special form `%' is an  abbre‐
148       viation for the address pair `1,$'.
149
150       In  the  following list of ed commands, the default addresses are shown
151       in parentheses.  The parentheses are not part of the address,  but  are
152       used to show that the given addresses are the default.
153
154       As  mentioned,  it  is  generally  illegal for more than one command to
155       appear on a line.  However, most commands may be suffixed by `p' or  by
156       `l', in which case the current line is either printed or listed respec‐
157       tively in the way discussed below.  Commands may also  be  suffixed  by
158       `n',  meaning  the output of the command is to be line numbered.  These
159       suffixes may be combined in any order.
160
161       (.)a
162       <text>
163       .
164            The append command reads the given text and appends it  after  the
165            addressed line.  `.' is left on the last line input, if there were
166            any, otherwise at the addressed line.  Address `0'  is  legal  for
167            this command; text is placed at the beginning of the buffer.
168
169       (., .)c
170       <text>
171       .
172            The change command deletes the addressed lines, then accepts input
173            text which replaces these lines.  `.' is left  at  the  last  line
174            input;  if  there  were none, it is left at the line preceding the
175            deleted lines.
176
177       (., .)d
178            The delete command deletes the addressed lines  from  the  buffer.
179            The  line  originally after the last line deleted becomes the cur‐
180            rent line; if the lines deleted were originally at  the  end,  the
181            new last line becomes the current line.
182
183       e filename
184            The  edit  command  causes the entire contents of the buffer to be
185            deleted, and then the named file to be read in.  `.' is set to the
186            last  line of the buffer.  The number of characters read is typed.
187            `filename' is remembered for possible use as a default  file  name
188            in  a  subsequent  r  or w command.  If `filename' is missing, the
189            remembered name is used.
190
191       E filename
192            This command is the same as e, except that no  diagnostic  results
193            when no w has been given since the last buffer alteration.
194
195       f filename
196            The  filename  command  prints the currently remembered file name.
197            If `filename' is given, the  currently  remembered  file  name  is
198            changed to `filename'.
199
200       (1,$)g/regular expression/command list
201            In  the global command, the first step is to mark every line which
202            matches the given regular expression.  Then for every  such  line,
203            the  given command list is executed with `.' initially set to that
204            line.  A single command or the first of multiple commands  appears
205            on  the  same line with the global command.  All lines of a multi-
206            line list except the last line must be ended with `\'.  A, i,  and
207            c commands and associated input are permitted; the `.' terminating
208            input mode may be omitted if it would be on the last line  of  the
209            command  list.  The commands g and v are not permitted in the com‐
210            mand list.
211
212       (.)i
213
214       <text>
215       .
216            This command inserts the given text  before  the  addressed  line.
217            `.' is left at the last line input, or, if there were none, at the
218            line before the addressed line.  This command differs from  the  a
219            command only in the placement of the text.
220
221       (., .+1)j
222            This  command joins the addressed lines into a single line; inter‐
223            mediate newlines simply disappear.  `.' is left at  the  resulting
224            line.
225
226       ( . )kx
227            The  mark command marks the addressed line with name x, which must
228            be a lower-case letter.  The address form `′x' then addresses this
229            line.
230
231       (., .)l
232            The list command prints the addressed lines in an unambiguous way:
233            non-graphic characters are printed in two-digit  octal,  and  long
234            lines  are  folded.   The l command may be placed on the same line
235            after any non-i/o command.
236
237       (., .)ma
238            The move command repositions the addressed lines  after  the  line
239            addressed  by  a.  The last of the moved lines becomes the current
240            line.
241
242       (., .)p
243            The print command prints the addressed lines.  `.'  is left at the
244            last  line  printed.  The p command may be placed on the same line
245            after any non-i/o command.
246
247       (., .)P
248            This command is a synonym for p.
249
250       q    The quit command causes ed to exit.  No automatic write of a  file
251            is done.
252
253       Q    This  command  is the same as q, except that no diagnostic results
254            when no w has been given since the last buffer alteration.
255
256       ($)r filename
257            The read command reads in the given file after the addressed line.
258            If  no  file  name  is given, the remembered file name, if any, is
259            used (see e and f commands).  The file name is remembered if there
260            was  no  remembered file name already.  Address `0' is legal for r
261            and causes the file to be read at the beginning of the buffer.  If
262            the  read  is  successful, the number of characters read is typed.
263            `.' is left at the last line read in from the file.
264
265       ( ., .)s/regular expression/replacement/       or,
266       ( ., .)s/regular expression/replacement/g
267            The substitute command searches each addressed line for an  occur‐
268            rence  of the specified regular expression.  On each line in which
269            a match is found, all matched strings are replaced by the replace‐
270            ment  specified,  if  the global replacement indicator `g' appears
271            after the command.  If the global indicator does not appear,  only
272            the  first occurrence of the matched string is replaced.  It is an
273            error for the substitution to fail on all  addressed  lines.   Any
274            punctuation  character  may  be used instead of `/' to delimit the
275            regular expression and the replacement.  `.' is left at  the  last
276            line substituted.
277
278            An  ampersand  `&' appearing in the replacement is replaced by the
279            string matching the regular expression.  The  special  meaning  of
280            `&' in this context may be suppressed by preceding it by `\'.  The
281            characters `\n' where n is a  digit,  are  replaced  by  the  text
282            matched  by  the  n-th regular subexpression enclosed between `\('
283            and `\)'.  When nested, parenthesized subexpressions are  present,
284            n  is determined by counting occurrences of `\(' starting from the
285            left.
286
287            Lines may be split by substituting new-line characters into  them.
288            The  new-line in the replacement string must be escaped by preced‐
289            ing it by `\'.
290
291            One or two trailing delimiters may be omitted,  implying  the  `p'
292            suffix.   The  special  form `s' followed by no delimiters repeats
293            the most recent substitute command on the  addressed  lines.   The
294            `s'  may be followed by the letters r (use the most recent regular
295            expression for the left hand side, instead of the most recent left
296            hand  side  of a substitute command), p (complement the setting of
297            the p suffix from the previous substitution), or g (complement the
298            setting  of  the  g suffix).  These letters may be combined in any
299            order.
300
301       (., .)ta
302            This command acts just like the m command, except that a  copy  of
303            the  addressed  lines  is placed after address a (which may be 0).
304            `.' is left on the last line of the copy.
305
306       (., .)u
307            The undo command restores the buffer to it's state before the most
308            recent  buffer  modifying  command.   The  current  line  is  also
309            restored.  Buffer modifying commands are a, c, d, g, i, k,  m,  r,
310            s, t, and v.  For purposes of undo, g and v are considered to be a
311            single buffer modifying command.  Undo is its own inverse.
312
313            When ed runs out of memory (at about 8000  lines  on  any  16  bit
314            mini-computer  such as the PDP-11) This full undo is not possible,
315            and u can only undo the effect of the most  recent  substitute  on
316            the  current  line.   This  restricted undo also applies to editor
317            scripts when ed is invoked with the - option.
318
319       (1, $)v/regular expression/command list
320            This command is the same as the global command g except  that  the
321            command  list  is  executed g with `.' initially set to every line
322            except those matching the regular expression.
323
324       (1, $)w filename
325            The write command writes the addressed lines onto the given  file.
326            If  the  file  does  not  exist,  it is created.  The file name is
327            remembered if there was no remembered file name  already.   If  no
328            file name is given, the remembered file name, if any, is used (see
329            e and f commands).  `.' is unchanged.  If the command is  success‐
330            ful, the number of characters written is printed.
331
332       (1, $)W filename
333            This command is the same as w, except that the addressed lines are
334            appended to the file.
335
336       (1, $)wq filename
337            This command is the same as w except that afterwards a  q  command
338            is done, exiting the editor after the file is written.
339
340       (.+1)z    or,
341       (.+1)zn
342            This  command scrolls through the buffer starting at the addressed
343            line.  22 (or n, if given)  lines  are  printed.   The  last  line
344            printed  becomes the current line.  The value n is sticky, in that
345            it becomes the default for future z commands.
346
347       ($)= The line number of the addressed line is typed.  `.' is  unchanged
348            by this command.
349
350       !<shell command>
351            The  remainder  of  the  line after the `!' is sent to sh(1) to be
352            interpreted as a command.  `.'  is unchanged.
353
354       (.+1,.+1)<newline>
355            An address alone on  a  line  causes  the  addressed  line  to  be
356            printed.  A blank line alone is equivalent to `.+1p'; it is useful
357            for stepping through text.  If two addresses are present  with  no
358            intervening  semicolon, ed prints the range of lines.  If they are
359            separated by a semicolon, the second line is printed.
360
361       If an interrupt signal (ASCII DEL) is sent,  ed  prints  `?interrupted'
362       and returns to its command level.
363
364       Some  size  limitations:  512  characters  per line, 256 characters per
365       global command list, 64 characters per file name, and, on mini  comput‐
366       ers, 128K characters in the temporary file.  The limit on the number of
367       lines depends on the amount of core: each line takes 2 words.
368
369       When reading a file, ed discards ASCII NUL characters and  all  charac‐
370       ters  after the last newline.  It refuses to read files containing non-
371       ASCII characters.
372

FILES

374       /tmp/e*
375       edhup: work is saved here if terminal hangs up
376

SEE ALSO

378       B. W. Kernighan, A Tutorial Introduction to the ED Text Editor
379       B. W. Kernighan, Advanced editing on UNIX
380       ex(1), sed(1), crypt(1)
381

DIAGNOSTICS

383       `?name' for inaccessible file; `?self-explanatory  message'  for  other
384       errors.
385
386       To  protect  against  throwing  away valuable work, a q or e command is
387       considered to be in error, unless a w has occurred since the last  buf‐
388       fer change.  A second q or e will be obeyed regardless.
389

BUGS

391       The l command mishandles DEL.
392       The undo command causes marks to be lost on affected lines.
393
394
395
3967th Edition                     August 12, 1986                          ED(1)
Impressum