\chapter{Introduction to Vi}\label{vi-chapter}

\bttindex{vi}

{\tt vi} (pronounced ``vee eye'') is really the only editor you can
find at almost every \unix\ installation. It was originally written at
the University of California at 
Berkeley\index{University of California, Berkeley} and versions can be
found it almost every vendor's edition of \unix, including \linux. It
is initially somewhat hard to get used to, but it has many powerful
features. In general, we suggest that a new user learn
Emacs\ttindex{emacs}, which is generally easier to use. However,
people who will use more than one platform or find they dislike Emacs
may want to try to learn {\tt vi}.

%This chapter is designed to unearth the mysteries of {\tt vi}. Some
%people will convulse at the mere mention of {\tt vi}, and will declare
%that users no longer should be subjected to "Vicious".  Others
%overflow with admiration for the speed, flexibility and power that
%{\tt vi} offers its loyal users. In reality personal taste dictates
%what tools are the best to use. Some like sports-cars, others like
%luxury-cars, and the same applies to computer hardware, operating
%systems and in this case editors.  The object of this chapter and the
%tutorials, is so that you can better evaluate whether or not {\tt vi}
%suits you.

A brief historical view of {\tt vi} is necessary to understand how the
key \key{k}\ can mean move cursor up one line and why there are three
different modes of use. If you are itchy to learn the editor, then the
two tutorials will guide you from being a raw beginner, through to
having enough knowledge of the command set you are ever likely to
need. The chapter also incorporates a command guide, which makes a
useful reference to keep by the terminal.

Even if {\tt vi} does not become your regular text editor, the
knowledge of its use is not wasted. It is almost certain that the
\unix\ system you are using will have some variant of the {\tt vi}
editor.  It may be necessary to use {\tt vi} while installing another
editor, such as Emacs. Many Unix tools, applications and games use a
subset of the {\tt vi} command set.

\section{A Quick History of Vi}

Early text editors were line oriented and typically were used from
dumb printing terminals. A typical editor that operates in this mode
is {\bf Ed}\ttindex{ed}. The editor is powerful and efficient, using a
very small amount of computer resources, and worked well with the
display equipment of the time.  {\tt vi} offers the user a visual
alternative with a significantly expanded command set compared with
{\tt ed}.

{\tt vi} as we know it today started as the line editor {\tt
  ex}\ttindex{ex}.  In fact {\tt ex} is seen as a special editing mode of
{\tt vi}, although actually the converse is true. The visual component
of {\tt ex} can be initiated from the command line by using the {\tt
  vi} command, or from within {\tt ex}.

The {\tt ex}/{\tt vi} editor was developed at the University of
California at Berkeley\index{University of California, Berkeley} by
William Joy\index{Joy, William}. It was originally supplied as an
unsupported utility until its official inclusion in the release of
AT\&T System 5 Unix.  It has steadily become more popular, even with
the challenges of more modern full screen editors.

Due to the popularity of {\tt vi} there exists many clone variants and
versions can be found for most operation systems. It is not the
intention of this chapter to include all the commands available under
{\tt vi} or its variants. Many clones have expanded and changed the
original behaviour of {\tt vi}. Most clones do not support all the
original commands of {\tt vi}. 

If you have a good working knowledge of {\tt ed} then {\tt
  vi} offers a smaller learning curve to master. Even if you have no
intention of using {\tt vi} as your regular editor, a basic knowledge
of {\tt vi} can only be an asset.

\section{Quick Ed Tutorial}
\bttindex{ed}

The aim of this tutorial is to get you started using {\tt
  ed}. {\tt ed} is designed to be easy to use, and requires
little training to get started. The best way to learn is to practice,
so follow the instructions and try the editor before discounting its
practical advantages.

\subsection{Creating a file}
{\tt ed} is only capable of editing one file at a time. Follow the next
example to create your first text file using {\tt ed}.

