#
cat << '%'
##nroff
.TI VI_PRACTICE
Practice Session with the Visual Editor "vi"
.ds CF		\" prevents shell from stumbling on the final page number
.sp
This will initiate a practice session with the "vi" editor.
You will be placed in the editor with a file containing instructions
on how to proceed.
.sp
##
'%'
cat > /tmp/hvi$$ << '%'
---------------------------------------------------------------
If you get into trouble: to exit from vi press ESC and type ZZ
---------------------------------------------------------------

First of all, we will practice moving around the screen.
Press the RETURN key to move down a line at a time.
Keep hitting RETURN until you arrive at the big X below.
Then use the arrow keys for directional movements.

                ______  Start at the x below and move right.
                |
                V

                X-----------------------x <-- You should be here.
                                        |       Now go down.
                                        |
                                        |
                                        |
                                        |
   This is the end. --> x               |
Now use CTRL-d to       |               |
scroll downwards.       |               |
                        |               |
                        |               |
                        |               |
                        |               |
                        |               |
You should be here. --> x---------------x <-- You should be here.
  Now go up.                                    Now move left.

Scrolling goes a few lines at a time.
If you want to scroll upwards, you could try the CTRL-u command.
CTRL-d goes down, whereas CTRL-u goes up.

Continue with CTRL-d until you're located around here.

To move a forward a word at a time, just type the letter w -- it
will not appear on the screen, but the cursor will jump to the
next word.  To go back a word at a time, try the b command.  There
is also the e command, which goes to the end of the current word.
Try moving around this paragraph with these three commands.

This is a block of text that is pretty boring.  Nonetheless,
there is an interesting word further down.  That word is Garbage.
Do a pattern search for this word.  To do this, type a slash (/),
then type the letters g a r b a g e (without spaces), and a RETURN.
Bla bla bla bla bla bla bla.  Bla bla bla bla.  Bla bla bla bla
bla bla bla bla bla.  Bla bla bla bla bla bla bla bla bla bla.
Bla bla bla bla bla bla bla.

I've had enough of this garbage.  <-- You should be on this line.

Now do another search for the same word.
To do this, type an "n", which will not appear on the screen.
You will be placed at the next instance of the word.

Another instance of the pattern "garbage".  <-- Here it is.

Now continue onwards by pressing RETURN a few times.

---------------------------------------------------------------
If you get into trouble: to exit from vi press ESC and type ZZ
---------------------------------------------------------------

Now that we've learned to move the cursor around the screen,
let's try several commands for changing the text.  The first is
the "a" command, which appends text-- everything you type will
be entered into your file, until you issue an ESC (this is a
button on the upper left corner of the keyboard).  Move down to
the line below the arrow, and add some text.  Don't forget to
end text input by pressing the ESC key.  After you're done,
move on to the lines below.

|
|
V


Good.  Remember, if you get into a weird state by pressing some
key you don't know about, you can always get out of the editor
and back to the shell by pressing ESC and typing ZZ (capital Z
two times in a row).  These characters will not appear on the
screen, but they will get you out of vi.

Now let's practice deleting characters with the x command.  The
lines below from Shakespeare have several extraneous characters;
delete them by moving to the extraneous character, and typing x,
as if you wanted to "x" them out on a typewriter.

Let me Nnot to the marBriage of trEue minds     <---
admitV impediRAments.

Good.  Now try deleting a line with the dd command.  Move down
to the line below, and type d twice in a row.  The d's will not
appear on the screen, but the line will disappear.

THiS liNe iS UggLy AnD wAnTs tO bE dELeTeD.     <---

Say you wanted to add a new line between two other lines.  In
the famous quote below, the fourth line is missing.  Just in case
you're not a John Donne fan, the fourth line is:

    And therefore never send to know for whom the bell tolls,

To open up a new line, move the cursor above where the new line
should go, type an o, which will move you onto the next line,
and then enter the text, ending with ESC (as with the a command).

No man is an island entire of itself;
Every man is a piece of the continent, a part of the main.
Any man's death dimishes me, because I am involved in mankind.
it tolls for thee.

With these four commands, a x dd and o, you can do anything you
want.  However, learning additional commands will make things
easier in the long run.  For instance, to split the following
line in half, you could go to the comma, type: a RETURN ESC, but
it would be easier to go to the space after the comma and type
r RETURN.  The r command replaces whatever is beneath the cursor
with whatever you type next.

This is an extremely long line, and needs to be split after the comma.

To join lines together again, go to the first line of the two
you wish to join, and issue the J command.  (It must be a capital
J, because a small j is reserved for down arrow.)

Two short lines
look better as one.

---------------------------------------------------------------
If you get into trouble: to exit from vi press ESC and type ZZ
---------------------------------------------------------------

One of the nicest features of vi is that it makes it possible to
combine movements and changes.  If you want to delete a single word,
for instance, you could move to the word and type x over and over
until the word is gone.  But it's easier to type dw -- which means
delete word.  Try this on the extra word in Shelley's line below:

I met a traveller traveller from an antique land

If you want to change a word, rather than deleting it, you could
type cw instead of dw -- cw stands for change word.  A dollar sign
will appear at the end of the word, and whatever you type, until
you press ESC, will replace the original word.  Replace the word
"enormous" with Shelley's original word, "vast":

Who said: Two enormous and trunkless legs of stone

Many commands take arguments indicating their scope.  It would be
painful to delete many lines unless this were the case.  Delete
the three lines below by using the 3dd command, meaning, perform
the dd command three times:

THiS liNe iS UggLy AnD wAnTs tO bE dELeTeD.             <---
THiS liNe iS aLSo UggLy AnD wAnTs tO bE dELeTeD.        <---
THiS liNe, tOo, iS UggLy AnD wAnTs tO bE dELeTeD.       <---

Good.  Now you're really blasting away those lines.  Don't forget
that the u command always undoes the last change.  Try typing u
now, and see those 3 lines come back.  Try u once more, and see
them disappear again.  Undo is its own inverse.

Oftentimes people who type quickly transpose characters.  To fix
this problem, move to the first character of the transposed pair,
and issue an xp command -- this stands for x out and put.  Mend
the word below, whose second and third characters are skewed:

        hpyerventilate

That's it for this session.  If you want to write your changes,
leave the editor with the ZZ command.  If you want to leave your
text the way it was before, type :q! to quit without writing. 
'%'
echo -n "Do you want to continue?  "
if ($< !~ y*) then
	echo "OK"
else
	vi /tmp/hvi$$
endif
rm -f /tmp/hvi$$
