[comp.editors] vi tutorial II

UH2@psuvm.psu.edu (Lee Sailer) (07/18/90)

And just in case you didn't like mine, here's the one Robert Drabek
sent me.  Don't blame for posting it, OK?
Received: from PSUVM by PSUVM.PSU.EDU (Mailer R2.07) with BSMTP id 8510; Mon,
 16 Jul 90 19:09:10 EDT
Received: from megaron.cs.arizona.edu by PSUVM.PSU.EDU (IBM VM SMTP R1.2.2MX)
 with TCP; Mon, 16 Jul 90 19:09:06 EDT
Date: Mon, 16 Jul 90 16:06:45 -0700
From: "Robert J. Drabek" <robert@cs.arizona.edu>
Message-Id: <9007162306.AA05499@megaron.cs.arizona.edu>
Received: by megaron.cs.arizona.edu (5.61/15)
        id AA05499; Mon, 16 Jul 90 16:06:45 -0700
To: UH2@psuvm.psu.edu
Subject: Re: vi tutorial
In-Reply-To: your article <90196.154016UH2@psuvm.psu.edu>
News-Path: arizona!noao!asuvax!cs.utexas.edu!wuarchive!psuvax1!psuvm!uh2

I don't have anything which you can print.  There are a lot of vi
reference sheets, and I am certain people will send you those.

Instead, I have included an interactive tutor (~600 lines).  What you do
is to have each student simply copy the file into their area and then
they run vi on that file.  As they they read the file it tells them to
type ^D, ^U, etc.  and explains what is happening.  My experience is
that it takes between 1 and 4 hours to complete, depending upon the
student, and does a pretty decent job.

This was adapted from a tutorial for Emacs.

- - - - - - - - - - - - - - - - cut here  - - - - - - - - - - - - - - - - - -
   You are looking at a VI tutorial.  Comments on this document may be
referred to your instructor in the Computer Science Department.

   Vi operates in one of two modes : command mode and insert mode.  You
are currently in command mode and will enter insert mode only when you
need to add new text to a file.

   Several commands are of the CONTROL type; these will be indicated as
an up-carat (^) followed by a letter.  So "CONTROL D" will now be
written as ^D.  You would execute this command by holding the <CONTROL>
key down with your left little finger and hitting the letter D (you
DON'T have to hold <SHIFT>) with your left index finger.

   Try it now!  The cursor moved down into the file.  Most vi commands
are mnemonic like this.  Type ^U now and see it move up.

   This tutorial attempts to help you to learn by doing.  The characters
">>" at the left margin of your screen indicate directions for you to
try using a command.  For instance:

>>  Now type ^D to move to the next screen. (go ahead, do it by
    depressing the control key and the D together).

   From now on, you'll be expected to type ^D whenever you finish
reading the current screen.

   Note that there is an overlap when going from screen to screen; this
provides some continuity when moving through the file.

   The first thing that you need to know is how to move around from
place to place in the file.  You already know how to move forward a
screen, with ^D.  To move backwards a screen, type ^U (depress the
CONTROL key and type U).

>>  Try typing ^U and then ^D to move back and forth a few times.


SUMMARY
-------

The following commands are useful for viewing screenfuls:

     ^D      Move forward one screenful
     ^U      Move backward one screenful
     ^L      Clear screen and redisplay everything.

>> Find the cursor and remember what text is near it.
   Then type a ^L.
   Find the cursor again and see what text is near it now.


BASIC CURSOR CONTROL
--------------------

   Getting from screenful to screenful is useful, but how do you
reposition yourself within a given screen to a specific place?  There
are several ways you can do this.  One way (not the best, but the most
basic) is to use the commands left, down, up and right.  As you can
imagine, these commands (which are given to vi as h, j, k, and l
respectively) move the cursor from where it currently is to a new place
in the given direction.  Here, in a more graphical form are the
commands:

                           Previous line, k
                                  :
                                  :
      Backward, h .... Current cursor position .... Forward, l
                                  :
                                  :
                             Next line, j

   You'll probably find it easy to think of these by placing the fingers
of your right hand on these four keys.  The h is the furthest to the
left, the l is the furthest to the right.  The j and k are for down and
up, respectively.  These are the basic cursor positioning commands and
you'll be using them ALL the time so it would be of great benefit if you
learn them now.

>> Do a few j's or k's to bring the cursor down to this line.
   Move into the line with l's and then up with several k's.
   See what k does when the cursor is in the middle of the line.

>> Try to use h to get to the beginning of a line.  Do a few more h's.
   Then do l's back to the end of the line.  Can you go beyond?

   The bell sound you heard or the screen flash you saw is vi's way of
telling you something cannot be done.

   When you go off the top or bottom of the screen, the text beyond the
edge is shifted onto the screen so that your instructions can be carried
out while keeping the cursor on the screen.

>> Try to move the cursor off the bottom of the screen with j and
   see what happens.

   If moving by characters is too slow, you can move by words.  The  w
moves forward a word and  b  moves back a word.

   The  e  command is similar to the  w  except that it moves to
the end of the word.

>> Type a few e's, w's, and b's.  Intersperse them with l's and h's.

   Note that e, w, and b will move to the next line.

   0 (zero) and $ (dollar) move to the beginning or end of a line.

>> Try a couple of 0's, and then a couple of $'s.

   The <SPACE> bar also moves across the screen just like the l key.

   The <RETURN> key goes to the first non-white character on the
next line.

   The minus sign (-) goes to the first non-white character on the
previous line.

>> Put the cursor in the middle of a line and type the <SPACE> bar
   a few times.

>> Try the <RETURN>.

>> Try the -.

   Another simple cursor-motion command is  G  (upper case) which moves
to the end of the file.  The command  1G  should move you to the
beginning of the file.  More on this later.  If you should try either
of these commands now, you'll have to get back here by yourself (you
can get back by typing `` ).

   To move to the top of the visible screen try upper-case  H  (for Home).
