% Linux Installation and Getting Started    -*- TeX -*-
% vi.tex
% Copyright (c) 1992, 1993 by Matt Welsh <mdw@sunsite.unc.edu>
%
% This file is freely redistributable, but you must preserve this copyright 
% notice on all copies, and it must be distributed only as part of "Linux 
% Installation and Getting Started". This file's use is covered by
% the copyright for the entire document, in the file "copyright.tex".

\section{Using the {\tt vi} Editor}\label{sec-vi}

\index{vi@{\tt vi}|(}
\index{text editor!defined}
\index{editor!defined}
A {\bf text editor} is simply a program used to edit files which contain
text, such as a letter, C program, or a system configuration file. 
While there are many such editors available for Linux, the only editor
which you are guaranteed to find on any UNIX system is {\tt vi}---
the ``visual editor''. {\tt vi} is not the easiest editor to use, nor
is it very self-explanatory. However, because it is so common in the UNIX
world, and at times you may be required to use it, it deserves some 
documentation here.

\index{text editor!comparing}
\index{Emacs}
Your choice of an editor is mostly a question of personal taste
and style. Many users prefer the baroque, self-explanatory and powerful
{\bf Emacs}---an editor with more features than any other single program in
the UNIX world. For example, Emacs has its own built-in dialect of the
LISP programming language, and has many extensions (one of which is an
``Eliza''-like AI program). However, because Emacs and all of its support
files are relatively large, you may not have access to it on many systems.
{\tt vi}, on the other hand, is small and powerful, but more difficult to
use. However, once you know your way around {\tt vi}, it's actually very
easy. It's just the learning curve which is sometimes difficult to cross.

This section is a coherent introduction to {\tt vi}---we won't discuss
all of its features, just the ones you need to know to get you started.
You can refer to the man page for {\tt vi} if you're interested in learning
about more of this editor's features. Or, you can read the book
{\em Learning the {\tt vi} Editor} from O'Reilly and Associates. See 
Appendix~\ref{app-info} for information.

\subsection{Concepts}
While using {\tt vi}, at any one time you are in one of three modes of 
operation. These modes are known as {\em command mode}, {\em insert mode},
and {\em last line mode}. 

\index{vi@{\tt vi}!command mode}
When you start up {\tt vi}, you are in {\em command mode}. This mode
allows you to use certain commands to edit files or to
change to other modes. For example, typing ``{\tt x}'' while in command mode
deletes the character underneath the cursor. The arrow keys move
the cursor around the file which you're editing. Generally, the commands
used in command mode are one or two characters long.

\index{vi@{\tt vi}!edit mode}
You actually insert or edit text within {\em insert mode}. When using
{\tt vi}, you'll probably spend most of your time within this mode. 
You start insert mode by using a command such as ``{\tt i}'' (for ``insert'')
from command mode. While in insert mode, you are inserting text into the
document from your current cursor location. To end insert mode and return
to command mode, press \key{esc}. 

\index{vi@{\tt vi}!last line mode}
{\em Last line mode\/} is a special mode used to give certain extended
commands to {\tt vi}. While typing these commands, they appear on the
last line of the screen (hence the name). For example, when you type
``{\tt :}'' from command mode, you jump into last line mode, and can
use commands such as ``{\tt wq}'' (to write the file and quit {\tt vi}), or
``{\tt q!}'' (to quit {\tt vi} without saving changes). Last line mode
is generally used for {\tt vi} commands which are longer than one character.
In last line mode, you enter a single-line command and press
\key{enter} to execute it.

\subsection{Starting {\tt vi}}
\index{vi@{\tt vi}!starting}
The best way to understand these concepts is to actually fire up {\tt vi}
and edit a file. In the example ``screens'' below, we're only going to
show a few lines of text, as if the screen was only six lines high
(instead of twenty-four). 

The syntax for {\tt vi} is
\begin{tscreen}
vi \cparam{filename}
\end{tscreen}
where \cparam{filename} is the name of the file that you wish to edit.

Start up {\tt vi} by typing
\begin{tscreen}
/home/larry\# {\em vi test}
\end{tscreen}
which will edit the file {\tt test}.
You should see something like
\begin{tscreen}\fbox{\begin{minipage}{0.8\textwidth}
\underline{\~{}} \\
\~{} \\
\~{} \\
\~{} \\
\~{} \\
\~{} \\
"test" [New file]
\end{minipage}}\end{tscreen}

The column of ``{\tt \~{}}'' characters indicates that you are the end of 
the file.

\subsection{Inserting text}

\index{vi@{\tt vi}!inserting text|(}
You are now in command mode; in order to insert text into the file, press
\key{i} (which will place you into insert mode), and begin typing.
\begin{tscreen}\fbox{\begin{minipage}{0.8\textwidth}
Now is the time for all good men to come to the aid of the party\underline{.} \\
\~{} \\
\~{} \\
\~{} \\
\~{} \\
\~{} 
\end{minipage}}\end{tscreen}

While inserting text, you may type as many lines as you wish (pressing
\key{return} after each, of course), and you may correct mistakes
using the backspace key.

To end insert mode, and return to command mode, press \key{esc}. 

While in command mode, you can use the arrow keys to move around the
file. Here, because we only have one line of text, trying to use the
up- or down-arrow keys will probably cause {\tt vi} to beep at you.

There are several ways to insert text, other than using the {\tt i} 
command. For example, the {\tt a} command inserts text beginning 
{\em after\/} the current cursor position, instead of on the current
cursor position. For example, use the left arrow key to move the cursor
between the words ``good'' and ``men''. 
\begin{tscreen}\fbox{\begin{minipage}{0.8\textwidth}
Now is the time for all good\underline{\ }men to come to the aid of the party. \\
\~{} \\
\~{} \\
\~{} \\
\~{} \\
\~{} 
\end{minipage}}\end{tscreen}
Press \key{a}, to start insert mode, type ``{\tt wo}'', and then hit
\key{esc} to return to command mode.
\begin{tscreen}\fbox{\begin{minipage}{0.8\textwidth}
Now is the time for all good wo\underline{m}en to come to the aid of the party. \\
\~{} \\
\~{} \\
\~{} \\
\~{} \\
\~{} 
\end{minipage}}\end{tscreen}

To begin inserting text at the line below the current one, use the {\tt o}
command. For example, press \key{o} and type another line or two:
\begin{tscreen}\fbox{\begin{minipage}{0.8\textwidth}
Now is the time for all good women to come to the aid of the party. \\
Afterwards, we'll go out for pizza and beer\underline{.} \\
\~{} \\
\~{} \\
\~{} \\
\~{}
\end{minipage}}\end{tscreen}

Just remember that at any time you're either in command mode (where
commands such as {\tt i}, {\tt a}, or {\tt o} are valid), or in
insert mode (where you're inserting text, followed by \key{esc} to 
return to command mode), or last line mode (where you're entering
extended commands, as discussed below).
\index{vi@{\tt vi}!inserting text|)}

