
NAME
ed - text editor - V3.23

SYNOPSIS
ed [ -abcdefilmnqst ] [ name ]

DESCRIPTION
ed is the standard text editor.

If a name argument is given,  ed  simulates  an  e  command  (see
below)  on  the named file; that is to say, the file is read into
ed's buffer so that it can be edited.  After every  15  (default)
commands have been executed, the edit buffer will be written on a
scratch file.  When ed terminates  successfully,  the  save  file
will  be removed unless the -d flag was selected.  If a writeable
file named edsav exists in the current directory, ed  will  write
all the user's commands onto the file.

The optional flags after the - have the following functions:

     -a
          The line numbers will be printed in apl mode.  The form
          is  "[ n ]\t" followed by the text.  In addition, over-
          struck characters will be printed  on  two  lines,  one
          above the other.

     -b
          Make a backup copy of the edit file upon entry  to  the
          editor.   The  file's name will be that of the original
          file with a .bak extension.

     -cnn
          Set the editor's idea of the depth of  the  Crt  screen
          for  the  pa  command  to  nn (decimal).  Default is 21
          lines.  If nn is zero, the paging will be disabled.

     -d
          Disables the deletion upon exit of the file created via
          the auto-save feature.  (The .tmp file).

     -e
          Each input command will be echoed on  standard  output.
          This  is  useful  for  debugging  editor command files,
          since the error message will be immediately preceded by
          the command that caused it.

     -f
          The editor will automatically  prompt  for  text  lines
          upon  being  invoked.   Upon  exit, the editor will au-
          tomatically write the file.  This is useful for  creat-
          ing files without having to type the a command upon en-
          try.  Note: If this flag is selected, the  editor  will
          over-write an existing file by the same name.

     -i
          If an interrupt (ASCII DEL) character is typed, the ed-
          itor will write the current contents of the edit buffer
          on a file, and exit.  The name of the dump file is that
          of  the  original  file  with a .int extension.  The -i
          flag is very useful for shell files which call the edi-
          tor, since the editor will not hang around after an in-
          terrupt, interfering with the user's commands.

     -lc
          The eol character is initialized to  character  c.   It
          may  be changed during the edit session by the e=c com-
          mand.

     -mnn
          The modification count before an automatic save of  the
          edit  buffer  is  set  to nn (decimal).  Default is 15.
          (That is, after every 15 commands which cause a modifi-
          cation  to  one  or more lines, the edit buffer will be
          written on the edit file name with .tmp extension.)  If
          the count is zero, the auto-save feature is disabled.

     -n
          The no-line-numbers flag is set.  This results  in  the
          omission of line number prompts as well as line numbers
          on text lists.

     -q
          The editor will NOT ignore a quit (ASCII FS or ctrl-\).

     -s
          Silent mode.  No prompts are issued.  This mode is use-
          ful for running editor command files.

     -tc
          Set the tab character to  c.   This  is  the  character
          which  will  be  expanded  to the appropriate number of
          blanks to get to the next column which has a  tab  stop
          set  in it.  The tab character may be set/changed using
          the t=c command.

     -0123456789
          A decimal number preceded by a - will set a tab stop in
          that column.  Tab settings may be made during edit ses-
          sion by the t,nn command.

ed operates on a copy of any file it is editing; changes made  in
the  copy have no effect on the file until a w (write) command is
given.  The copy of the text being edited resides in a  temporary
file called the buffer.  There is only one buffer.

Commands to ed have a simple and regular structure: zero or  more
addresses  followed  by a one or more character command, possibly
followed by parameters to the command.  These  addresses  specify
one  or  more  lines in the buffer.  Every command which requires
addresses has default addresses, so that the addresses can  often
be omitted.

In general, only one command may appear on a line.  Certain  com-
mands  allow  the  input of text.  This text is placed in the ap-
propriate place in the buffer.  While ed is accepting text, it is
said  to  be in input mode.  In this mode, no commands are recog-
nized; all input is merely collected.  Input mode is left by typ-
ing  a period "." alone at the beginning of a line, or by receipt
of an end-of-file (Ctrl-D) from the keyboard.

ed supports a limited form of  regular  expression  notation.   A
regular  expression  specifies a set of strings of characters.  A
member of this set of strings is said to be matched by the  regu-
lar  expression.   The regular expressions allowed by ed are con-
structed as follows:

     1. An ordinary character (not one of those discussed  below)
        is a regular expression and matches that character.

     2. A circumflex "^" at the beginning of a regular expression
        matches the empty string at the beginning of a line.

     3. A currency symbol "$" at the end of a regular  expression
        matches the null character at the end of a line.

     4. A period "." matches  any  character  except  a  new-line
        character.

     5. A regular expression followed by an asterisk "*"  matches
        any  number  of  adjacent occurrences (including zero) of
        the regular expression it follows.

     6. A string of characters enclosed in square brackets "[  ]"
        matches  any  character in the string but no others.  If,
        however, the first character of the string is  a  circum-
        flex "^" the regular expression matches any character ex-
        cept new-line and the characters in the string.

     7. The concatenation of regular expressions is a regular ex-
        pression  which  matches the concatenation of the strings
        matched by the components of the regular expression.

     8. A regular expression enclosed between the sequences  "\("
        and  "\)"  is  identical to the unadorned expression; the
        construction has side effects discussed under the s  com-
        mand.

     9. The null regular expression standing alone is  equivalent
        to the last regular expression encountered.

