Introduction to the vi/vim editor
Work on program 4

Lab 8
CSCI 112


Goals:

Introduce the basic operation and commands for the vi editor.


Notes:

You do not need to learn or use vi/vim for this class.  However, I believe that using an advanced editor will save you lots of time in the long run.


References:

Online version of Steve Oualline's vim book.

An excellent vi tutorial from Purdue. 



Vi overview
Versions of vi:
Original UNIX editor vi (comes with all UNIX operating systems)
New and iMproved vi: vim, available from www.vim.org, comes with Linux and Cygwin
Graphical version of vim: gvim, available from www.vim.org, comes with Linux and Cygwin

Department machines have vi vim and gvim

Telnet: you can use vi and vim via telnet, but you can't use gvim via telnet
How to start editing a file:  

$ gvim filename

Modal interface
The hardest thing to get use to is the two different modes
Insert mode:   the keys you type are inserted into the file.  Type an h and an h will be inserted into your file.

Command mode:  the keys you type are commands.  Type and h and the cursor will move to the left
There are several ways to enter insert mode (see below)
<esc> key stops the current insert

Moving around (while in command mode)

    h left
    j  down
    k up
    l right

    / pattern is search
    n is next
    ? is search back

Inserting text (must be in command mode to start inserting)

    i insert before cursor
    a insert after cursor

    I insert at start of line
    A insert at end of line

    C delete from cursor to end of line, start inserting

The command line editor
When vi was created, editors did not display the text being edited on the screen.  The vi stands for visual.

vi was built on top of the line editor ed.  A line editor allows you to edit a single line at a time.

The line editor provides lots of vi's functionality (read file, write file, substitute).

When in command mode, ":" allows you to enter a command that is passed to the line editor.

For example, if you wanted to change all occurrence of foo to bar you could use the s (for substitute) command
: 1,$ s/foo/bar/g
When using the line editor, you must first tell it what lines you want to edit.

In the above example, the "1,$" tells the editor you want to edit all lines 1 to $ ($ stands for the last line)

The "s" stands for substitute

After the first / you put the old string
After the second / you put the new string
the g stands for global (you want to substitute ALL the foo's on a given line to bar.
Three other common commands are
":q" for quit
":w" for write
":wq" for write and then quit
":e filename" to start editing another file

Exercise 1:
Create a new file using vi                    $ gvim f

Type in several lines of text.                type "i" to start inserting, <esc> to stop

Save the file                                        after you press <esc> to stop inserting, type ":w"

Try moving around the file                   use h j k l to move around

Try inserting new text


Exercise 2:
Try this excellent vi tutorial


Exercise 3:

If you are not interested in learning vi, you may:

work on the next assignment (I will answer questions)
leave (after I take role)