\subsection{Deleting text}
\index{vi@{\tt vi}!deleting text|(}
From command mode, the {\tt x} command deletes the character under the
cursor. If you press \key{x} five times, you'll end up with:
\begin{tscreen}\fbox{\begin{minipage}{0.8\textwidth}
Now is the time for all good women to come to the aid of the party. \\
Afterwards, we'll go out for pizza and\underline{\ } \\
\~{} \\
\~{} \\
\~{} \\
\~{} 
\end{minipage}}\end{tscreen}
Now press \key{a}, insert some text, followed by \key{esc}:
\begin{tscreen}\fbox{\begin{minipage}{0.8\textwidth}
Now is the time for all good women to come to the aid of the party. \\
Afterwards, we'll go out for pizza and Diet Coke\underline{.} \\
\~{} \\
\~{} \\
\~{} \\
\~{} 
\end{minipage}}\end{tscreen}

You can delete entire lines using the command {\tt dd} (that is, 
press \key{d} twice in a row). If your cursor is on the second line,
and you type {\tt dd}, 
\begin{tscreen}\fbox{\begin{minipage}{0.8\textwidth}
\underline{N}ow is the time for all good women to come to the aid of the party. \\
\~{} \\
\~{} \\
\~{} \\
\~{} \\
\~{} 
\end{minipage}}\end{tscreen}

To delete the word which the cursor is on, use the {\tt dw} command.
Place the cursor on the word ``good'', and type {\tt dw}.
\begin{tscreen}\fbox{\begin{minipage}{0.8\textwidth}
Now is the time for all \underline{w}omen to come to the aid of the party. \\
\~{} \\
\~{} \\
\~{} \\
\~{} \\
\~{}
\end{minipage}}\end{tscreen}
\index{vi@{\tt vi}!deleting text|)}

