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

NAME

6       ed, red - text editor
7

SYNOPSIS

9       ed [-GVhs] [-p string] [file]
10
11       red [-GVhs] [-p string] [file]
12

DESCRIPTION

14       ed is a line-oriented text editor.  It is used to create, display, mod‐
15       ify and otherwise manipulate text files.  red is a  restricted  ed:  it
16       can  only  edit files in the current directory and cannot execute shell
17       commands.
18
19       If invoked with a file argument, then a copy of file is read  into  the
20       editor's  buffer.   Changes  are  made to this copy and not directly to
21       file itself.  Upon quitting ed, any changes not explicitly saved   with
22       a `w' command are lost.
23
24       Editing  is  done in two distinct modes: command and input.  When first
25       invoked, ed is in command mode.  In this mode commands  are  read  from
26       the  standard input and executed to manipulate the contents of the edi‐
27       tor buffer.  A typical command might look like:
28
29              ,s/old/new/g
30
31       which replaces all occurrences of the string old with new.
32
33       When an input command, such  as  `a'  (append),  `i'  (insert)  or  `c'
34       (change), is given, ed enters input mode.  This is the primary means of
35       adding text to a file.   In  this  mode,  no  commands  are  available;
36       instead,  the  standard input is written directly to the editor buffer.
37       Lines consist of text up to and including a newline  character.   Input
38       mode is terminated by entering a single period  (.) on a line.
39
40       All  ed  commands  operate on whole lines or ranges of lines; e.g., the
41       `d' command deletes lines; the `m' command moves lines, and so on.   It
42       is possible to modify only a portion of a line by means of replacement,
43       as in the example above.  However even here, the `s' command is applied
44       to whole lines at a time.
45
46       In  general,  ed  commands consist of zero or more line addresses, fol‐
47       lowed by a single character command and possibly additional parameters;
48       i.e., commands have the structure:
49
50              [address [,address]]command[parameters]
51
52       The  address(es)  indicate the line or range of lines to be affected by
53       the command.  If fewer addresses are given than  the  command  accepts,
54       then default addresses are supplied.
55
56
57   OPTIONS
58       -G      Forces backwards compatibility.  Affects the commands `G', `V',
59               `f', `l', `m', `t', and `!!'.
60
61       -s      Suppresses diagnostics. This should be used  if  ed's  standard
62               input is from a script.
63
64
65       -p string
66               Specifies  a  command  prompt.   This may be toggled on and off
67               with the `P' command.
68
69
70       file    Specifies the name of a file to read.  If file is prefixed with
71               a bang (!), then it is interpreted as a shell command.  In this
72               case, what is read is the standard output of file executed  via
73               sh(1).   To  read  a file whose name begins with a bang, prefix
74               the name with a backslash (\).  The default filename is set  to
75               file only if it is not prefixed with a bang.
76
77
78   LINE ADDRESSING
79       An address represents the number of a line in the buffer.  ed maintains
80       a current address which  is  typically  supplied  to  commands  as  the
81       default  address  when  none  is specified.  When a file is first read,
82       the current address is set to the last line of the file.   In  general,
83       the current address is set to the last line affected by a command.
84
85       A  line address is constructed from one of the bases in the list below,
86       optionally followed by a numeric offset.  The offset  may  include  any
87       combination  of  digits,  operators  (i.e.   +  and  -) and whitespace.
88       Addresses are read from left to right, and their  values  are  computed
89       relative to the current address.
90
91       One  exception to the rule that addresses represent line numbers is the
92       address 0 (zero).  This means "before the first  line,"  and  is  legal
93       wherever it makes sense.
94
95       An  address range is two addresses separated either by a comma or semi‐
96       colon. The value of the first address in  a  range  cannot  exceed  the
97       value of the second.  If only one address is given in a range, then the
98       second address is set to the given address.  If an n-tuple of addresses
99       is given where n > 2, then the corresponding range is determined by the
100       last two addresses in the n-tuple.  If only one  address  is  expected,
101       then the last address is used.
102
103       Each  address in a comma-delimited range is interpreted relative to the
104       current address.  In a semicolon-delimited range, the first address  is
105       used  to set the current address, and the second address is interpreted
106       relative to the first.
107
108
109       The following address symbols are recognized.
110
111
112       .       The current line (address) in the buffer.
113
114
115       $       The last line in the buffer.
116
117
118       n       The nth, line in the buffer where n is a number  in  the  range
119               [0,$].
120
121
122       -       The  previous  line.   This  is  equivalent  to  -1  and may be
123               repeated with cumulative effect.
124
125
126       ^n      The nth previous line, where n is a non-negative number.
127
128
129       +       The next line.  This is equivalent to +1 and  may  be  repeated
130               with cumulative effect.
131
132
133       whitespace n
134
135       +n      The  nth  next  line, where n is a non-negative number.  White‐
136               space followed by a number n is interpreted as +n.
137
138
139       ,       The first through last lines in the buffer.  This is equivalent
140               to the address range 1,$.
141
142
143       ;       The  current through last lines in the buffer.  This is equiva‐
144               lent to the address range .,$.
145
146
147       /re/    The next line containing the regular expression re.  The search
148               wraps  to the beginning of the buffer and continues down to the
149               current line, if necessary.  // repeats the last search.
150
151
152       ?re?    The previous line containing the regular  expression  re.   The
153               search  wraps  to the end of the buffer and continues up to the
154               current line, if necessary.  ?? repeats the last search.
155
156
157       'lc     The line previously marked by a `k' (mark) command, where lc is
158               a lower case letter.
159
160
161   REGULAR EXPRESSIONS
162       Regular  expressions are patterns used in selecting text.  For example,
163       the ed command
164
165              g/string/
166
167       prints all lines containing string.  Regular expressions are also  used
168       by the `s' command for selecting old text to be replaced with new.
169
170       In  addition  to  a specifying string literals, regular expressions can
171       represent classes of strings.  Strings thus represented are said to  be
172       matched by the corresponding regular expression.  If it is possible for
173       a regular expression to match several strings in a line, then the left-
174       most longest match is the one selected.
175
176       The following symbols are used in constructing regular expressions:
177
178
179       c       Any character c not listed below, including `{', '}', `(', `)',
180               `<' and `>', matches itself.
181
182
183       \c      A backslash-escaped character c other than `{', '}', `(',  `)',
184               `<', `>', `b', 'B', `w', `W', `+', and `?'  matches itself.
185
186
187       .       Matches any single character.
188
189
190       [char-class]
191               Matches  any single character in char-class.  To include a  `]'
192               in char-class, it must be the  first  character.   A  range  of
193               characters may be specified by separating the end characters of
194               the range with a `-', e.g.,  `a-z'  specifies  the  lower  case
195               characters.  The following literal expressions can also be used
196               in char-class to specify sets of characters:
197
198                 [:alnum:]  [:cntrl:]  [:lower:]  [:space:]
199                 [:alpha:]  [:digit:]  [:print:]  [:upper:]
200                 [:blank:]  [:graph:]  [:punct:]  [:xdigit:]
201
202               If `-' appears as the first or last  character  of  char-class,
203               then  it  matches  itself.   All other characters in char-class
204               match themselves.
205
206               Patterns in char-class of the form:
207
208                 [.col-elm.] or,   [=col-elm=]
209
210               where col-elm is a collating element are interpreted  according
211               to  locale(5)  (not  currently supported).  See regex(3) for an
212               explanation of these constructs.
213
214       [^char-class]
215               Matches any single character, other than newline, not in  char-
216               class.  char-class is defined as above.
217
218       ^       If  `^' is the first character of a regular expression, then it
219               anchors the regular expression to  the  beginning  of  a  line.
220               Otherwise, it matches itself.
221
222       $       If  `$'  is  the  last  character  of  a regular expression, it
223               anchors the regular expression to the end of  a  line.   Other‐
224               wise, it matches itself.
225
226       \(re\)  Defines a (possibly null) subexpression re.  Subexpressions may
227               be nested.  A subsequent backreference of the form `\n',  where
228               n  is  a number in the range [1,9], expands to the text matched
229               by the nth subexpression.  For example, the regular  expression
230               `\(a.c\)\1'  matches  the  string  `abcabc',  but not `abcadc'.
231               Subexpressions are ordered relative to their left delimiter.
232
233       *       Matches the single character regular expression  or  subexpres‐
234               sion  immediately  preceding  it zero or more times.  If '*' is
235               the first character of a regular expression  or  subexpression,
236               then  it  matches  itself.   The  `*' operator sometimes yields
237               unexpected results.  For example, the regular  expression  `b*'
238               matches  the  beginning of the string `abbb', as opposed to the
239               substring `bbb', since a  null  match  is  the  only  left-most
240               match.
241
242       \{n,m\}
243       \{n,\}
244       \{n\}   Matches  the  single character regular expression or subexpres‐
245               sion immediately preceding it at least n and at most  m  times.
246               If  m  is  omitted,  then  it matches at least n times.  If the
247               comma is also omitted, then it matches  exactly  n  times.   If
248               any of these forms occurs first in a regular expression or sub‐
249               expression, then it is interpreted literally (i.e., the regular
250               expression `\{2\}' matches the string `{2}', and so on).
251
252       \<
253       \>      Anchors  the  single character regular expression or subexpres‐
254               sion immediately following it to the beginning (\<)  or  ending
255               (\>)  of  a  word, i.e., in ASCII, a maximal string of alphanu‐
256               meric characters, including the underscore (_).
257
258
259       The following extended operators are preceded by  a  backslash  (\)  to
260       distinguish them from traditional ed syntax.
261
262       \`
263       \´      Unconditionally  matches the beginning (\`) or ending (\´) of a
264               line.
265
266       \?      Optionally matches the single character regular  expression  or
267               subexpression immediately preceding it.  For example, the regu‐
268               lar expression `a[bd]\?c' matches the strings `abc', `adc'  and
269               `ac'.   If  \? occurs at the beginning of a regular expressions
270               or subexpression, then it matches a literal `?'.
271
272       \+      Matches the single character regular expression  or  subexpres‐
273               sion  immediately preceding it one or more times.  So the regu‐
274               lar expression `a\+' is shorthand for `aa*'.  If \+  occurs  at
275               the beginning of a regular expression or subexpression, then it
276               matches a literal `+'.
277
278
279       \b      Matches the beginning or ending (null string) of a word.   Thus
280               the   regular   expression   `\bhello\b'   is   equivalent   to
281               `\<hello\>'.  However, `\b\b' is  a  valid  regular  expression
282               whereas `\<\>' is not.
283
284       \B      Matches (a null string) inside a word.
285
286       \w      Matches any character in a word.
287
288       \W      Matches any character not in a word.
289
290   COMMANDS
291       All  ed  commands are single characters, though some require additional
292       parameters.  If a command's parameters extend over several lines,  then
293       each line except for the last must be terminated with a backslash (\).
294
295       In  general,  at  most  one command is allowed per line.  However, most
296       commands accept a print suffix, which is any of `p' (print), `l' (list)
297       , or `n' (enumerate), to print the last line affected by the command.
298
299       An interrupt (typically ^C) has the effect of aborting the current com‐
300       mand and returning the editor to command mode.
301
302       ed recognizes the following commands.  The commands are shown  together
303       with the default address or address range supplied if none is specified
304       (in parenthesis).
305
306       (.)a    Appends text to the buffer after the addressed line, which  may
307               be  the  address 0 (zero).  Text is entered in input mode.  The
308               current address is set to last line entered.
309
310       (.,.)c  Changes lines in the buffer.  The addressed lines  are  deleted
311               from  the buffer, and text is appended in their place.  Text is
312               entered in input mode.  The current address is set to last line
313               entered.
314
315       (.,.)d  Deletes  the  addressed  lines  from the buffer.  If there is a
316               line after the deleted range, then the current address  is  set
317               to  this line. Otherwise the current address is set to the line
318               before the deleted range.
319
320       e file  Edits file, and sets the default  filename.   If  file  is  not
321               specified,  then  the   default filename is used.  Any lines in
322               the buffer are deleted before the new file is read.   The  cur‐
323               rent address is set to the last line read.
324
325       e !command
326               Edits  the standard output of `!command', (see !command below).
327               The default filename is unchanged.  Any lines in the buffer are
328               deleted  before  the  output  of  command is read.  The current
329               address is set to the last line read.
330
331       E file  Edits file unconditionally.  This is similar to the e  command,
332               except  that  unwritten  changes are discarded without warning.
333               The current address is set to the last line read.
334
335       f file  Sets the default filename to file.  If file is  not  specified,
336               then the default unescaped filename is printed.
337
338       (1,$)g/re/command-list
339               Applies  command-list to each of the addressed lines matching a
340               regular expression re.  The current address is set to the  line
341               currently  matched before command-list is executed.  At the end
342               of the `g' command, the current address is set to the last line
343               affected by command-list.
344
345               Each  command  in  command-list must be on a separate line, and
346               every line except for the last must be terminated  by  a  back‐
347               slash (\).  Any commands are allowed, except for `g', `G', `v',
348               and `V'.  A newline alone in command-list is  equivalent  to  a
349               `p' command.
350
351       (1,$)G/re/
352               Interactively  edits  the  addressed  lines  matching a regular
353               expression re.  For each matching line, the  line  is  printed,
354               the current address is set, and the user is prompted to enter a
355               command-list.  At the end  of  the  `G'  command,  the  current
356               address is set to the last line affected by (the last) command-
357               list.
358
359               The format of command-list is the same as that of the `g'  com‐
360               mand.   A  newline alone acts as a null command list.  A single
361               `&' repeats the last non-null command list.
362
363       H       Toggles the printing of error explanations.  By default, expla‐
364               nations  are  not  printed.   It is recommended that ed scripts
365               begin with this command to aid in debugging.
366
367       h       Prints an explanation of the last error.
368
369       (.)i    Inserts text in the buffer before the current  line.   Text  is
370               entered  in input mode.  The current address is set to the last
371               line entered.
372
373       (.,.+1)j
374               Joins the addressed lines.  The  addressed  lines  are  deleted
375               from  the buffer and replaced by a single line containing their
376               joined text.  The current address is set to the resultant line.
377
378       (.)klc  Marks a line with a lower case letter lc.  The  line  can  then
379               be  addressed  as 'lc (i.e., a single quote followed by lc ) in
380               subsequent commands.  The mark is not cleared until the line is
381               deleted or otherwise modified.
382
383       (.,.)l  Prints  the  addressed  lines unambiguously.  If invoked from a
384               terminal, ed pauses at the end of each page until a newline  is
385               entered.  The current address is set to the last line printed.
386
387       (.,.)m(.)
388               Moves  lines  in  the buffer.  The addressed lines are moved to
389               after the right-hand destination  address,  which  may  be  the
390               address  0  (zero).   The  current  address  is  set to the new
391               address of the last line moved.
392
393       (.,.)n  Prints the addressed lines along with their line numbers.   The
394               current address is set to the last line printed.
395
396       (.,.)p  Prints  the  addressed lines.    If invoked from a terminal, ed
397               pauses at the end of each page until a newline is entered.  The
398               current address is set to the last line printed.
399
400       P       Toggles  the  command  prompt  on and off.  Unless a prompt was
401               specified by with command-line option -p  string,  the  command
402               prompt is by default turned off.
403
404       q       Quits ed.
405
406       Q       Quits  ed  unconditionally.   This is similar to the q command,
407               except that unwritten changes are discarded without warning.
408
409       ($)r file
410               Reads file to after the addressed line.  If file is not  speci‐
411               fied,  then  the  default  filename  is  used.  If there was no
412               default filename prior to the command, then the  default  file‐
413               name  is  set  to  file.   Otherwise,  the  default filename is
414               unchanged.  The current address is set to the last line read.
415
416       ($)r !command
417               Reads to after the addressed line the standard output of `!com‐
418               mand',  (see  the  !command  below).   The  default filename is
419               unchanged.  The current address is set to the last line read.
420
421       (.,.)s/re/replacement/
422       (.,.)s/re/replacement/g
423       (.,.)s/re/replacement/n
424               Replaces text in the addressed lines matching a regular expres‐
425               sion  re with replacement.  By default, only the first match in
426               each line is replaced.  If the `g' (global)  suffix  is  given,
427               then  every match to be replaced.  The `n' suffix, where n is a
428               positive number, causes only the nth match to be replaced.   It
429               is  an  error  if  no substitutions are performed on any of the
430               addressed lines.  The current address is set to the  last  line
431               affected.
432
433               re and replacement may be delimited by any character other than
434               space, newline and the characters used by the form of  the  `s'
435               command  shown  below.  If one or two of the last delimiters is
436               omitted, then the last line affected is printed as  though  the
437               print suffix `p' were specified.
438
439
440               An  unescaped  `&'  in replacement is replaced by the currently
441               matched text.  The character sequence `\m', where m is a number
442               in  the  range  [1,9],  is  replaced  by  the mth backreference
443               expression of the matched text.  If replacement consists  of  a
444               single  `%',  then  replacement  from  the last substitution is
445               used.  Newlines may be embedded  in  replacement  if  they  are
446               escaped with a backslash (\).
447
448       (.,.)s  Repeats  the  last  substitution.  This form of the `s' command
449               accepts a count suffix `n', and any combination of the  charac‐
450               ters  `r',  `g', and `p'.  If a count suffix `n' is given, then
451               only the nth match is replaced.  The `r' suffix causes the reg‐
452               ular  expression  of  the last search to be used instead of the
453               that of the last substitution.   The  `g'  suffix  toggles  the
454               global suffix of the last substitution.  The `p' suffix toggles
455               the print suffix of the last substitution.  The current address
456               is set to the last line affected.
457
458       (.,.)t(.)
459               Copies  (i.e.,  transfers)  the  addressed  lines  to after the
460               right-hand destination address, which  may  be  the  address  0
461               (zero).  The current address is set to the last line copied.
462
463       u       Undoes  the  last  command  and restores the current address to
464               what it was before the command.  The global commands `g',  `G',
465               `v', and `V'.  are treated as a single command by undo.  `u' is
466               its own inverse.
467
468       (1,$)v/re/command-list
469               Applies command-list to each of the addressed lines not  match‐
470               ing  a  regular expression re.  This is similar to the `g' com‐
471               mand.
472
473       (1,$)V/re/
474               Interactively edits the addressed lines not matching a  regular
475               expression re.  This is similar to the `G' command.
476
477       (1,$)w file
478               Writes  the  addressed lines to file.  Any previous contents of
479               file is lost without warning.  If there is no default filename,
480               then  the  default  filename  is  set  to file, otherwise it is
481               unchanged.  If no filename is specified, then the default file‐
482               name is used.  The current address is unchanged.
483
484       (1,$)wq file
485               Writes  the  addressed  lines  to file, and then executes a `q'
486               command.
487
488       (1,$)w !command
489               Writes the addressed lines to the standard input of `!command',
490               (see  the  !command  below).   The default filename and current
491               address are unchanged.
492
493       (1,$)W file
494               Appends the addressed lines to the end of file.  This is  simi‐
495               lar  to  the  `w' command, expect that the previous contents of
496               file is not clobbered.  The current address is unchanged.
497
498       (.)x    Copies (puts) the contents of  the  cut  buffer  to  after  the
499               addressed  line.   The  current address is set to the last line
500               copied.
501
502       (.,.)y  Copies (yanks) the addressed lines to the cut buffer.  The  cut
503               buffer  is overwritten by subsequent `y', `s', `j', `d', or `c'
504               commands.  The current address is unchanged.
505
506       (.+1)zn Scrolls n lines at a time starting at addressed line.  If n  is
507               not  specified, then the current window size is used.  The cur‐
508               rent address is set to the last line printed.
509
510       !command
511               Executes command via sh(1).  If the first character of  command
512               is `!', then it is replaced by text of the previous `!command'.
513               ed does not process command for backslash  (\)  escapes.   How‐
514               ever,  an  unescaped  `%'  is replaced by the default filename.
515               When the shell returns from execution, a `!'  is printed to the
516               standard output.  The current line is unchanged.
517
518       (.,.)#  Begins  a  comment;   the rest of the line, up to a newline, is
519               ignored.  If a line address followed by a semicolon  is  given,
520               then  the  current  address is set to that address.  Otherwise,
521               the current address is unchanged.
522
523       ($)=    Prints the line number of the addressed line.
524
525       (.+1)newline
526               Prints the addressed line, and sets the current address to that
527               line.
528

FILES

530       ed.hup  The  file to which ed attempts to write the  buffer if the ter‐
531               minal hangs up.
532

SEE ALSO

534       vi(1), sed(1), regex(3), sh(1).
535
536       USD:12-13
537
538       B. W. Kernighan and P. J. Plauger, Software Tools in Pascal ,  Addison-
539       Wesley, 1981.
540

LIMITATIONS

542       ed  processes  file  arguments for backslash escapes, i.e.,  in a file‐
543       name, any characters preceded by a backslash (\) are interpreted liter‐
544       ally.
545
546       If  a  text (non-binary) file is not terminated by a newline character,
547       then ed appends one on reading/writing it.  In the  case  of  a  binary
548       file, ed does not append a newline on reading/writing.
549
550       per line overhead: 4 ints
551

DIAGNOSTICS

553       When an error occurs, if ed's input is from a regular file or here doc‐
554       ument, then it exits, otherwise it prints a `?' and returns to  command
555       mode.   An  explanation  of  the last error can be printed with the `h'
556       (help) command.
557
558       Attempting to quit ed or edit another file before  writing  a  modified
559       buffer  results  in an error.  If the command is entered a second time,
560       it succeeds, but any changes to the buffer are lost.
561
562       ed exits with 0 if no errors occurred; otherwise >0.
563
564
565
566                                 13 June 2009                            ED(1)
Impressum