Using the VI editor
Background of vi
vi is the name of a popular text editor in the unix world. Vi has been
around for a long time, and is a quite capable text editor. It is closely
related to ex, which is a line-oriented text editors. These editors were
designed before monitors were in use, so they could operate on a
single-line teletype machine. Obviously, this was not an easy way to
process text, but it was all that the early computer users had. VI was
built as a visual interface on top of ex. It was a vast improvement, as
you could see an entire screenfull of the document at once. You will still
see vestiges of the line-editor heritage in vi. In fact, many of the most
important commands in vi are actually calls to the ex program. VI was
designed for a time when there were many different types of computers and
keyboards, so the interface had to be extremely simplistic. Many keyboards
did not have arrow keys or function keys, so all commands had to be
available through the basic keys on the keyboard. Memory was very tight,
so there was no online help or menus. This is text editing the way Grandma
used to do it!!!
Command and edit mode
The vi editor operates in two very distinct modes. In command mode,
the keyboard is used to issue commands to the program. In edit
mode, the keys can be used to enter or edit text. This can be a bit
confusing, as there is no clear way to tell which mode you are in when
using most versions of vi. The same key can have drastically different
results, depending on which mode you are in. For example, in edit mode,
the 'x' key types an x on the screen, but in command mode, 'x' means
'delete the current character.' It is a little strange, but you can get
used to it.
Getting Started
To get started in vi, just type vi filename at the command line, and
the file you specify will be opened or created. You will start in command
mode. To type in new text, use a to go to append mode (which will write
stuff AFTER the cursor's position, or i to go to insert mode, (which
will insert new text exactly at the cursor position.) You can then write
some text on the screen, but you cannot count on any keys except the normal
character keys. For example, the arrow keys are bound to give you big
problems when you try to back up like you do in modern word processors. To
do ANYTHING but type in text, you will need to get out of edit mode, and
into command mode. Use the escape key to do this.
Moving around
Of course, we are spoiled by the arrow keys in the modern world, but they
were not always a given. In command mode, you can usually use them, but
even so, you should know the alternatives. All the movement keys are under
the fingers of your right hand on the home row. 'j' and 'k' move down and up,
respectively. I remember this because j is the only character under the right
hand which goes below the line in manuscript. When you write a j, it goes
below the line you are writing on. Maybe you can think of a better mneumonic
device, but this one works for me. When you can remember that j goes down,
it just feels natural that k will go up a line. 'h' is to the left of j
and k, so naturally it moves the cursor to the left, and 'l' moves to the
right. This takes a lot of getting used to, but it is an important skill.
In addition to moving around one character at a time, vi allows us to move
around using other logical units. 'w' moves forward one word at a time,
and 'b' moves back a word at a time. '0' moves to the beginning of the
current line, and '$' moves to the end of it. '(' and ')' move to the
beginning and end of the current sentance (although it does not seem to be
terribly reliable), and '{' and '}' take you to the beginning and end of
the current paragraph. You can also use a number immediately before the
command to cause that command to happen a certain number of times. If I
place the cursor at the beginning of the previous sentence (on the Y of 'You
can'), then type '5w' in command mode, the cursor will end up on the n of
'number' because the w command (forward one word) happened 5 times.
searching
Perhaps the easiest way to get somewhere quickly in vi is through the
search command. The '/' character allows you to search for whatever text
you type after it, so, if we place the cursor at the top of this document,
and (in command mode, of course) typed '/monitor' (followed by the
'enter' key), we would find ourselves on the 13th line, at the beginning of
the word 'monitors'. After you have done a search, you can repeat it with
the 'n' command. This can be a very speedy way to find your way through a
document.
changing things
The hardest thing for most people to get used to in vi is that you cannot
change text as you write it, like in most modern programs. You have to go
to command mode first. Once you get used to that fact, you will find that
vi gives you a remarkable amount of control. To kill a character, place
the cursor over that character using the movement controls, and press the
'x' key. If you simply typed the wrong character, you can press 'r' over
the character, and the next character you type will replace whatever was
there.
You can also change a word easily. Just move the cursor to the beginning
of the word you want to fix, and type 'cw'. You will see a $ character, and
whatever you type will replace the word you were on. Press the escape key
to stop editing the word.
Deleting things
You can delete characters with the 'x' key, but it is far more powerful to
delete entire structures at a time. Of course, you will be in command mode
for this: to delete a word, go to the beginning of the word, and type dw.
You can precede this with a number; for example 3dw will delete the next
three words. You can also use the other movement commands to delete other
structures. 'b' means 'go back a word' so 'db' means delete the previous
word. We can also use d} to delete to the end of the paragraph, d$ to
delete to the end of the current line, and so on.
Putting them back
To return deleted things to the screen (in essence, when we deleted them,
we copied them, so we might want to paste them back somewhere else) we use
the p (for put) operator. So, to copy and paste something in vi, use some
variant of the d command to delete stuff, move the cursor wherever you want
the new text to go, and use the p command to paste the text there. VI also
uses special buffers so that you can get back the last few things you
deleted, or things that you stored in special buffers. You can precede
delete and put commands to use special buffers. For example, you might
want to frequently refer to some complex name in your document. Normally,
you might delete the word with a dw command. If you know you will need it
again, you can store it in buffer 'n' (or any other letter you wish) by
typing
"ndw
This means 'delete the word, and store it in buffer n.' To get the
contents of that buffer back, go to the spot in the text where you want the
word to be placed, and type
"np
which means 'put the contents of buffer n here.' In addition, even if you
do not specially store the deletions in a buffer, the latest thing you
deleted will be in buffer 1, and the next to last thing will be in buffer
2. As soon as you begin to think that vi is completely antiquated, it
does something better than most modern editors.
keyboard macros
One of the most important characteristics of a programmer's editor is the
ability to handle keyboard macros. It is remarkable how often a set of
keystrokes will need to be repeated many times. The classic example is
line indenting. You will frequently find in programming that you have to
indent a series of lines in two spaces. If you did this in vi, you would
rapidly discover that you are typing the exact same series of keystrokes
over and over. Indenting a line would involve the following commands:
- presume we're at the beginning of the line
- insert two spaces (i {space}{space})
- get back to command mode ({esc})
- go down one line (j)
- get to the beginning of this line (0)
To outdent (remove two spaces from the beginning of the line, we have a
similar sequence
- presume we're at the beginning of the line
- delete two characters (xx)
- go down one line (j)
- go to the beginning of that line (0)
Note that we have tried to start and end at similar places (always a good
idea in a keyboard macro) and we have carefully noted the keystrokes
necessary. vi gives us the map function, which is used to attach a series
of commands to some key. We can attach a macro to any key which does not
already have meaning. q, v, ^x, and ^y are good choices. Here's how we
would attach the 'out-dent two spaces' macro to the v key:
:map v xxj0
As you can see, we are simply repeating the keystrokes needed to perform
the out-dent. Indenting is a little trickier, as we will need to get into
edit mode (that's easy, use the i command) and get out of it (that's
trickier, how do we say the escape key?) Here's how we map the indent to
the q key:
:map q i j0
What we do here, is press control-v to explain that our next key is to be
recorded, not treated as a command immediately. This is needed when we
want to include an enter key or an escape key sequence into a macro.