Regular expressions are used in addresses to specify lines and in
one command (see s below) to specify a portion of a line which is
to be replaced.  If it is desired to use one of the  regular  ex-
pression  metacharacters as an ordinary character, that character
may be preceded by "\".   This  also  applies  to  the  character
bounding the regular expression (often "/") and to "\" itself.

To understand addressing in ed it is necessary to  know  that  at
any  time  there  is  a  current  line.   Generally speaking, the
current line is the last line affected by a command; however, the
exact  effect on the current line is discussed under the descrip-
tion of the command.  Addresses are constructed as follows.

        1. The character "." addresses the current line.

        2. The character "$"  addresses  the  last  line  of  the
           buffer.

        3. A decimal number n addresses  the  n-th  line  of  the
           buffer.

        4. "'x" addresses the line  marked  with  the  mark  name
           character  x,  which  must be a lower-case letter.  An
           alternative to  this  syntax  is  the  capital  letter
           alone.   Lines are marked with the k command described
           below.

        5. A regular expression enclosed in slashes "/" addresses
           the  first  line  found by searching toward the end of
           the buffer and stopping at the first line containing a
           string  matching the regular expression.  If necessary
           the search  wraps  around  to  the  beginning  of  the
           buffer.

        6. A regular expression enclosed in queries "?" addresses
           the first line found by searching toward the beginning
           of the buffer and stopping at the first line  contain-
           ing  a  string  matching  the  regular expression.  If
           necessary the search wraps around to the  end  of  the
           buffer.

        7. An address followed by a plus sign "+" or a minus sign
           "-"  followed  by  a decimal number specifies that ad-
           dress plus  (resp.  minus)  the  indicated  number  of
           lines.  The plus sign may be omitted.

        8. If an address begins with "+" or "-" the  addition  or
           subtraction is taken with respect to the current line;
           e.g. "-5" is understood to mean ".-5".

        9. If an address ends with "+" or "-", then  1  is  added
           (resp. subtracted).  As a consequence of this rule and
           rule 8, the address "-" refers to the line before  the
           current  line.  Moreover, trailing "+" and "-" charac-
           ters have cumulative effect, so  "--"  refers  to  the
           current line less 2.

        10. To maintain compatibility with earlier version of the
           editor,  the  character  "^"  in addresses is entirely
           equivalent to "-".

Commands may require zero, one, or two addresses.  Commands which
require  no addresses regard the presence of an address as an er-
ror.  Commands which accept one or two addresses  assume  default
addresses  when  insufficient  are  given.  If more addresses are
given than such a command requires, the last one or two  (depend-
ing on what is accepted) are used.

Addresses are separated from each other typically by a comma  ","
They  may also be separated by a semicolon ";".  In this case the
current line "." is set to the previous address before  the  next
address  is  interpreted.   This feature can be used to determine
the starting line for forward and backward searches  ("/",  "?").
The second address of any two-address sequence must correspond to
a line following the line corresponding to the first address.

In the following list of ed commands, the default  addresses  are
shown  in  parentheses.   The parentheses are not part of the ad-
dress, but are used to show that the given addresses are the  de-
fault.

