UNIX Editors
 ************* BACK AND FORTH PAGING SUBROUTINE ************

PAGEFLOP

If they want to quit, do so
 If They want to quit subsession, Do SO!
  Decrement LOCAL and GLOBAL Page Counters

FORWARD

  Increment LOCAL and GLOBAL Page Counters
   *************** END PAGEFLOP *********************
  ****** CHAPTER INTERNAL SELECTOR ALGORITHM ******

SELECTOR

   Reset Local Page Counter

SELAGAIN

 CHECK IF ONLY A <CR>

WRONGSEL

YOUR SELECTION IS NOT IN THE 0 - #SELIMIT  RANGE  Please try again

AANVANG

Copyright CourseWare Technologies Inc., 1985-88


Lesson - 5


 UNIX EDITORS 

MENU

UNIX Editors|Topics to Learn|5-0|12,45
    #                  Topic
   ---                -------
    1   -   What is an Editor?
    2   -   The ed Line Editor
    3   -   Using the vi Screen Editor
    4   -   Cursor Movement Commands in vi
    5   -   Basic Local Editing Commands
    6   -   Basic Global Editing Commands
    7   -   Customizing Your Editing Environment
    8   -   Other Editors: emacs and sed
    9   -   Lesson Review
    0   -   Return to the Main Menu

TOPIC1

What is an Editor

P1

UNIX Editors|What is an Editor?|5-1.1|10,45
     An editor is a utility program that enables 
you to create or modify text files.


     UNIX systems generally come with several 
different types of editors including:

         *  line editors:  ed, ex, edit
         *  screen editors:  vi, emacs
         *  stream editors:  sed

P2

UNIX Editors|What is an Editor?|5-1.2|14,56
     Line editors display only one line of text at a
time.  Lines can be printed, changed, deleted, or 
copied to a different place; new lines can be inserted.


     Line editors are usually interactive in that you
can see what your file looks like before making changes.  
You can make many modifications before making your 
changes permanent.


     By today's standards, line editors are considered 
rather primitive.  Why look at only a line at a time 
when you could see an entire screen? 

P3

UNIX Editors|What is an Editor?|5-1.3|6,54
     Screen editors display a screenful of text.
They are visually oriented, with a cursor to mark
your position in the file.  You can browse through
your file and use the cursor to indicate where to 
make a change.  You can select which portion of the 
file you want to have displayed on the screen.
??? scroll thru file to illustrate visual aspect L: 5uu_scroll
L: 5uu_scroll

P4

UNIX Editors|What is an Editor?|5-1.4|5,54
     Stream editors are not interactive.  You do not
view your file and you cannot move backwards in the
file.  Stream editors are usually not used to make
permanent changes to a file, but as a filter for some
other process.

P5

UNIX Editors|What is an Editor?|5-1.5|9,54
     When you use a line or a screen editor, a copy 
of the file you request is placed in a temporary work 
space called a buffer.


     Using a buffer is like working on scratch paper.  
You can scratch out words, rewrite sentences, or move 
paragraphs.  When you are satisfied with your draft, 
you can make a good copy.

P6

UNIX Editors|What is an Editor?|5-1.6|3,54
     When you want to save your work, you can write 
your buffer copy to the original filename on the disk.  
Then you can quit the editor.

P7

??? need setenv term here too
UNIX Editors|What is an Editor?|5-1.7|12,58
     In order for an editor to work properly, the operating
system must be told what type of terminal you are using.
You provide this information by setting a variable called
TERM.  You may be asked to do this after you enter your
password; if not, the syntax for setting the TERM variable is:

TERM=your terminal type

Some terminal types include: tvi925, wy50, adm3a, vt100, v55,
h1500, ansi, or h19.  If you are not sure what type you are
using, look for a nameplate on your terminal or ask your
system administrator.

P8

UNIX Editors|What is an Editor?|5-1.8|5,52
     After you set the TERM variable, you must make 
the new value available to the system.  This is done 
by entering the following line:

export TERM

TOPIC2

The ed Line Editor

P9

UNIX Editors|The Line Editor|5-2.1|15,54
     The most commonly available editor on most 
UNIX systems is the line editor ed.


     ed may be used with practically any terminal,
since it uses only basic facilities.


     ed may be used for:

       *  creating files,
       *  making changes on the current line,
       *  making changes affecting multiple lines,
       *  context searching, and
       *  performing external commands.

P10

UNIX Editors|The Line Editor|5-2.2|5,54
     For every operation you want to perform, you 
must specify what line or lines on which to perform it.


     Lines can be addressed by line number.

P11

UNIX Editors|The Line Editor|5-2.3|4,54
     We won't go into any more detail about ed 
since there are more convenient editors available.  
If you want to try ed, see "A Tutorial Introduction 
to the UNIX Text Editor" in your system documentation.
If you want to try ed, see the Appendix called
"Ed" in your system documentation.

TOPIC3

Using the vi Screen Editor

P12

UNIX Editors|Using the "vi" Editor|5-3.1|16,60
     Some features of the vi editor:

    *  visually oriented; uses your terminal screen as
       a window to view portions of your file;

    *  has a uniform and powerful method for changing, 
       deleting, moving and copying text;

    *  has an easy method for undoing the last editing 
       change, and another for repeating the last change;

    *  has systematic methods for making local and global 
       substitutions; and

    *  has methods for abbreviating text and customizing the 
       editing environment to the preferences of the user.

P13

UNIX Editors|Using the "vi" Editor|5-3.2|10,54
     Unlike some word processors, the vi editor has 
three modes of operation:

   *  moving the cursor around in a file to locate
      text for modification

   *  inserting text locally (where the cursor is 
      positioned)

   *  making changes that affect the entire file

P14

UNIX Editors|Using the "vi" Editor|5-3.3|13,54
     As you were shown in our Getting Started in UNIX
tutorial, you begin a vi session with the command:

vi  filename


     You can exit a vi session with one of the following:

       :wq  - to write the new version to the disk and quit

        :q  - to quit if you saved your changes already

       :q!  - to quit without saving changes

P15

UNIX Editors|Using the "vi" Editor|5-3.4|11,54
The general strategy for editing with vi is:

1.  Determine the portion of the file to be modified.

2.  Move the cursor to the point of revision using
     cursor movement commands.

3.  Determine the type of change to be made.

4.  Issue the appropriate text insertion commands
     to make the change.

TOPIC4

Cursor Movemement Commands

P16

UNIX Editors|Cursor Movement Commands|5-4.1|7,54
     You can move the cursor by certain units of measure 
that include:

                   *  characters, 
                   *  words, 
                   *  lines, or 
                   *  screens

P17

UNIX Editors|Cursor Movement Commands|5-4.2|4,54
     A character can be a letter, number, punctuation 
mark, or symbol.  One character takes up one space on 
the screen.  The following commands may be used to move 
the cursor one character at a time:

P18