Upper-case  M  puts you in the middle of the screen.  Upper-case  L  puts
you on the last line of the screen.

>> Try all of H, M, and L now.

   Here is a summary of simple moving operations including the word
commands:

     l        Move forward a character
     <SPACE>  Move forward a character
     h        Move backward a character

     w        Move forward a word
     b        Move backward a word

     j        Move to next line
     k        Move to previous line

     0        Move to beginning of line
     $        Move to end of line

     <RETURN> Move to the beginning of the next line
     -        Move to the beginning of the previous line

     H        Move to the top of the screen
     M        Move to the middle of the screen
     L        Move to the bottom of the screen

     1G       Go to beginning of file
     G        Go to end of file

>> Try all of these commands now a few times for practice.
   Since the last two will take you away from this screen,
   you can come back here with ^D's and ^U's.

   Like most commands in vi, these commands can be given positive
numeric arguments which cause them to be executed repeatedly.  The
way you give a command a repeat count is by typing the number before
typing the command.  The digits will not show, but they are there
anyway.

   For instance, 8h moves back eight characters.

>> Try giving a suitable argument to j or k to come as close
   as you can to this line in one jump.

   This also applies to the screen moving commands, ^D and ^U.  When
given an argument, they scroll the screen up or down by that many
lines and, in fact, ^D and ^U will now only scroll by that new number
of lines.

>> Try typing 3^D now.
   Type ^D (without the 3) and note that it still only scrolled 3
   lines.

>> Try typing 12^D to get back the default amount of scroll.

   ^D and ^U are the only commands which remember their last argument.

   If the go-to commands are preceded by a number, they go to that
line number in the file.  Very useful when the compiler says there
is an error on line X!

>> Try 195G.


QUITTING FROM PARTIAL COMMANDS
------------------------------

   The character in vi used to quit out of partially completed commands
is <ESC>.  For example, you can use <ESC> to discard a numeric argument
or the beginning of a command that you don't want to finish.

>> Type 100 to make a numeric arg of 100, then type <ESC>.  Did
   the bell sound?  Now type k.  How many lines does it move?


INSERTING and DELETING
----------------------

   If you want to enter text, you must enter insert mode.  This may be
