============================
The VI (VIM) Editor - Basics
============================
-IAN! idallen@idallen.ca

Reference: Online VIM tutorial.

On most Linux/BSD machines, the VIM editor comes with a short online
tutorial in several languages.  Set your terminal window to 25 lines by
80 characters and try typing "vimtutor" at a shell prompt.  (You can
get the tutorial in French by typing "vimtutor fr".)  Other languages
are available.

------------------
Versions of vi/vim
------------------

The stock Unix "vi" and the open-source "vim" (and various other clone
editors such as "nvi", "elvis", and "stevie") are all based on the
original 1980's Berkeley "vi" program written by Bill Joy.

On Linux machines, "vi" and "vim" are usually the same program ("vim").
The Course Linux Server also has a full version of "vim" installed as both
"vi" and "vim".  Both "vi" and "vim" lead to the same program on Linux.

You may find that some of the features described in a textbook only work
on a full installation of "vim" (e.g. the "Getting Help" commands).

I will refer to the editor using its traditional name "vi" - you should
always interpret this to mean modern "vim", not the old "vi" editor.

-----------------------------
VI Cheat Sheets and Tutorials
-----------------------------

You can find good "cheat sheets" on VI by searching on the Internet for
"vi editor cheat sheet" or "vi editor summary pdf" or "vi reference card".
URLs come and go; but, these were working at one point:

    http://www.colostate.edu/services/acns/bulls/ed03.pdf
    http://home.earthlink.net/~krfrazer2/vi_cmd_summary.pdf
    http://www.kcomputing.com/kcvi.pdf

There are tutorials available online.  (See below for the location of
the online tutorial.)

-------------
What to Learn
-------------

You can get by in VI with knowing how to read in a file, delete and
append single characters and lines, and write the file back out.
(See the list of "bare minimum" commands, below.)

The more you learn about VI, the faster you will edit files and the
better your mark will be on tests and exams.  VI skill is essential to
you as a Unix programmer; it is now the universal Unix editor.

Almost every letter, upper and lower case, is a VI command.  The more
you learn, the faster you can edit.  While you may begin by learning
to delete a word slowly using ten "x" commands, eventually you should
evolve into knowing how to use the single quick "dw" command.

See below for the location of the online VIM tutorial.

---------------------------------
WARNING! Configure your Terminal!
---------------------------------

Make sure your terminal is correctly configured before you enter VI!
The VI/VIM editors must know how to drive your terminal during an edit
session; if you see anything about an "unknown" terminal type when you
start up the editor, you must exit immediately and fix the terminal type.
(See the other configuration guidelines in the "terminal.txt" page.)

Be careful when you resize a window containing a VI/VIM session.  A remote
session of VI on another machine may not pick up the new window size,
and what you see on your screen may get garbled.  (If this happens,
quit VI, type "resize" at the shell prompt, and restart VI again.)

To be safe, do not change the size of a VI/VIM window when the editor
is open.

If you use telnet from under Windows to connect to a Unix machine, make
sure you have NO SCROLL BARS VISIBLE in your telnet window!  Set the
correct number of lines and correct terminal type when you start telnet,
and don't change them.  (This won't be a problem for people working from
a Unix machine, since Unix is good about passing the window size to VI.)

The "resize" command is useful to set the number of lines correctly
after you telnet or ssh into the machine:

    $ resize

This is outlined in detail in the "Using Terminal" web page (terminal.txt).

--------------------
Avoid the Arrow Keys
--------------------

Do not use the arrow keys to move the cursor - use the command-mode
letters h,j,k,l instead.  The letters don't require you to move your hands
from the home row on your keyboard, and they encourage you to remain in
command mode where you can use all the VIM commands.  Arrow keys will
slow you down, and your employer and other Unix programmers will make
rude remarks about how slow you are.

----------------------------------
Setting VIM Options - Getting Help
----------------------------------

VIM comes with hundreds of options that influence how it works.  You
can set the options at any time; but, to save them, you need to put
commands into a file named ".vimrc" in your home directory.