UNIX Editors|Cursor Movement Commands|5-4.3|11,54
    Commands to Move the Cursor by Characters
    -----------------------------------------

   l  and SPACEBAR:  move the cursor 1 character to the
                     right;  5l  (that's "el" not "one")
                     will move 5 characters to the right.


  h  and BACKSPACE:  move the cursor 1 character to the
                     left;  3h  will move 3 characters 
                     to the left.
??? L: 5uu_char practice moving left and right
L: 5uu_char

P19

UNIX Editors|Cursor Movement Commands|5-4.4|6,56
     A word is a group of letters, numbers or 
underscores surrounded by anything but letters, 
numbers or underscores.

Thus, "cat", "c5a_t" and "_c_a_7_" are all con-
sidered to be words for cursor movement commands.

P20

UNIX Editors|Cursor Movement Commands|5-4.5|10,56
    Commands to Move the Cursor by Words
    ------------------------------------

   w   (for word-at-a-time) moves the cursor to the first 
       character of the next word.   3w  moves 3 words to 
       the right.

   b   (for backwards) moves the cursor to the first 
       character of the previous word.   4b  moves back 4 
       words.
??? L: 5uu_word practice moving by words
L: 5uu_word

P21

UNIX Editors|Cursor Movement Commands|5-4.6|5,54
     A line is a group of words up to and including a 
carriage return.  A line is usually between 0 and 80 
characters, but may be hundreds of characters long if 
you forget to type a carriage return when entering text.  
A sentence may be several lines long.

P22

UNIX Editors|Cursor Movement Commands|5-4.7|14,57
Commands to Move the Cursor by Lines
------------------------------------

   j   moves the cursor 1 line down on the screen and 
       keeps the cursor in the same column.   2j  will
       move down 2 lines.

  <CR> moves the cursor 1 line down on the screen but 
       puts the cursor on the beginning of the next line.  
       Again, a number may precede the command.

   k   moves the cursor 1 line up on the screen and keeps
       the cursor in the same column.   Again, a number
       may precede the command.

P23

UNIX Editors|Cursor Movement Commands|5-4.8|10,55
More Commands to Move the Cursor by Lines
-----------------------------------------

   ^   moves the cursor to the beginning of the current 
       line.

   $   moves the cursor to the end of the current line.

  #G   (for Go to) moves the cursor to the beginning 
       of line  # .
??? L: 5uu_line practice moving by lines
L: 5uu_line

P24

UNIX Editors|Cursor Movement Commands|5-4.9|16,54
     A screen is all the lines in view at one time
on your terminal screen (usually 23 lines).


    Commands to Move the Cursor by Screens
    --------------------------------------

  <CONTROL-f> (for forward) scrolls the text forward a
              little less than 1 screenful (about 21 lines)

  <CONTROL-b> (for backward) scrolls the text back a
              little less than 1 screenful (about 21 lines)

   G  (for Go to) without a line number, will place the 
              cursor on the first character of the last line 
              in the file.
??? L: 5uu_screen practice moving by screens
L: 5uu_screen

P25

UNIX Editors|Cursor Movement Commands|5-4.10|9,54
     When you invoke vi, you are automatically placed 
in cursor movement mode.  The cursor will be placed
on the first character of the first word in the file.


     To move the cursor, you must indicate the number 
of units of measure (1 is assumed if you don't).  As 
soon as the command is entered, the movement is done;  
cursor movement commands are not echoed on the screen.

TOPIC5

Basic Local Editing Commands

P26

UNIX Editors|Local Editing Commands|5-5.1|2,54
     A change made in only one place, usually at the
current cursor position, is called a local revision.

P27

UNIX Editors|Local Editing Commands|5-5.2|12,54
     Once you have used the cursor movement commands 
to locate a portion of text to be altered, there are 
several local editing tasks you can perform:

               *  adding text
               *  deleting text
               *  changing text
               *  repeating the last change
               *  reversing the last change
               *  joining lines
               *  copying lines
               *  moving lines

P28

UNIX Editors|Local Editing Commands|5-5.3|15,58
     To make an editing change, you must tell the editor
what type of change is to be made by using an operator.


    Operators to Delete Text
    ------------------------
     x   removes the character at the current cursor position

  dunit  removes the unit of measure it precedes
         (character, word, lines, etc.)

    dd   removes the current line of text

     D   removes text from the current cursor position to
         the end of the line

P29

UNIX Editors|Local Editing Commands|5-5.4|7,54
     As with cursor movement commands, you can add
a number to the operators:

     4x   removes 4 characters, including the current one

  3dunit  removes 3 units of measure, including the 
          current one

    5dd   removes 5 lines of text, including the current one
??? L: 5uu_delete practice d, x, dd, D
L: 5uu_delete

P30

UNIX Editors|Local Editing Commands|5-5.5|8,54
     Adding text is a 3-step operation:

     *  specify the operator
     *  type in the new text
     *  indicate the end of the text-insertion with <ESC>


     You CANNOT add a count to text insertion operators.

P31

UNIX Editors|Local Editing Commands|5-5.6|15,54
    Text Insertion Commands
    -----------------------
  a   appends new text AFTER the current cursor position

  i   inserts new text BEFORE the current cursor position

  A   Appends new text at the END of the current line

  I   Inserts new text at the BEGINNING of the current line

  o   opens a new line AFTER the current line

  O   Opens a new line BEFORE the current line

 ALL text insertion commands must be ended with <ESC>  
??? L: 5uu_insert practice aiAIoO
L: 5uu_insert

P32

UNIX Editors|Local Editing Commands|5-5.7|6,54
    You can have the editor repeat your last editing
change with the dot  .  operator.  If for example
you want to add the same word in several places, you 
can add it in the first location with the  a  
operator, move to the next location and simply type
a  .  to have it added in the new position.
??? L: 5uu_dot practice dot
L: 5uu_dot

P33

UNIX Editors|Local Editing Commands|5-5.8|10,54
    You can have the editor "undo" your last editing
change with the  u  operator.  If for example you
added a sentence that you don't like and want to
remove it, you can simply undo the change by typing
a  u .  The text addition will be removed.  If you
type a  u  again, the text will be added again.


    However, the editor will only remember the
LAST change you made.
??? L: 5uu_undo practice undo
L: 5uu_undo

P34

UNIX Editors|Local Editing Commands|5-5.9|17,58
    Text Replacing and Changing Commands
    ------------------------------------
  r   replaces the character at the current cursor position
      with the next character entered

  ~   changes the character at the current cursor position
      from uppercase to lowercase or vice versa

  ccount unit  changes (replaces) count number of units of
      measure (characters, words, lines, etc.) to the new 
      text you enter.  Because new text is added, this 
      operation must be ended with <ESC>.

  C   Changes all text from the current cursor position
      to the end of the current line to the text that you 
      enter.  Because new text is added, this operation 
      must be ended with <ESC>.
??? L: 5uu_change practice r ~ c C
L: 5uu_change

P35

UNIX Editors|Local Editing Commands|5-5.10|5,54
     You can join two lines of text with the  J 
operator.  Your cursor may be anywhere on the first 
line.  You can add a count to specify how many lines 
below the current line are to be joined.
??? L: 5uu_join practice J
L: 5uu_join

P36

UNIX Editors|Local Editing Commands|5-5.11|13,54
     To copy lines, you can use the  Y  (for Yank) 
operator.  Move the cursor to the place you want the 
new text to go and issue p or P command to place
the new text.


  Commands for Copying Lines
  --------------------------
  nY  will yank n lines to a temporary storage area

   p  will put yanked lines AFTER the current line

   P  will Put yanked lines BEFORE the current line
??? L: 5uu_yank practice Y, p, P
L: 5uu_yank

TOPIC6

Basic Global Editing Commands

P37

UNIX Editors|Global Editing Commands|5-6.1|11,54
     A global revision is one that affects every 
instance of specified text throughout the file, 
regardless of the current cursor position.  Changing 
every occurrence of a misspelled word, for instance, 
or substituting the full name for an abbreviation 
or acronym.


     Global revisions are based on the ability of 
the editor to search for certain patterns or strings 
of characters in a file.

P38

UNIX Editors|Global Editing Commands|5-6.2|15,54
 Search Commands 
 ---------------
/search string/  will search FORWARD in the file
for the first occurrence of search string.

?search string?  will search BACKWARD in the file
for the first occurrence of search string.

/^search string/  will search FORWARD in the file
for the first occurrence of search string at the 
BEGINNING of a line.

/search string$/  will search FORWARD in the file
for the first occurrence of search string at the 
END of a line.

P39

UNIX Editors|Global Editing Commands|5-6.3|7,54
 Search and Delete Command
 -------------------------

:g/search string/d<CR> 

will search through the file for search string and 
delete every line that contains it.

P40

UNIX Editors|Global Editing Commands|5-6.4|18,54
 Search and Change Commands
 --------------------------
:g/search string/s/existing text/new text/g<CR> 

will search through the file for the search string and
every time it finds a line containing it, it will replace 
the existing text with the new text.  (The existing
text does not have to be included in the search string.)


     The g at the beginning of the line means to globally 
go through the file vertically, looking for the first 
occurrence of the string on a line. 


     The g at the end of the line means to globally go
through the located lines horizontally, looking for 
every occurrence of the string on each line. 

P41

UNIX Editors|Global Editing Commands|5-6.5|15,54
     Commands that begin with a colon ( : ) are  
another category of editing commands.  In addition to
performing global searches and changes, these commands
give you access to another set of operators which allow 
you to: 

  *  include other files as part of your text, 
  *  include the output of another command in your text,
  *  to customize your editing environment, or 
  *  to escape from the editor to execute a command at 
     the shell level.  


     When you use a  :  prefix, you must terminate the 
command with a <CR>.

P42

UNIX Editors|Global Editing Commands|5-6.6|18,54
     While in vi, you can insert the contents of
another file after the current line with:

:r  file name<CR>


     You can edit another file without exiting from
the current vi session with:

:e  file name<CR>

Quitting will return you to the Shell.


     You can write the contents of your current file to
a new file name (leaving the original file as is) with:

:w  file name<CR>

P43

UNIX Editors|Global Editing Commands|5-6.7|16,54
      You can escape your current vi session to
execute another command at the Shell level and then
return to vi with:

:!unix_command<CR>


For example,           :!date<CR>

will escape to the Shell, execute the date command 
and return you to the same place in the editor.

:!vi file name <CR>

will allow you to escape to edit file name and be 
returned to the same place when you quit.

P44

UNIX Editors|Global Editing Commands|5-6.8|11,54
      You include the output of another UNIX command
in your current file with:

:r !unix_command<CR>

For example,

:r !date<CR>

will insert the output of the date command after the
current line in your file.

TOPIC7

Customizing Your Editing Environment

P45

UNIX Editors|Customizing Your Editing Environment|5-7.1|15,54
     You can use the  :set  command to set up
certain screen related editor options.  Some of 
these options include:

:set number (nu) - will cause the editor to display 
a line number for each line of text.

:set list - makes all control characters visible and
marks the end of each line with  $ .

:set wrapmargin=n (wm) - automatically inserts a carriage 
return when the cursor is within n characters of the 
right edge of the screen.

:set all - will show how all of your options are set.

P46

UNIX Editors|Customizing Your Editing Environment|5-7.2|9,54
     You can turn on the above options by entering:

 :setoption<CR>


     You can disable an option by preceding the
name with the prefix no, i.e.

 :set nooption<CR>

TOPIC8

Other Editors: emacs and sed

P47

UNIX Editors|Other Editors|5-8.1|12,54
     Another full-screen editor available under 
UNIX is emacs.


     emacs features include: 

*  multiple windows,
*  the ability to extend and customize with a built-in 
    programming language and macros, and
*  the ability to be used as a session manager for
    UNIX through a powerful interface with the UNIX 
    shells sh and csh.

P48

UNIX Editors|Other Editors|5-8.2|14,54
     sed is a noninteractive editor.  It works  on your
file from beginning to end.  Once it is invoked, it allows 
you no choice of commands, instead it passes data through 
a set of transformations, like through a pipe.  


     sed does not use a buffer, so it can be used for
files that are too big for other editors.  


     It is good for transient changes because it applies 
editing commands to a file and sends the results to the 
standard output rather than making permanent changes.

P49

UNIX Editors|Other Editors|5-8.3|6,54
     Because sed understands ed line editing commands,
you can make a file with the editing commands and have
sed read them and apply them to your file.  The
syntax is:

sed  -f  command file  old file  >  newfile

TOPIC9

Lesson Review