\subsection{Changing text}
\index{vi@{\tt vi}!changing text|(}
You can replace sections of text using the {\tt R} command. Place the
cursor on the first letter in ``party'', press \key{R}, and type the 
word ``hungry''.
\begin{tscreen}\fbox{\begin{minipage}{0.8\textwidth}
Now is the time for all women to come to the aid of the hungry\underline{.} \\
\~{} \\
\~{} \\
\~{} \\
\~{} \\
\~{} 
\end{minipage}}\end{tscreen}
Using {\tt R} to edit text is much like the {\tt i} and {\tt a} commands,
but {\tt R} overwrites text, instead of inserting it.

The {\tt r} command replaces the single character under the cursor.
For example, move the cursor to the beginning of the word ``Now'', and
type {\tt r} followed by {\tt C}, you'll have:
\begin{tscreen}\fbox{\begin{minipage}{0.8\textwidth}
\underline{C}ow is the time for all women to come to the aid of the hungry. \\
\~{} \\
\~{} \\
\~{} \\
\~{} \\
\~{} 
\end{minipage}}\end{tscreen}

The ``{\tt \~{}}'' command changes the case of the letter under the
cursor from upper- to lower-case, and vise versa, For example, if you 
place the cursor on the ``o'' in ``Cow'', above, and repeatedly press
\key{\~{}}, you'll end up with:
\begin{tscreen}\fbox{\begin{minipage}{0.8\textwidth}
COW IS THE TIME FOR ALL WOMEN TO COME TO THE AID OF THE HUNGRY\underline{.} \\
\~{} \\
\~{} \\
\~{} \\
\~{} \\
\~{} 
\end{minipage}}\end{tscreen}
\index{vi@{\tt vi}!changing text|)}


\subsection{Moving commands}
\index{vi@{\tt vi}!moving cursor}
You already know how to use the arrow keys to move around the document.
In addition, you can use the {\tt h}, {\tt j}, {\tt k}, and {\tt l} 
commands to move the cursor left, down, up, and right, respectively. 
This comes in handy when (for some reason) your arrow keys aren't working
correctly. 

The {\tt w} command moves the cursor to the beginning of the next word;
the {\tt b} moves it to the beginning of the previous word. 