The following are good options that you should set in your .vimrc file:

    $ echo "set showcmd showmode confirm ruler" >>$HOME/.vimrc
    $ cat $HOME/.vimrc
    set showcmd showmode confirm ruler

The above shell command line appends the options to your .vimrc file.
You only need to do this once; VIM will read the file on start-up.
If you execute the above command more than once, you will put multiple
copies of the options into the file.  This isn't an error; but, it isn't
necessary either.  Edit the file and remove the duplicate lines.

The full "vim" editor has help available by typing ":help", e.g. you
can type ":help showcmd".  This splits your terminal screen and opens
up a help file inside "vim".  To close the help file, just do :q as you
would do to close any file.

Some people also like to have option "set number" in their .vimrc.

-------------------
Online VIM Tutorial
-------------------

On most Linux/BSD machines, the VIM editor comes with a short online
tutorial in several languages.  Set your terminal window to 25 lines by
80 characters and try typing "vimtutor" at a shell prompt.  (You can
get the tutorial in French by typing "vimtutor fr".)  Other languages
are available.

If "vimtutor" doesn't work, you will have to locate and copy the tutorial
file yourself.  On most Unix/Linux machines, you will find the tutorial
somewhere under the directories /usr/share/vim* or /usr/local/share/vim*,
e.g. possibly /usr/share/vim/tutor/tutor.  Different Unix systems put
the tutorial in different places; go look for the file named "tutor"
under the above places.

The vim tutorial is a file named "tutor" under directory
/usr/share/vim/vim63/tutor/ on Knoppix 3.7 and under
/usr/share/vim/tutor/ on the Course Linux Server.

The "vimtutor" command startes up vim on a fresh copy of the tutorial
file.  If you are not using "vimtutor", you must copy the tutorial file
into your own directory first:

    $ cd
    $ cp /usr/share/vim/tutor/tutor tutor     # use the actual pathname!
    $ vim tutor

Use the actual pathname to the "tutor" file on your system.
Follow the instructions in the tutorial.

The VIM tutorial was written to be used under various operating systems,
including MS-DOS; so, it has one or two MS-DOS specific external commands
in it (e.g. there is no DEL command in Unix - use rm instead!).  Also,
if you have "set ruler" turned on, the Ctrl-g command will not show your
current line number; because, the ruler line already shows it for you.

---------------------------------------
Absolute bare minimum basic VI commands
---------------------------------------

Look up the meaning of these VI/VIM commands:

    ESC
    a i x dd
    h j k l
    :help
    :r file
    :w file
    :q
    :q!

The above commands will perform most any edit on any file you have.
All the other things in VI simply make the editing faster and get you
better marks (and a better job) in less time.

Some of the other VI commands make the editing much, much faster.
Compare "dd" (delete a whole line) with typing 80 "x" commands, or
":1,$s/idallen/alleni/g" to replace every occurence of "idallen" with
"alleni" in the whole file using a single global search and replace!

------------------------
What to learn Next in VI
------------------------

Other useful things to know in VI, in vague order of importance:

    u                        -- undo
    p P                      -- put (after yank or delete)
    /search string           -- search for pattern in file
    .                        -- repeat last change
    r                        -- replace one character
    o O                      -- start a new, empty input line
    5dd 10x                  -- repeat counts on most commands
    0 ^ $ H L M              -- position cursor on screen
    A I                      -- insert text at end/start of line
    U                        -- undo all changes on current line
    cc s                     -- change current line, current char
    f F                      -- move forward/backward to character
    !!                       -- pipe text into any Unix command
    :1,$s/pattern/new text/g -- global search and replace
    :set showcmd             -- show partial commands in status line
    :set showmode            -- show INPUT mode
    :set confirm             -- ask before quitting modified buffer
    :set ruler               -- show current file position
    :set autoindent          -- auto-indent lines of code
    :set wrapmargin=5        -- wrap input text at column 75
    :set number              -- show buffer with line numbers
    :syntax off              -- turn off syntax highlighting
    :$r file                 -- read file in at end of buffer
    :$r! program             -- read output of executing program

The VIM tutoral has details on many of these commands.