

ED                             04/21/78                            ED 


NAME
       ed - text editor

SYNOPSIS
       ed [-] [-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.
       
       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 before
       the next address is interpreted.  This feature can be  used  to


                                 -1-                                  


ED                             04/21/78                            ED 


       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).
       
       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.   The  concatenation  of  regular  expressions  is a regular
       expression which  matches  the  concatenation  of  the  strings
       matched by the components of the regular expression.
       
       9.  The null regular expression standing alone is equivalent to
       the last regular expression encountered.
       


                                 -2-                                  


ED                             04/21/78                            ED 


       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.
       
       e filename
            The edit command causes the entire contents of the  buffer
            to  be  deleted and then the named file to be read in. '.'


                                 -3-                                  


ED                             04/21/78                            ED 


            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,  the user 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 '@'.
       
       (.)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). '.'
            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.
       
       (.,.)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


                                 -4-                                  


ED                             04/21/78                            ED 


            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, the user 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.
            
            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 '@'.)
            
            Lines may be split or merged by using the symbol  '@n'  to
            stand for the newline character at the end of a line.
       
       
       (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


                                 -5-                                  


ED                             04/21/78                            ED 


            The  except  command  is  the  same  as the global command
            except that the command is executed for every line  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)
       
    *            Closure  (zero  or  more  occurrences   of   previous
       pattern)
       
    +            Anchored closure (one or more occurrences)
       


                                 -6-                                  


ED                             04/21/78                            ED 


    @            Escaped character (e.g. @%, @[, @*)
       
    &            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
       other temporary files, known as $1  and  $2,  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 the user wishes 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 and Joe Sventek.

BUGS/DEFICIENCIES
       At  the  present  time  the  editor  is  still  in  a  somewhat
       experimental version. It has not been overlayed yet,  nor  have
       any  real  attempts  been  made  to improve its efficiency. For


                                 -7-                                  


ED                             04/21/78                            ED 


       these reasons, the field length needed for  execution  is  high
       and the files being edited are limited to 1000(VMS-5000) lines.
       The  line  limit  applies to all lines read in and subsequently
       changed. For instance, a 600 line file with 401  changed  lines
       would  exceed  ed's  buffer  space.  This problem can be partly
       alleviated by writing (w command) and  re-editing  (e  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.
            
            
       Because of the  nature  of  the  editor's  IO,  it  is  usually
       impossible  to  properly  print  an  altered  line whenever its
       newline character has been deleted or a new one inserted.  Line
       numbering and counts may also become erratic, although the file
       itself  is  actually correct. This problem can be alleviated by
       writing (w command) and re-editing (e command) the  file  after
       tampering with any newline characters.












                                 -8-                                  