\begin{verbatim}
/home/larry# ed
a
This is my first text file using Ed.
This is really fun.
.
w firstone.txt
/home/larry# q
\end{verbatim}

You can verify the file's contents using the Unix concatenate utility.
\begin{verbatim}
/home/larry# cat firstone.txt
\end{verbatim}

The above example has illustrated a number of important points. When
invoking {\tt ed} as above you will have an empty file. The key
\key{a}\ is used to add text to the file. To end the text entering
session, a period \key{.}\ is used in the first column of the text. To
save the text to a file, the key \key{q}\ is used in combination with
the file's name and finally, the key \key{q}\ is used to exit the
editor.

The most important observation is the two modes of operation.
Initially the editor is in command mode. A command is defined by
characters, so to ascertain what the user's intention is, {\tt
  ed} uses a {\bf text mode}, and a {\bf command mode}.

\subsection{editing a existing file}
To add a line of text to an existing file follow the next example. 
\begin{verbatim}
/home/larry# ed firstone.txt
a
This is a new line of text.
.
w
q
\end{verbatim}
        
If you check the file with {\tt cat} you'll see that a new line was
inserted between the original first and second lines. How did {\tt ed} know
where to place the new line of text?

When {\tt ed} reads in the file it keeps track of the current line.
The command \key{a}\ will add the text after the current line. {\tt
  ed} can also place the text before the current line with the key
command \key{i}. The effect will be the insertion of the text before
the current line.

Now it is easy to see that {\tt ed} operates on the text, line by line. All commands
can be applied to a chosen line.

To add a line of text at the end of a file.
\begin{verbatim}
/home/larry# ed firstone.txt
        $a
        The last line of text.
        .
        w
        q
\end{verbatim}

The command modifier \key{\$}\ tells {\tt ed} to add the line after the last 
line. To add the line after the first line the modifier would be \key{1}.
The power is now available to select the line to either add a line of text
after the line number, or insert a line before the line number.

How do we know what is on the current line? The command key \key{p}\ 
will display the contents of the current line. If you want to change
the current line to line 2 and see the contents of that line then do
the following.
\begin{verbatim}
/home/larry# ed firstone.txt
        2p
        q
\end{verbatim}

\subsection{Line numbers in detail}
        You have seen how to display the contents of the current line, by
the use of the \key{p} command. We also know there are line number modifiers
for the commands. To print the contents of the second line.
\begin{verbatim}
        2p
\end{verbatim}

        There are some special modifiers that refer to positions that can
change, in the lifetime of the edit session. The {\key \$} is the last line
of the text. To print the last line.
\begin{verbatim}
        $p
\end{verbatim}

The current line number uses the special modifier symbol {\key .}. To
display the current line using a modifier.
\begin{verbatim}
        .p
\end{verbatim}

This may appear to be unnecessary, although it is very useful in the context of
line number ranges.

To display the contents of the text from line 1 to line 2 the range needs 
to be supplied to {\tt ed}.
\begin{verbatim}
        1,2p
\end{verbatim}

The first number refers to the starting line, and the second refers to the
finishing line. The current line will subsequently be the second number of the command
range. 

        If you want to display the contents of the file from the start to
the current line.
\begin{verbatim}
        1,.p
\end{verbatim}

        To display the contents from the current line to the end of the
file.
\begin{verbatim}
        .,$p
\end{verbatim}

        All that is left is to display the contents of the entire file which
is left to you.

        How can you delete the first 2 lines of the file.
\begin{verbatim}
        1,2d
\end{verbatim}

The command key {\key d} deletes the text line by line. If you wanted
to delete the entire contents you would issue.
\begin{verbatim}
        1,$d
\end{verbatim}

If you have made to many changes and do not want to save the contents
of the file, then the best option is to quit the editor without
writing the file beforehand.

