

ED                        (1) 21-Apr-78                    ED (1)


NAME
       ed - line-oriented text editor

SYNOPSIS
       ed [-] [-n] [-pstring] [file] 

DESCRIPTION
       Ed  is  a  text  editor.  If the 'file' argument is given,
       the file is read into ed's   buffer  so  that  it  can  be
       edited   and  its  name  is remembered for possible future
       use.  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 optional '-' suppresses the printing  of  line  counts
       by the e (edit),  r (read), and w (write) commands. 
       
       The  -p  flag  may  be used to specify ed's prompt string.
       The default is "* ".  If prompting is not desired, a  bare
       -p in the command line will turn it off. 
       
       The  -n  flag  indicates that you want to see line numbers
       prepended to each line of the buffer. 
       
       Ed accepts  commands  from  script  files  as  well  as  a
       terminal.   To  do  this,  invoke  ed  and  substitute the
       script file name for  the standard input, as follows - 
       
                               ed [file] <script 
       
       Commands to ed have a simple and regular structure:  zero,
       one,   or   two   line  addresses  followed  by  a  single
       character command, possibly   followed  by  parameters  to
       the command.  The structure is: 
       
                      [line],[line]command <parameters> 
       
       The  '[line]'  specifies  a  line number or address in the
       buffer.   Every  command  which  requires  addresses   has
       default   addresses,    so  the  addresses  can  often  be
       omitted. 
       
       Line  addresses  may  be   formed   from   the   following
       components: 
       
       
                  17           an integer number 
                  .            the current line 
                  $            the last line in the buffer 
                  .+n          "n" lines past the current line 
                  .-n          "n" lines before the current line 
                  /<pattern>/  a forward context search 
                  \<pattern>\  a backward context search 
       
       Line  numbers may be separated by commas or semicolons;  a
       semicolon sets the current line to  the  previous  address


                               -1-