done in a number of ways, depending upon where you need to enter the new
text.

   To enter a new line immediately under the current line, type the
letter  o.  This will "open" an empty line and anything you now type will
appear on that line.  When you are finished entering this new data, you
will want to get back to command mode.  This is done by hitting the
<ESC> key.

>> Type the lower-case  o  now.  And then enter a couple of cute words.
   Then hit the <ESC> key.  You should now be able to move around in
   the file as before.

   The upper-case  O  is similar, except that a new line is opened above
the current one.

   Note that the movement keys h, j, k, and l are not movement keys when
in insert mode; they are just letters then.

>> Open a new line with either O or o.  Add a cute word or two and then
   type ^U.  Note that the bell rings.  You can not use control
   characters when in insert mode.
   Type <RETURN> to insert a line separator.  You can enter as much
   information as you need.  Type <ESC> to get back to command mode.

   You can delete the last character you typed while in insert mode by
typing a <BACKSPACE>.

>> Open a new line, type a few characters, and then delete them by typing
   <BACKSPACE> a few times.

   Vi can only read or write lines, to or from a file, which are less
than or equal to 255 characters.

   Lines which are longer than the width of your terminal are usually
wrapped around.  "Usually" because some terminals operate differently
than others.  It is generally a good idea never to try to go beyond the
end of the screen.  More on this later.

   If you want to create a blank line in between two lines, move to the
first of the two lines and type o.

>>  Try moving to a line and typing  o  now.

   You've now learned the most basic way of entering something in vi.

DELETION
--------

   You can delete characters, words, or lines as well.  Here is a
summary of the delete operations:

     x             delete the character under the cursor

     dw            kill the next word after the cursor
     db            kill the word immediately before the cursor

     dd            kill the entire line

     D             kill from the cursor position to end of line

>> Try all five of these commands until you are familiar with them.

>> Move the cursor to this line and delete it with dd.

   Note that dd deletes the line itself, and makes all the other lines
move up.  If you give dd a repeat count, it deletes that many lines.

   The text that has just disappeared is saved so that you can retrieve
it.  To retrieve the last killed line and put it after where the cursor
currently is, type p.  Use upper case P to put it above the current
line.

>> Try it; type  p  to put back the line you just deleted.

   When you put a line back it is still in the save buffer.  This means
you can put several copies into different locations.

   You do not have to delete lines to put them into the save buffer.
Typing yy will yank a line without deleting it.  This is a handy way to
duplicate some lines.

>> Put the cursor anywhere on this line.
   Type   3yy
   Then type upper case P.  Make certain you understand what occurred.

>> Put the cursor anywhere on this line.
   Type   3yy
   Then type lower case p.  Make certain you understand what occurred.

   What if you delete a line and would like to put it back, but delete
another line first?  Sorry, but only the last one is saved.

>> Kill a line, move around, kill another line.
   Then do  p  to get back the second killed line.


MORE INSERTION
--------------

   The open-a-line commands only start fresh lines.  What if you want
to add new material to the middle of an existing line? There are two
commands for this:

     i             enter insert mode immediately in front of the cursor
     a             enter insert mode immediately after the cursor

>> Put the cursor in the middle of a word on this line.
   Type the i command.  Now type a word.  You should see it appearing
   on the screen.  Type <ESC> to get back to command mode.

>> Put the cursor in the middle of a word on this line.
   Type the a command.  Now type a word.  You should see it appearing
   on the screen.  Type <ESC> to get back to command mode.

   Study how the two commands differ.  Try i and a at the beginning and
the end of a line.

   There are three "change" commands - change word, change entire line,
and change-to-the-end-of-the-line.

   Change word is  cw.  This changes from the cursor to the end of the
word.

   Change entire line is  cc.

   The change-to-the-end-of-the-line command is the upper-case C.  This
deletes the text from the cursor to the end of the line and then does
an append (the a command).

>> Change the word GORGEOUS to UGLY on this line.  Remember to use
   <ESC> to get back to command mode.

>> Put the cursor in the middle of this line.  Type  cc  and add
   some new material and hit <ESC> to get back to command mode.

