Introduction to the vim editor
Work on program 4 (on Tuesday) or program 5 (on Thursday)
Lab 9
CSCI 112
Goals:
Introduce the basic operation and
commands for the vi editor.
Notes:
I will not force you to use vim
for your 112 projects. However, if you take the time to learn it
now you will save hundreds of hours in your upper division courses.
There is a graphical version of vim called gvim. It does not work
on the lab machines but is available for Windows, Linux, and Mac OSX.
It is easier to use gvim but the real power of vim comes from
knowing the keystroke commands.
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 but not gvim
Putty: you can use vi and vim via putty, but you can't use gvim via putty
How to start editing a file:
$ vim 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
$ vim 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.