As mentioned, it is generally illegal for more than  one  command
to  appear on a line.  However, any command may be suffied by "p"
or by "l", in which case the current line is  either  printed  or
listed respectively in the way discussed below.

     ( . )a <text> or,
     ( . )a
     <text>
     .
          The append command reads the given text and appends  it
          after the addressed line.  "." is left on the last line
          input, if there were any, otherwise  at  the  addressed
          line.   Address  "0" is legal for this command; text is
          placed at the beginning of the buffer.

     ( . , . )a/<text>
          Append the text after the last  character  in  the  ad-
          dressed lines.

     b nn
          The browse count is set to nn (where nn is a  (decimal)
          number).   This  count is then used for subsequent new-
          line commands as the number of lines to be printed out.

          If nn is missing, the count is reset to 1.

     ( . , . )c/regular expression/replacement/ or,
     ( . , . )c/regular expression/replacement/nn or,
     ( . , . )c/regular expression/replacement/g
          This form of the change command is identical to  the  s
          command below.

     ( . , . )co a
          The co (copy) command is identical  to  the  t  command
          below.

     ( . , . )c
     <text>
     .
          The change command deletes the  addressed  lines,  then
          accepts  input text which replaces these lines.  "." is
          left at the last line input; if there were none, it  is
          left at the first line not deleted.

     ( . , . )d
          The delete command deletes the addressed lines from the
          buffer.  The line originally after the last line delet-
          ed becomes the current line; if the lines deleted  were
          originally  at  the  end, the new last line becomes the
          current line.

     e filename
          The edit command causes  the  entire  contents  of  the
          buffer  to  be  deleted,  and then the named file to be
          read in.  If no filename is given, the current file  is
          used.   "." is set to the last line of the buffer.  The
          number of lines read is typed.  "filename" (if present)
          is  remembered  for possible use as a default file name
          in a subsequent e, r, or w command.

     f filename
          The filename command prints  the  currently  remembered
          file  name.   If  "filename"  is  given,  the currently
          remembered file name is changed to "filename".

     ( 1 , $ )g/regular expression/command list
          In the global command, the first step is to mark  every
          line  which matches the given regular expression.  Then
          for every such line, the given command list is executed
          with  "." initially set to that line.  A single command
          or the first of multiple commands appears on  the  same
          line  with  the  global command.  All lines of a multi-
          line list except the last line must be ended with  "\".
          A,  i,  and c commands and associated input are permit-
          ted; the "." terminating input mode may be  omitted  if
          it  would be on the last line of the command list.  The
          (global) commands, g, and v, are not permitted  in  the
          command list.

     h    or,
     hnn
          Column numbers to column nn (default = 71) are  printed
          out.   Any  columns which have tab stops set will print
          out with - character in their position.

     he[lp]
          List syntax of all ed commands available.

     ( . )i <text> or,
     ( . )i
     <text>
     .
          This command inserts the  given  text  before  the  ad-
          dressed  line.   "." is left at the last line input; if
          there were none, at the addressed line.   This  command
          differs from the a command only in the placement of the
          text.

     ( .-1 , . )j
          Join the addressed line to the line  before  it.   This
          effectively  removes  the  new-line from the end of the
          first line.  Also useful for rejoining a line that  was
          split incorrectly by the s command.

     k    or,
     ( . )kx
          The mark command marks the addressed line with name  x,
          which  must  be a lower-case letter.  Either of the ad-
          dress forms 'x or X  (capital  letter)  then  addresses
          this line.  If no character is specified after the com-
          mand, all currently marked lines are listed.

     ( . , . )l
          The list command prints the addressed lines in an unam-
          biguous  way: non-graphic characters are printed in oc-
          tal, and long lines are folded.  An l command may  fol-
          low any other on the same line.

     ll
          The entire contents of the edit buffer are listed as if
          "1,$l" had been typed.

     ( . , . )m a or,
     ( . , . )mo a
          The move command repositions the addressed lines  after
          the  line  addressed by a.  The last of the moved lines
          becomes the current line.

     ( . , . )p
          The print command prints the addressed lines.   "."  is
          left  at  the  last line printed.  The p command may be
          placed on the same line after any command.

     pp
          The entire contents of the edit buffer are listed as if
          "1,$p" had been typed.

     q    or,
     qi
          The quit command causes ed to exit.  No automatic write
          of  a file is done.  If the edit file has been modified
          and the entire contents of the  buffer  have  not  been
          written  to  a  file, a query will be issued to be sure
          that the user has not forgotten to write his file.

          If the "i" is present, the editor will quit immediately
          (without double-checking first).

     ( $ )r filename
          The read command reads in the given file after the  ad-
          dressed line.  If no file name is given, the remembered
          file name, if any, is used (see e and f commands).  The
          remembered  file  name is not changed unless "filename"
          is the very first file name mentioned.  Address "0"  is
          legal  for  r and causes the file to be read at the be-
          ginning of the buffer.  If the read is successful,  the
          number of lines read is typed.  "." is left at the last
          line read in from the file.

     s
          The s command without any parameters  performs  an  au-
          tomatic  write  (w)  if  the file has been modified and
          then exits the editor.

     ( . , . )s/regular expression/replacement/ or,
     ( . , . )s/regular expression/replacement/nn or,
     ( . , . )s/regular expression/replacement/g
          The substitute command searches each addressed line for
          an  occurrence of the specified regular expression.  On
          each line in which a match is found, one of the  folow-
          ing  actions  are  taken for each of the three forms of
          the command:

          1.   The first occurrence of the  specified  expression
          is replaced by the replacement text.

          2.   The nn-th (where  nn  is  a  decimal  number)  oc-
          currence of the specified expression is replaced by the
          replacement text.

          3.   All occurrences of the  specified  expression  are
          replaced.

          It is an error for the substitution to fail on all  ad-
          dressed  lines.   Any character other than new-line may
          be used instead of "/" to delimit the  regular  expres-
          sion and the replacement.  "." is left at the last line
          substituted.

          An ampersand "&" appearing in the  replacement  is  re-
          placed  by  the string matching the regular expression.
          The special meaning of  "&"  in  this  context  may  be
          suppressed  by  preceding it by "\".  As a more general
          feature, the characters 0 where n is a digit,  are  re-
          placed  by  the text matched by the n-th regular subex-
          pression enclosed between "\(" and "\)".  When  nested,
          parenthesized  subexpressions  are present, n is deter-
          mined by counting occurrences of "\(" starting from the
          left.

          Lines may be split by substituting new-line  characters
          into them.  The new-line in the replacement string must
          be escaped by preceding it by "\".

     sa nn
          The save-count command changes the default  (15)  count
          of  text-changing commands which may be executed before
          an automatic buffer save will be done.  ( nn is  a  de-
          cimal  number.)  The  save  file name is file.tmp where
          file is the name of the file being edited.  A count  of
          zero (0) will disable the auto-save feature.

     ( . , . )t a
          This command acts just like the m command, except  that
          a copy of the addressed lines is placed after address a
          (which may be 0).  "." is left at the last line of  the
          copy.

     t=c
          Set tab character to c.  All occurrecnces of this char-
          acter  entered  by the a or i commands will be expanded
          to the appropriate number of blanks to get to the  next
          column  with  a  tab  stop.  Any occurrences of the tab
          character after the last tab column will be  untouched.
          (IE. not converted to blank(s).)

     t,nn,nn,nn ...
          Set tab stops in specified (numeric) columns.

     u filename or,
     up filename
          The editor opens the specified file, and reads  command
          lines from it.  The commands are echoed to the terminal
          (if the p is present) as each character  is  processed.
          This  allows  monitoring the command file as it is run-
          ning, so that erroneous command line(s) will appear be-
          fore  their  respective error messages.  If no filename
          is given, the last used filename, if any, will be used.

     ( 1 , $ )v/regular expression/command list
          This command is the same as the global  command  except
          that  the  command  list is executed with "." initially
          set to every line except those matching the regular ex-
          pression.

     ( 1 , $ )w filename
          The write command writes the addressed lines  onto  the
          given  file.  If the file does not exist, it is created
          mode 0644.  The remembered file name is not changed un-
          less  "filename" is the very first file name mentioned.
          If no file name is given, the remembered file name,  if
          any, is used (see e and f commands).  "." is unchanged.

     ( . )x
          Undelete is used to recover the most  recently  deleted
          (or  replaced) block of lines.  "." is left at the last
          recovered line.

          Example:
               25,34d  delete the lines
               -10,+10p see the damage
               24x  recovers the lost lines

     ( . )y+ or,
     y    or,
     y-
          The y command changes the processing  of  an  interrupt
          character  received  from  the terminal.  If the "-" is
          present, normal processing takes place.  That  is,  the
          message  "INTERRUPT!" will be displayed on the terminal
          and ed will prompt for another command.  If the "+"  is
          present,  the  addressed line is set as the initial ad-
          dress for the pa command, which will  automatically  be
          invoked  upon  each interrupt.  Lastly, if no character
          follows, then upon each interrupt,  one  page  will  be
          displayed  from  the  current position onward, which is
          useful for paging through sections of code (or text, or
          whatever).

     =<command>
          The <command> is invoked with  the  previous  command's
          address bounds.

     !UNIX-command
          The remainder of the line after the "!" is sent to UNIX
          to  be  interpreted  as  a  shell  command.  "." is un-
          changed.

     ( .+1 , .+nn )<newline>
          An address alone on a line causes the addressed line to
          be  printed.   A  blank  line  alone  is  equivalent to
          ".+nnp"; it is useful for stepping through  text.   The
          nn is the count specified with the b command.

If an interrupt signal (ASCII DEL) is  sent,  ed  prints  "INTER-
RUPT!" and returns to its command level.  (See also the y command
for alternate interrupt processing).

Some size limitations:
512 characters per line,
256 characters per global command list,
64 characters per file name,
128K characters in the temporary file.  The limit on  the  number
of lines depends on the amount of core: each line takes 1 word.

FILES
/tmp/e?????, temporary; ????? is process number (in decimal).
file.hup if hangup signal is received.
file.bak if -b flag is specified.
file.int if -i flag is specified and interrupt is received.
file.tmp auto-save (every 15 commands).

DIAGNOSTICS
Each command has self-explanatory error messages.

SEE ALSO
A Tutorial Introduction to the ED Text Editor (B. W. Kernighan)

BUGS
The k command doesn't mark line 1 properly.