ED                        (1) 21-Apr-78                    ED (1)


       before  the  next  address  is  interpreted.  This feature
       can  be used to determine the starting  line  for  forward
       and  backward context searches ("/" and "\"). 
       
       
                          REGULAR EXPRESSIONS 
       
       Ed  includes  some  additional  capabilities  such  as the
       ability to  search for  patterns  that  match  classes  of
       characters,   that  match   patterns  only  at  particular
       positions on a line, or that  match   text  of  indefinite
       length.   These  pattern-seaching  capabilities  include a
       class of patterns called   regular  expressions.   Regular
       expressions  are used in addresses to specify lines and in
       the  s  command to specify a portion of a  line  which  is
       to  be replaced.  To be able to express these more general
       patterns,     some     special      characters     (called
       metacharacters)   are   used.    The  regular  expressions
       allowed by ed are constructed as follows: 
       
       1.  An ordinary character  (not  one  of  those  discussed
       below)   is   a    regular  expression  and  matches  that
       character. 
       
       2.   A  percent  "%"  at  the  beginning  of   a   regular
       expression  matches   the empty string at the beginning of
       a line. 
       
       3.  A dollar sign "$" at the end of a  regular  expression
       matches  the null character at the end of a line. 
       
       4.   A  question  mark  "?" matches any character except a
       newline  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 regular expression followed by a  plus  "+"  matches
       one   or   more    adjacent  occurrences  of  the  regular
       expression it follows (anchored closure).  [NOT  AVAILABLE
       ON SOME SYSTEMS]
       
       7.   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 an
       exclamation point "!" the regular  expression matches  any
       character   except   the characters in the string (and the
       newline). 
       
       8.  A string of regular  expressions  enclosed  in  braces
       "{}"  is  known as a tagged pattern, and can be referenced
       positionally as $1...$9  in  the  replacement  side  of  a
       substitute command.  [NOT AVAILABLE ON SOME SYSTEMS]
       
       9.   The concatenation of regular expressions is a regular


                               -2-




ED                        (1) 21-Apr-78                    ED (1)


       expression   which  matches  the  concatenation   of   the
       strings   matched   by  the  components   of  the  regular
       expression. 
       
       10.  The  null  regular  expression  standing   alone   is
       equivalent to the  last regular expression encountered. 
       
       If  it  is  desired  to  use one of the regular expression
       metacharacters  as an ordinary character,  that  character
       may be escaped  by preceding it with an atsign "@". 





                               COMMANDS 
       
       Following  is  a  list  of ed commands.  Default addresses
       are shown  in parentheses: 
       
       (.)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. 
       
       (.)b[+/./-][<screensize>]
            The  browse  command  is a shorthand command to print
            out a screenful of data.  It has three  basic  forms,
            any   of   which   may  have  a  number("screensize")
            appended to it.  The default screensize is  23.   The
            b-  form will print the screen of text preceding (and
            including) the addressed line; b. prints  the  screen
            centered  on  the  addressed line; and b or b+ prints
            the current line and the screen  after  it.   "."  is
            left  at  the  last line printed.  If a screensize is
            specified, it becomes the default screensize for  the
            rest of the editing session or until changed again. 
       
       (.,.)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 any,
            otherwise at the first line not deleted. 
       
       (.,.)d 
            The delete command deletes the addressed  lines  from
            the  buffer.  The line originally AFTER the last line
            deleted becomes the current line;   however,  if  the
            lines  deleted  were  originally  at the end, the new
            last line  becomes the current line. 
       


                               -3-




ED                        (1) 21-Apr-78                    ED (1)


       e filename 
            The edit command causes the entire  contents  of  the
            buffer  to  be deleted  and then the named file to be
            read in.   '.'  is  set  to  the  last  line  of  the
            buffer.    The   number   of  lines  read  is  typed.
            'Filename'  is  remembered  for  possible  use  as  a
            default   file  name  in   a  subsequent   r   or   w
            command.  If changes have been made  to  the  current
            file  since the last write command, you will be asked
            to repeat the edit 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 
            In the global command, the given command is  executed
            for  every   line  which  matches  the  given regular
            expression.  Multiple commands  may  be  executed  by
            placing  each on a preceding line and terminated each
            command except the last with an atsign '@'. 
       
       h
            Prints a small help message. 
       
       
       (.)i 
       <text> 
              . 
            The insert command inserts the given text BEFORE  the
            addressed  line.  '.' is left at the last line input,
            or if there were none, at the  addressed line.   This
            command  differs  from  the   a   command only in the
            placement of text. 
       
       (.,.+1)j
            The join command joins the specified lines  into  one
            line.   '.'  is  left  at the new line created by the
            join.  If the join would  result  in  a  line  longer
            than  MAXLINE characters, an error is reported and no
            changes are made to the file.  A trailing p or l  may
            be  given  on  the  join  command to cause the merged
            line to be printed or listed. 
       
       (.,.)l 
            The  list  command  prints   the   addressed   lines,
            expanding  all ASCII characters with values between 1
            and 31 (^A - ^_) as  the  appropriate  two  character
            digraph,  ^(character).   The  end  of  line  is also
            indicated by a '$'.  '.' is left at  the   last  line
            listed.   The   l  command  may be placed on the same
            line after any other command   to  cause  listing  of
            the last line affected by the command. 
            



                               -4-




ED                        (1) 21-Apr-78                    ED (1)


       n[+/-/=][value]
            This   command   manipulates   the   number  register
            maintained by ed.  A  bare  `n'  causes  the  current
            value  of  the  register  to  be  displayed.  The `='
            function causes the number register to be set to  the
            value  specified,  or to 0 if left null.  The `+' and
            `-'   functions   cause   the    register    to    be
            incremented/decremented  by `value', or by 1 if value
            is null. 
       
       (.,.)m<address> 
            The move  command  repositions  the  addressed  lines
            after  the  line   specified by  <address>.  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  other
            command   to cause printing of the last line affected
            by the command. 
       
       q 
            The quit command causes ed  to  exit.   No  automatic
            write  of  the  file  is  done.  If changes have been
            made  to  the  current  file  since  the  last  write
            command,  you  will  be  asked  to  repeat  the  quit
            command. 
       
       (.)r filename 
            The read command reads in the given  file  after  the
            addressed  line.   If  no  file  name  is  given, the
            remembered  file  name  is  used  (see   e   and    f
            commands).    The   remembered   file   name  is  not
            changed.  Address '0' is legal for this  command  and
            causes  the  file  to be  read in at the beginning 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/regular expression/replacement/       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,
            the  first  occurrence of the  expression is replaced
            by  the  replacement  specified.    If   the   global
            replacement  indicator  g  appears after the command,
            all  occurrences  of  the  regular   expression   are
            replaced.   Any character other than space or newline
            may be used instead of  the slash '/' to delimit  the
            regular  expression and replacement.  A question mark
            '?' is printed  if  the  substitution  fails  on  all
            addressed  lines.   '.'  is  left  at  the  last line
            substituted. 
            


                               -5-




ED                        (1) 21-Apr-78                    ED (1)


            An ampersand '&'  appearing  in  the  replacement  is
            replaced   by    the   string  matching  the  regular
            expression.  (The special  meaning  of  '&'  in  this
            context may be suppressed by  preceding it by '@'.) 
            
            The  strings '$n', '$n+[d]' and '$n-[d]' appearing in
            the replacement string cause  the  current  value  of
            the  number  register  to be placed in the line.  The
            optional trailing  increment/decrement  syntax  cause
            the  number register value to incremented/decremented
            by `d' AFTER the value is placed in the  string.   If
            `d' is omitted, a value of 1 is used. 
            
            Lines  may  be  split  or  merged by using the symbol
            '@n' to stand  for the newline character at  the  end
            of a line. 
            
            
       t [format arguments]
            This  command  allows  one  to  `typeset' the current
            buffer  without  leaving  the  editor.   The  current
            contents  of  the  buffer  are  written  to a scratch
            file, and `format' is invoked  with  a  command  line
            consisting   of   the  scratch  file  name  plus  any
            trailing arguments in  the  `t'  command  line.   For
            example:
            
            t +5 -7
            
            causes  format  to be invoked on the buffer and pages
            5 through 7 to be output.  The value of  '.'  is  not
            changed and the buffer is left intact. 
            
            
       (.)u
            This  causes  the  last  line or range of lines which
            were deleted,  either  via  a  delete  command  or  a
            substitute   command,   to  be  undeleted  after  the
            specified line.  This is NOT an  undo  command.   The
            last  line  or  set  of  lines  deleted are kept in a
            special place before  recycling  the  line  pointers,
            and may be recalled. 
       
       
       (1,$)w filename 
            The  write  command  writes  the addressed lines onto
            the given file.  If the file does not  exist,  it  is
            created.    The   remembered   file   name   is   not
            changed.  If no file name is  given,  the  remembered
            file  name  is  used (see  the  e  and  f  commands).
            '.'  is  left   unchanged.    If   the   command   is
            successful, the number of lines written is typed. 
       
       (1,$)x/regular expression/command 
            The  except command is the same as the global command
            except that the  command is executed for  every  line


                               -6-




ED                        (1) 21-Apr-78                    ED (1)


            except  those matching the regular expression. 
            
       (.)= 
            The  line number of the addressed line is typed.  '.'
            is left unchanged. 
       
       # comment
            The remainder of the line after the "#" is a  comment
            and  ignored  by  the editor.  This allows ed scripts
            to be commented for future enlightenment. 
       
       ^shell command
            The remainder of the line after the "^"  is  sent  to
            the  shell as a command.  If there is nothing else on
            the line but a bare "^", the shell will  be  spawned,
            allowing  a  number of commands to be performed; when
            that shell quits, the terminal  is  returned  to  the
            editor.  "." is left unchanged. 
       
       (.+1)<carriage return> 
            An  address alone on a line causes the addressed line
            to be printed.  A blank line alone is  equivalent  to
            '.+1'  and  thus  is  useful   for  stepping  through
            text.  A minus '-' followed by a carriage  return  is
            equivalent to '.-1'. 


                     SUMMARY OF SPECIAL CHARACTERS 
       
       The following are special characters used by the editor: 
       
        Character       Usage 
        ---------       ----- 
       
           ?            Matches any character (except newline) 
       
           %            Indicates beginning of line 
       
           $            Indicates end of line or end of file 
       
           [...]        Character class (any one of these characters) 
       
           [!...]       Negated character class (any character except these 
                        characters) 
       
           {expression} tagged pattern
       
           *            Closure (zero or more occurrences of previous 
                        pattern)
       
           +            Anchored closure (one or more occurrences)
                        [AVAILABLE ON SOME SYSTEMS]
       
       
           @            Escaped character (e.g. @%, @[, @*) 
       


                               -7-




ED                        (1) 21-Apr-78                    ED (1)


           &            Ditto, i.e. whatever was matched 
       
           c1-c2        Range of characters between c1 and c2 
       
           @n           Specifies the newline character at the end of a line 
       
           @t           Specifies a tab character 
       

FILES
       A  temporary  file  is used to hold the text being edited.
       Two (or more, depending on  the  system)  other  temporary
       files,  known as $1 and $2 etc., may be used as parameters
       for the r,  w,  and  @  commands.   For  example,  if  the
       current  date  and time are desired at the top of the text
       buffer, perform the following:
       
            * ^date >$1
            * 0r $1
            
       As another example, if you wish to make a  copy  of  lines
       1,5 after the last line in the buffer, do the following:
       
            * 1,5w $1
            * $r $1
            

SEE ALSO
       The Unix command "ed" in the Unix manual 
       The software tools tutorial "Edit"
       "Edit  is  for  Beginners"  by  David A. Mosher (available
       from 
                    UC Berkeley Computer Science Library) 
       "Edit:  A Tutorial" (also available from the 
                    UC Berkeley Computer Science Library) 
       "A Tutorial Introduction to the ED Text Editor" by  B.  W.
       Kernighan 
                    (UC Berkeley Computer Science Library) 
       Kernighan and Plauger's "Software Tools", pages 163-217 

DIAGNOSTICS
       The   error  message  "?"  is  printed  whenever  an  edit
       command  fails or is not understood. 

AUTHORS
       Original   code   by   Kernighan    and    Plauger    with
       modifications    by  Debbie  Scherrer,  Dennis  Hall,  Joe
       Sventek and Dave Martin. 

BUGS/DEFICIENCIES
       At the present time the editor  is  still  in  a  somewhat
       experimental   There is a compiled-in limit to the maximum
       number of lines which a file  being  edited  may  contain.
       The   line   limit  applies  to  all  lines  read  in  and
       subsequently   changed.   This  problem  can   be   partly
       alleviated  by  writing  (w  command)  and   re-editing (e


                               -8-




ED                        (1) 21-Apr-78                    ED (1)


       command)  the  file  after  a  lot  of  lines  have   been
       changed. 
       
       There  are  several  discrepancies between this editor and
       Unix's ed.  These include: 
       
            1.  Unix uses 'v'  instead  of  'x'  for  the  except
            command. 
            
            2.    Unix   uses   '^'   instead   of  '%'  for  the
            beginning-of-line character. 
            
            3.  Unix uses '.' instead of '?' to indicate a  match
            of any character. 
            
            4.   Unix  uses   '^'  instead  of  '!'  to  indicate
            exclusion of a  character class. 
            
            5.  Unix uses '\'  instead  of  '@'  for  the  escape
            character. 
            
            6.   Unix  uses  '?'  instead  of  '\'  to  delimit a
            backward search pattern. 
            
            7.  The Unix 'r' command uses the last  line  of  the
            file,  instead   of  the current line, as the default
            address. 
            
            8.  The Unix editor prints the number of  characters,
            rather  than  lines read or written when dealing with
            files. 
            
            

























                               -9-