Most users do not use {\tt ed} as the main editor of choice. The more modern
editors offer a full edit screen and more flexible command sets.  Ed
offers a good introduction to {\tt vi} and helps explain where its
command set originates.

\ettindex{ed}

\section{Quick Vi Tutorial}
The aim of this tutorial is to get you started using the {\tt vi} editor. This
tutorial assumes no {\tt vi} experience, so you will be exposed to the ten most basic
{\tt vi} commands.
These fundamental commands are enough to perform the bulk of your editing needs, and
you can expand your {\tt vi} vocabulary as needed. It is recommended you
have a machine to practice with, as you proceed through the tutorial.

\subsection{Invoking vi}
To invoke vi, simply type the letters {\tt vi} followed by the name
of the file you wish to create.  You will see a screen with
a column of tildes (\verb"~") along the left side. {\tt vi} is now in 
command mode.  Anything you type will be understood as a command, not
as text to be input.  In order to input text, you must type a command.
The two basic input commands are the following:

\begin{verbatim}
     i     insert text to the left of the cursor
     a     append text to the right of the cursor
\end{verbatim}

Since you are at the beginning of an empty file, it doesn't matter 
which of these you type. Type one of them, and then type in the 
following text (a poem by Augustus DeMorgan found in {\em The Unix Programming 
Environment} by B.W. Kernighan and R. Pike):

\begin{verbatim}
     Great fleas have little fleas<Enter>
       upon their backs to bite 'em,<Enter>
     And little fleas have lesser fleas<Enter>
       and so ad infinitum.<Enter>
     And the great fleas themselves, in turn,<Enter>
       have greater fleas to go on;<Enter>
     While these again have greater still,<Enter>
       and greater still, and so on.<Enter>
     <Esc>
\end{verbatim}

Note that you press the \key{Esc} key to end insertion and return 
to command mode.

\subsection{Cursor movement commands}

\begin{verbatim}
     h     move the cursor one space to the left
     j     move the cursor one space down
     k     move the cursor one space up
     l     move the cursor one space to the right
\end{verbatim}

These commands may be repeated by holding the key down. Try moving 
around in your text now. If you attempt an impossible movement, 
e.g., pressing the letter {\bf k} when the cursor is on the top line, 
the screen will flash, or the terminal will beep.  Don't worry, it won't 
bite, and your file will not be harmed.

\subsection{Deleting text}
\begin{verbatim}
     x     delete the character at the cursor
     dd    delete a line
\end{verbatim}