>> Put the cursor in the middle of this line.  Type  C  and add
   some new material and hit <ESC> to get back to command mode.

  The upper-case  A  appends new characters to the end of the line.

  The  r  command is to replace a single character without having to
first delete it and then insert a new character.

>> Try positioning the cursor on the X of this line.
   Then type  rz  and note that the X should have changed to a little z.

  Similar is the  s  command.  This substitutes new characters for
the character under the cursor.

>> Try putting the cursor on the X on this line.
   Then type some new characters.  It's like typing first an  x  and
   then an  i.


UNDOING CHANGES
---------------

   Suppose you have just deleted a word and realized it was the wrong
word.  You may undo the last change (insertion or deletion) by simply
typing the  u  command.

>> Put the cursor in the middle of this line and insert some extra
   text and hit <ESC> to get back to command mode.
   Now hit the  u  key.  Note what happened.

   Undoes can themselves be undone.  Hit the  u  key again to see.


REPEATING CHANGES
-----------------

Any change may be repeated by hitting the period (.).

>> Insert a few new characters in the middle of this line and then
   move somewhere else on the screen, hit the period key and note
   what happened.  You will this an often-useful command.


JOINING TWO LINES TOGETHER
--------------------------

   The upper-case J is used to join the current line and the one below
into a single line.

>> Put the cursor on this line
   and hit the upper-case J.
   Type u to remember how undo works.


FILES
-----

   In order to make the text you edit permanent, you must put it in a
file.  Otherwise, it will go away when you leave vi.  While you are
editing a file in vi, your changes are actually being made to a private
"scratch" copy of the file.  However, the changes still don't become
permanent until you "save" the file.  This is so you can have control to
avoid leaving a half-changed file around when you don't want to.

   Type ^G and then look near the bottom of the screen.  You will see a
line that looks something like this:

       vi.tut: [Modified] line 405 of 584

   Note that vi indicates the name of the file you are currently
editing.  This is the name of your own temporary copy of the text of the
vi tutorial; the file you are now editing.

   If you would like to exit vi and save all your changes, the easiest