The {\tt 0} (that's a zero) command moves the cursor to the beginning of
the current line, and the {\tt \$} command moves it to the end of the line.

When editing large files, you'll want to move forwards or backwards through
the file a screenful at a time. Pressing \key{ctrl-F} moves the cursor
one screenful forward, and \key{ctrl-B} moves it a screenful back.

In order to move the cursor to the end of the file, type {\tt G}. You can
also move to an arbitrary line; for example, typing the command
{\tt 10G} would move the cursor to line 10 in the file. To move to the
beginning of the file, use {\tt 1G}. 

You can couple moving commands with other commands, such as deletion.
For example, the command {\tt d\$} will delete everything from the
cursor to the end of the line; {\tt dG} will delete everything from the
cursor to the end of the file, and so on.

\subsection{Saving files and quitting {\tt vi}}
\index{vi@{\tt vi}!saving changes}
\index{vi@{\tt vi}!writing changes}
\index{vi@{\tt vi}!quitting}
To quit {\tt vi} without making changes to the file, use the
command {\tt :q!}. When you type the ``{\tt :}'', the cursor will move
to the last line on the screen; you'll be in last line mode.
\begin{tscreen}\fbox{\begin{minipage}{0.8\textwidth}
COW IS THE TIME FOR ALL WOMEN TO COME TO THE AID OF THE HUNGRY. \\
\~{} \\
\~{} \\
\~{} \\
\~{} \\
\~{} \\
:\underline{\ }
\end{minipage}}\end{tscreen}
In last line mode, certain extended commands are available. One of them
is {\tt q!}, which quits {\tt vi} without saving. The command {\tt :wq}
saves the file and then exits {\tt vi}. The command {\tt ZZ} (from 
command mode, without the ``{\tt :}'') is equivalent to {\tt :wq}. 
Remember that you must press \key{enter} after a command entered
in last line mode. 

To save the file without quitting vi, just use {\tt :w}. 

\subsection{Editing another file}
\index{vi@{\tt vi}!switching files}
To edit another file, use the {\tt :e} command. For example, 
to stop editing {\tt test}, and edit the file {\tt foo} instead, use
the command
\begin{tscreen}\fbox{\begin{minipage}{0.8\textwidth}
COW IS THE TIME FOR ALL WOMEN TO COME TO THE AID OF THE HUNGRY. \\
\~{} \\
\~{} \\
\~{} \\
\~{} \\
\~{} \\
:e foo\underline{\ }
\end{minipage}}\end{tscreen}
If you use {\tt :e} without saving the file first, you'll get the error
message
\begin{tscreen}\fbox{\begin{minipage}{0.8\textwidth}
No write since last change (":edit!" overrides)
\end{minipage}}\end{tscreen}
which simply means that {\tt vi} doesn't want to edit another file until
you save the first one. At this point, you can use {\tt :w} to save the
original file, and then use {\tt :e}, or you can use the command
\begin{tscreen}\fbox{\begin{minipage}{0.8\textwidth}
COW IS THE TIME FOR ALL WOMEN TO COME TO THE AID OF THE HUNGRY. \\
\~{} \\
\~{} \\
\~{} \\
\~{} \\
\~{} \\
:e! foo\underline{\ }
\end{minipage}}\end{tscreen}
The ``{\tt !}'' tells {\tt vi} that you really mean it---edit the new file
without saving changes to the first.

\subsection{Including other files}
\index{vi@{\tt vi}!including files}
If you use the {\tt :r} command, you can include the contents of another
file in the current file. For example, the command
\begin{tscreen}
:r foo.txt
\end{tscreen}
would insert the contents of the file {\tt foo.txt} in the text at the
current cursor location.

\subsection{Running shell commands}
\index{vi@{\tt vi}!shell commands from}
You can also run shell commands from within {\tt vi}. The {\tt :r!} command
works like {\tt :r}, but instead of reading a file, it inserts the 
output of the given command into the buffer at the current cursor location.
For example, if you use the command 
\begin{tscreen}
:r! ls -F
\end{tscreen}
you'll end up with
\begin{tscreen}\fbox{\begin{minipage}{0.8\textwidth}
COW IS THE TIME FOR ALL WOMEN TO COME TO THE AID OF THE HUNGRY. \\
letters/ \\
misc/ \\
papers\underline{/} \\
\~{} \\
\~{} 
\end{minipage}}\end{tscreen}

You can also ``shell out'' of {\tt vi}, in other words, run a command
from within {\tt vi}, and return to the editor when you're done. For
example, if you use the command
\begin{tscreen}
:! ls -F
\end{tscreen}
the {\tt ls -F} command will be executed, and the results displayed on
the screen, but not inserted into the file which you're editing. 
If you use the command
\begin{tscreen}
:shell
\end{tscreen}
{\tt vi} will start an instance of the shell, allowing you to temporarily
put vi ``on hold'' while you execute other commands. Just logout of the
shell (using the {\tt exit} command) to return to {\tt vi}.


\subsection{Getting help}
{\tt vi} doesn't provide much in the way of interactive help (most UNIX 
programs don't), but you can always read the man page for {\tt vi}. 
{\tt vi} is a visual front-end to the {\tt ex} editor; it is {\tt ex}
which handles many of the last-line mode commands in {\tt vi}. So, in
addition to reading the man page for {\tt vi}, see {\tt ex} as well.
\index{vi@{\tt vi}|)}