Move the cursor to the second line and position it so that it 
is underneath the apostrophe in {\em 'em}. Press the letter \key{x}, and 
the {\em '} will disappear. Now press the letter \key{i} to move into 
insert mode and type the letters {\bf th}. Press \key{Esc} when you 
are finished.

\subsection{File saving}
\begin{verbatim}
     :w    save (write to disk)
     :q    exit 
\end{verbatim}

Make sure you are in command mode by pressing the \key{Esc} key. Now
type {\bf :w}. This will save your work by writing it to a disk file.

The command for quitting vi is \key{q}. If you wish to combine saving 
and quitting, just type {\bf :wq}. There is also a convenient abbreviation 
for {\bf :wq} --- {\bf ZZ}. Since much of your programming work will consist of 
running a program, encountering a problem, calling up the program in 
the editor to make a small change, and then exiting from the editor to 
run the program again, {\bf ZZ} will be a command you use often. 
(Actually, {\bf ZZ} is not an exact synonym for {\bf :wq} --- if you have not 
made any changes
to the file you are editing since the last save, {\bf ZZ} will just exit
>from the editor whereas {\bf :wq} will (redundantly) save before exiting.)

If you have hopelessly messed things up and just want to start 
all over again, you can type {\bf :q!} (remember to press the \key{Esc} 
key first). If you omit the {\bf !}, vi will not allow you to quit 
without saving.

\subsection{What's next}

The ten commands you have just learned should be enough for your 
work. However, you have just scratched the surface of 
the vi editor. There are commands to copy material from 
one place in a file to another, to move material from one place 
in a file to another, to move material from one file to another, 
to fine tune the editor to your personal tastes, etc. In all, 
there about 150 commands.

\section{Advanced Vi Tutorial}
The advantage and power of {\tt vi} is the ability to use it successfully
with only knowing a small subset of the commands. Most users of {\tt vi}
feel a bit awkward at the start, however after a small amount of time they find
the need for more command knowledge.

The following tutorial is assuming the user has completed the quick
tutorial (above) and hence feels comfortable with {\tt vi}. It will
expose some of the more powerful features of {\tt ex}/{\tt vi} from
copying text to macro definitions.  There is a section on {\tt ex} and
its settings which helps customize the editor. This tutorial describes
the commands, rather then taking you set by set through each of them.
It is recommended you spend the time trying the commands out on some
example text, which you can afford to destroy.

This tutorial does not expose all the commands of {\tt vi} though all of the
commonly used commands and more are covered. Even if you choose to use
an alternative text editor, it is hoped you will appreciate {\tt vi} and
what it offers those who do choose to use it.

\subsection{Moving around}
The most basic functionality of an editor, is to move the cursor around in the text.
Here are more movement commands.
\begin{verbatim}
        h          move the cursor one space to the left
        j          move one line down
        k          move one line up
        l          move one line right

        Some implementations also allow the arrows keys to move the cursor.

        w          move to the start of the next word
        e          move to the end of the next word
        E          move to the end of the next word before a space
        b          move to the start of the previous word
        0          move to the start of the line
        ^          move to the first word of the current line
        $          move to the end of the line
        <CR>       move to the start of the next line
        -          move to the start of the previous line
        G          move to the end of the file
        1G         move to the start of the file
        nG         move to line number n
        <Cntl> G   display the current line number
        %          to the matching bracket
        H          top line of the screen
        M          middle line of the screen
        L          bottom of the screen
        n|         more cursor to column n
\end{verbatim}

The screen will automatically scroll when the cursor reaches either the top
or the bottom of the screen. There are alternative commands which can control
scrolling the text.

\begin{verbatim}
        <Cntl> f   scroll forward a screen
        <Cntl> b   scroll backward a screen
        <Cntl> d   scroll down half a screen
        <Cntl> u   scroll down half a screen
\end{verbatim}

        The above commands control cursor movement. Some of the commands
use a command modifier in the form of a number preciding the command. This
feature will usually repeat the command that number of times.

To move the cursor a number of positions left.
\begin{verbatim}
        nl         move the cursor n positions left
\end{verbatim}

If you wanted to enter a number or spaces in front of the some text you could
use the command modifier to the insert command. Enter the repeat number then
\key{i} followed by the space then press \key{ESC}.
\begin{verbatim}
        ni         insert some text and repeat the text n times.
\end{verbatim}

The commands that deal with lines use the modifier to refer to line numbers.
The \key{G} is a good example.

\begin{verbatim}
        1G         Move the cursor to the first line.
\end{verbatim}

{\tt vi} has a large set of commands which can be used to move the cursor
around the file. Single character movement through to direct line placement
of the cursor. {\tt vi} can also place the cursor at a selected line from the
command line.
\begin{verbatim}
        vi +10 myfile.tex
\end{verbatim}
This command opens the file called {\em myfile.tex} and places the cursor
10 lines down from the start of the file.

Try out some of the commands in this section. Very few people
can remember all of them in one session. Most users use only a subset of the
above commands. 

You can move around, so how do you change the text?

\subsection{Modifing Text}

        The aim is to change the contents of the file and {\tt vi} offers a
very large set of commands to help in this process. 

This section will focus on adding text, changing the existing text
and deleting the text. At the end of this section you will have the 
knowledge to create any text file desired. The remaining sections
focus on more desireable and convenient commands.

        When entering text, multiple lines can be entered by using the
\key{return} key. If a typing mistake needs to be corrected and you are on the
entering text on the line in question. You can use the \key{backspace} key to 
move the cursor over the text. The different implementations of {\tt vi} 
behave differently. Some just move the cursor back and the text can still
be viewed and accepted. Others will remove the text as you backspace. Some
clones even allow the arrow keys to be used to move the cursor when
in input mode. This is not normal {\tt vi} behaviour. If the text is visable
and you use the \key{ESC} key when on the line you have backspaced on the
text after the cursor will be cleared. Use your editor to become accustomed
to its' behaviour.

\begin{verbatim}
        a           Append some text from the current cursor postion
        A           Append at the end of the line
        i           Insert text to the Left of the cursor
        I           Inserts text to the Left of the first non-white character
                        on current line
        o           Open a new line and adds text Below current line
        O           Open a new line and adds text Above the current line
\end{verbatim}

        We give it and we take it away. {\tt vi} has a small set of delete
commands which can be enhanced with the use of command modifiers.

\begin{verbatim}
        x           Delete one character from under the cursor
        dw          Delete from the current position to the end of the word
        dd          Delete the current line.
        D           Delete from the current position to the end of the line
\end{verbatim}

The modifiers can be used to add greater power to the commands.
The following examples are a subset of the posibilities.
\begin{verbatim}
        nx          Delete n characters from under the cursor
        ndd         Delete n lines
        dnw         Deletes n words. (Same as ndw)
        dG          Delete from the current position to the end of the file
        d1G         Delete from the current postion to the start of the file
        d$          Delete from current postion to the end of the line
                    (This is the same as D)
        dn$         Delete from current line the end of the nth line
\end{verbatim}

The above command list shows the delete operating can be very powerfull.
This is evident when applied in combination with the cursor movement
commands. One command to note is \key{D} since it ignores the modifier
directives.

On occasions you may need to undo the changes. The following commands
restore the text after changes.
\begin{verbatim}
        u           Undo the last command
        U           Undo the current line from all changes on that line
        :e!         Edit again. Restores to the state of the last save
\end{verbatim}
{\tt vi} not only allows
you to undo changes, it can  reverse the undo. Using the command
\key{5dd} delete 5 lines then restore the lines with \key{u}. The changes
can be restored by the \key{u} again.

{\tt vi} offers commands which allow changes to the text to be made
without first deleting then typing in the new version.
\begin{verbatim}
        rc          Replace the character under the cursor with c
                    (Moves cursor right if repeat modifier used eg 2rc)
        R           Overwrites the text with the new text
        cw          Changes the text of the current word
        c$          Changes the text from current position to end of the line
        cnw         Changes next n words.(same as ncw)
        cn$         Changes to the end of the nth line
        C           Changes to the end of the line (same as c$)
        cc          Changes the current line
        s           Substitutes text you type for the current character
        ns          Substitutes text you type for the next n characters
\end{verbatim}

The series of change commands which allow a string of characters to be
entered are exited with the\key{ESC} key.

The \key{cw} command started from the current location in the word to the
end of the word. When using a change command that specifies a distance 
the change will apply. {\tt vi} will place a {\bf \$} at the last character
position. The new text can overflow or underflow the original text length.

\subsection{Copying and Moving sections of text}

        Moving text involves a number of commands all combined to achieve
the end result. This section will introduce named and unnamed buffers along
with the commands which cut and paste the text.

Coping text involves three main steps.
\begin{enumerate}
\item {\bf Yanking} (copying) the text to a buffer.
\item {\bf Moving} the cursor to the destination location.
\item {\bf Pasting} (putting) the text to the edit buffer.
\end{enumerate}

To {\bf Yank} text to the unnamed use \key{y} command.
\begin{verbatim}
        yy          Move a copy of the current line to the unnamed buffer.
        Y           Move a copy of the current line to the unnamed buffer.
        nyy         Move the next n lines to the unnamed buffer
        nY          Move the next n lines to the unnamed buffer
        yw          Move a word to the unnamed buffer.
        ynw         Move n words to the unnamed buffer.
        nyw         Move n words to the unnamed buffer.
        y$          Move the current position to the end of the line.
\end{verbatim}

The unnamed buffer is a tempory buffer that is easily currupted by other
common commands. On occations the text my be needed for a long period
of time. In this case the named buffers would be used. {\tt vi} has 26
named buffers. The buffers use the letters of the alphabet as the
identification name. To distinguish the difference between a command
or a named buffer, {\tt vi} uses the \key{"} character. 
When using a named buffer by the lowercase letter the contents are over written while
the uppercase version appends to the current contents.
\begin{verbatim}
        "ayy        Move current line to named buffer a.
        "aY         Move current line to named buffer a.
        "byw        Move current word to named buffer b.
        "Byw        Append the word the contents of the named buffer b.
        "by3w       Move the next 3 words to named buffer b.
\end{verbatim}

Use the \key{p} command to paste the contents of the cut buffer to the
edit buffer.
\begin{verbatim}
        p           Paste from the unnamed buffer to the RIGHT of the cursor
        P           Paste from the unnamed buffer to the LEFT of the cursor
        nP          Paste n copies of the unnamed buffer to the LEFT of the cursor
        "ap         Paste from the named buffer a RIGHT of the cursor.
        "b3P        Paste 3 copies from the named buffer b LEFT of the cursor.
\end{verbatim}

When using {\tt vi} within an xterm you have one more option for copying text.
Highlight the section of text you wish to copy by draging the mouse cursor
over text.
Holding down the left mouse button and draging the mouse from the start
to the finish will invert the text. This automatically places the text into 
a buffer reserved by the X server. To paste the text press the middle button.
Remmember the put {\tt vi} into insert mode as the input could be interpreted
as commands and the result will be unknown. Using the same techinque a single
word can be copied by double clicking the left mouse button over the word. Just
the single word will be copied. Pasting is the same as above. The buffer 
contents will only change when a new highlighted area is created.

Moving the text has three steps.
\begin{enumerate}
\item {\bf Delete} text to a named or unnamed buffer.
\item {\bf Moving} the cursor the to destination location.
\item {\bf Pasting} the named or unnamed buffer.
\end{enumerate}

The process is the same as copying with the change on step one to delete.
When the command \key{dd} is performed the line is deleted and placed into
the unnamed buffer. You can then paste the contents just as you had
when copying the text into the desired position.

\begin{verbatim}
         "add       Delete the line and place it into named buffer a.
         "a4dd      Delete 4 lines and place into named buffer a.
         dw         Delete a word and place into unnamed buffer
\end{verbatim}
See the section on modifying text for more examples of deleting text.

On the event of a system crash the named and unnamed buffer contents are
lost but the edit buffers content can be recovered (See Usefull commands).

\subsection{Searching and replacing text}

{\tt vi} has a number of search command. You can search for individual
charaters through to regular expressions.

The main two character based search commands are \key{f} and \key{t}.
\begin{verbatim}
         fc         Find the next character c. Moves RIGHT to the next.
         Fc         Find the next character c. Moves LEFT to the preceding.
         tc         Move RIGHT to character before the next c.
         Tc         Move LEFT to the character following the preceding c.
                    (Some clones this is the same as Fc)
         ;          Repeats the last f,F,t,T command
         ,          Same as ; but reverses the direction to the orginal command.
\end{verbatim}

If the character you were searching for was not found, {\tt vi} will
beep or give some other sort of signal.

{\tt vi} allows you to search for a string in the edit buffer.
\begin{verbatim}
         /str       Searches Right and Down for the next occurance of str.
         ?str       Searches Left and UP for the next occurance of str.
         n          Repeat the last / or ? command
         N          Repeats the last / or ? in the Reverse direction.
\end{verbatim}

 When using the \key{/} or \key{?} commands a line will be cleared along
the bottom of the screen. You enter the search string followed by \key{RETURN}.

The string in the command \key{/} or \key{?} can be a regular expression. A
regular expression is a description of a set of characters. The description
is build using text intermixed with special characters. The special
characters in regular expressions are {\bf . * [] \ \^ \$}.

\begin{verbatim}
         .          Matches any single character except newline.
         \          Escapes any special characters.
         *          Matches 0 or More occurances of the preceding character.
         []         Matches exactly one of the enclosed characters.
         ^          Match of the next character must be at the begining of the line.
         $          Matches characters preceding at the end of the line.
         [^]        Matches anything not enclosed after the not character.
         [-]        Matches a range of characters.
\end{verbatim}

        The only way to get use to the regular expression is to use them.
Following is a series of examples.

\begin{verbatim}
         c.pe       Matches cope, cape, caper etc
         c\.pe      Matches c.pe, c.per etc
         sto*p      Matches stp, stop, stoop etc
         car.*n     Matches carton, cartoon, carmen etc
         xyz.*      Matches xyz to the end of the line.
         ^The       Matches any line starting with The.
         atime$     Matches any line ending with atime.
         ^Only$     Matches any line with Only as the only word in the line.
         b[aou]rn   Matches barn, born, burn.
         Ver[D-F]   Matches VerD, VerE, VerF.
         Ver[^1-9]  Matches Ver followed by any non digit.
        the[ir][re] Matches their,therr, there, theie.
  [A-Za-z][A-Za-z]* Matches any word.
\end{verbatim}

        {\tt vi} uses {\tt ex} command mode to perform search and replace
operations. All commands which start with a colon are requests in {\tt ex} 
mode. 

The search and replace command allows regular expression to be used over
a range of lines and replace the matching string. The user can ask for
confirmation before the substitution is performed. It may be well worth
a review of line number representation in the {\tt ed} tutorial.
\begin{verbatim}
         :<start>,<finish>s/<find>/<replace>/g   General command

         :1,$s/the/The/g      Search the entire file and replace the with The.
         :%s/the/The/g        % means the complete file. (Same as above).
         :.,5s/^.*//g         Delete the contents from the current to 5th line.
         :%s/the/The/gc       Replace the with The but ask before substituting.
         :%s/^....//g         Delete the first four characters on each line.
\end{verbatim}

The search command is very powerfull when combined with the regular expression
search strings. If the \key{g} directive is not included then the change is
performed only on the first occurance of a match on each line. 

Sometimes you may want to use the original search string in the replacement
result. You could retype the command on the line but {\tt vi} allows 
the replacement string to contain some special characters.
\begin{verbatim}
         :1,5s/help/&ing/g    Replaces help with helping on the first 5 lines.
         :%s/ */&&/g          Double the number of spaces between the words.
\end{verbatim}

Using the complete match string has its limits hence {\tt vi} uses the
escaped parentheses \key{\\(} and \key{\\)} to select the range of the
substitution.  Using an escaped digit \key{\\1} which identifies the
range in the order of the definition the replacement can be build.
\begin{verbatim}
         :s/^\(.*\):.*/\1/g   Delete everything after and including the colon.
         :s/\(.*\):\(.*\)/\2:\1/g    Swap the words either side of the colon.
\end{verbatim}

You will most likely read the last series of gems again. {\tt vi}
offers powerfull commands that many more modern editors do not or can
not offer.  The cost for this power is also the main argument against
{\tt vi}. The commands can be difficult to learn and read. Though most
good things can be a little awkward at first.  With a little practice
and time, the {\tt vi} command set will become second nature.

\ettindex{vi}
% Local Variables: 
% mode: latex
% TeX-master: "guide"
% End: 