thing to do is type ZZ (two upper-case Z's).  Don't do it quite yet.

   If you would like to exit vi and abandon all your work, type the
:q! command (colon, q, and exclamation mark).  Be careful doing this.

>> Type :q! and when you see the  :q!  message on the bottom of
   the screen hit the <DELETE> key.  Then nothing will happen.

   If you had hit the <RETURN>, vi would have left your file unchanged
and you would have your system prompt back.  This may be necessary if
you accidently make a lot of bad changes and wanted to forget it all.

   The other commands for saving and reading files are unlike the other
commands you have learned in that they consist of two characters.  They
all start with the colon character (:).  There is a small series of
commands that start with a colon; they all end with a <RETURN>

:w<RETURN>          Write the changes to the current file but don't exit
:w filename<RETURN> Write the changes to the named file but don't exit

:r filename<RETURN> Read the named file and place its contents right
                    after the current line

:e filename<RETURN> Edit a different file

   The first command is useful when you are making many changes and
would not like to loose all of your work if the computer crashed.
Use this command every 5 minutes or so to prevent catastrophes.

   The second is useful when you would like to save your changes in
a file different than the original.  (Not that often used.)

   The next one is good for including information from one file into
the current one.

   The last one is how to go to a different file and work on it
without first having to leave the editor.  Make certain that you
save the current file first with  :w  or vi won't allow you to
switch files.

   To make a new file, just edit it "as if" it already existed.  Then
start typing in the text.  When you ask to "save" the file, vi will
really create the file with the text that you have inserted.  From then
on, you can consider yourself to be editing an already existing file.

   It is not easy for you to try out editing a file and continue with
the tutorial.  But you can always come back into the tutorial by
starting it over and skipping forward.  So, when you feel ready, you
should try editing a file named "foo", putting some text in it, and
saving it; then exit from vi and look at the file to be sure that it
worked.


TEXT SEARCHING
--------------

   A very common thing to do is to look for a word in a file.  This
is easily done in vi by first typing the slash (/).  When you do
you should see a highlighted slash on the very bottom of the screen.
Now type in the string you would like to find and hit return.

>> Type an upper-case H to put the cursor at the top of the screen
   and then type  /case  and hit <RETURN>.  Where is the cursor now?

   The  n  will find the next occurrence of the string last searched
for.  Type the lower-case n one more time.

   To search backwards use the  ?  command.

   The  n  remembers which way you were last searching to help you
repeat the search.

   If you were searching forward but wanted to search backward without
retyping the search string, simply typing  ?  and <RETURN> will do
the trick.  Also, visa-versa.

   You can do a simple "find a character only on the current line"
by typing  f  and a single character.  The upper-case F does the simple
find in the reverse direction.

   To repeat a f or F command, use the semicolon (;).

>> Try the /, ?, f, F, and ; commands until you are thoroughly
   familiar with them.


SEARCH AND REPLACE
------------------

   It is often necessary that we need to find a certain character sequence
and replace it with a different one.  For example, replacing all occur-
rences of Pat with Chris when romance has take a diversion in another
direction.

   This is a little cumbersome, but that's life.  The command is another
of the "colon" commands.  (Maybe there's a joke in there?)  Type

:1,$s/Pat/Chris/g

This is read as "for lines 1 to the end of the file substitute the
characters Pat for the characters Chris everywhere on the line."
You can restrict the substitution to particular line; for example,

:200,312s/Pat/Dale/g

   You will have to try it a couple of times.  Don't worry about messing
up this file; you can alway "quit" the editor or make a new copy the
way you got the first one.

   By the way, the colon commands are really commands to another editor
(vi is actually built on top of the ex editor) to do the work.  You
should learn more about ex very soon.


MARKING YOUR LOCATION AND RETURNING TO IT
-----------------------------------------

   If you move from one location to another in the file and would like
to get back without using ^D and ^U the quote-quote (two single quotes)
will do it for you.  This is very useful when you have used G or / to
go somewhere and found you went to the wrong somewhere.

>> You will type 1G and then two quotes immediately after that.  This
   should put you right back here.

   Often we are working on a section of code and would like to explore
other sections for a while and then return to the section of interest.
One way would be to use ^G to find the line number, write it down and
then finally use #G to get back.  But there is an easier way!

   The lower-case m marks the spot.  The single quote (') returns you
there.  But its a little more complicated.  You have to name the spot
with a letter.  Let's try an example.

>> Type  ma  (note the a) on your line of interest.  Move around the
   file and then type  'a  and you should find yourself back where you
   wanted to be.


CONCLUSION
----------

   Remember, to exit use ZZ or q.

   This tutorial is meant to be understandable to all new users, so if
you found something unclear, don't sit and blame yourself - complain!

   You'll probably find that if you use vi for a few days you won't
be able to give it up.  Initially it may give you trouble.  But remember
that this is the case with any editor, especially one that can do many,
many things.

   A copy of the manual (not the manpage) is very useful to have nearby
when first working with vi.  There is a copy in the Grad Lab.  I don't
have any idea how you can get a copy for yourself.

   Learning vi should not stop here.  There are many things you can
do with it.  Find an experienced vi hacker to show you some tricks
after you've gotten used to the basics.  Learn about something called
the .exrc file and read through the afore-mentioned manual.

   This version of vi is slightly different than the version you may
have used on IVAX, but only slightly.  Learning the differences will
be about a 5-minute job.  That was really a slimmed-down version and
is small enough to easily fit on a MS-DOS micro computer.  If you
are interested in the MS-DOS version, see Robert Drabek in the Computer
Science Department.


Acknowledgements
----------------

   This is a modified version of the "JOVE Tutorial" by Jonathan Payne
(19 January 86).  That document was in turn a modified version of the
tutorial "Teach-Emacs" from MIT as modified by Steve Zimmerman at
CCA-UNIX (31 October 85).

   This document was last updated on 7-24-87 by Robert Drabek.
- - - - - - - - - - - - - - - - cut here  - - - - - - - - - - - - - - - - - -