 |

The VI FAQ
Table of Contents
These are some basic hints for the novice vi user. First, keep a
command summary with you at all times. A quick reference guide/command
summary is included later on. Second, get a good book that covers vi.
This document is not the best way to learn it (at least not yet.) I'm
not sure if this document should teach people to to use vi from
scratch, as there are many good books on it already. However, there
are hints here. As for choosing a book, the standard rules apply:
look at it before buying it. See if any of it makes sense to you.
Make sure that it has exercises that you can practice with. Compare it
with other books -- after all, vi can be very confusing, and you want
to make sure that it is the book that is confusing, and not just that
you underestimated the difficulty of learning vi.
Also, seek out vilearn or vitutor, programs designed to teach you the
basics of vi.
This may seem a bit silly, but there are many games on Unix systems
that can help you learn to use vi. These help particularly with the
basics. Although I don't know of any games that help with every vi
command, I do know of a few that will help you learn to use hjkl to
move the cursor around. NetHack, a rogue-like game, is particularly
good for this, as it is a large game and can be entertaining for quite
some time. Not to make the other games sound worse, but some other
ones are: rogue, moria, omega, worm, and snake.
Often cited as one of the main problems with vi, and equally often
cited as being one of its best strengths, vi differentiates between a
"Command mode" and an "Insert mode." Understanding this difference is
VITAL to learning vi. When one starts vi it starts in command mode.
In this mode, one can move around the file, and issue commands to
change certain areas of the text, cut, copy and paste sections of the
text and do much more. Insert mode is where one can actually insert
text. In other words, command mode is used to move around the
document, and insert mode is used to type text into the document.
Commands such as: a, i, c, C, O, o and others will switch one from
command mode to insert mode.
&leEsc> or ^C will take one out of insert mode and return one to
command mode.
Get used to this distinction. It is one of the things that makes vi
different from most other editors. It also allows one to do a lot of
things without taking one's hands from the standard keyboard position.
Try hitting ^[ instead. If your keyboard has a <Meta> key, try
that. If neither of these work, try ^3.
They're just there to let you know where the bottom of your file is,
they are not actually in your document, and you do not need to worry
about them.
First, if your terminal is set properly and you have a good
connection, you should be able to use the arrow keys. However, if you
think that you will be using vi a lot, then it makes sense to learn
hjkl, as they are faster to type. Also, there are occasions where you
may have a bad connection and the ESC sequences may get lost.
:q! will do it. If vi appears to have frozen, make sure that you
haven't hit ^S by mistake. In order to undo a ^S, hit ^Q.
:r <filename>
For example, to insert the file /etc/motd, type: :r /etc/motd
This will insert the file at the current location in the file you are
working on. If you specify a number before the r, it will insert it at
that location in the file.
/<text> will search forward. ?<text> will search backwards. ?? or
// will repeat the last search. It is worth noting that these are
pretty much standard in Unix. In addition, in vi, n will find the next
occurrence. N will repeat the last search, reversing the direction.
Regular Expressions may be used within searches.
/^V^<seq>
^V will tell vi to take the next character literally, and not to take
it as a command.
If your computer has the program fmt on it, all you need to do is
type !}fmt from command mode (without a : before it). This will
rejustify the text from the current location until the end of the
paragraph. If your machine does not have fmt, you need to find a
similar program. (I gather there are many such programs available from
the public domain, but I do not know much about them.)
Okay, this might be a bit complicated. Take from this section what
you can, and reread it a few times. Also, experiment.
"<letter>yy will copy one line of text into register <letter>. (A
register is vi-lingo for a place to store data that was cut or copied.)
<letter> must be between a and z. "<letter>dd will delete one line and
place it in register <letter>. You may use a number before the yy or
dd to specify the number of lines. Using an uppercase <letter> will
append the text into the register leaving what was there before.
"<letter>p will put the text after the cursor. "<letter>P will put it
before the cursor. If the register contains the beginning or end of a
line, the line will be placed on another line as appropriate. Y may be
used as a short cut for yy. In addition, y$, yH, yM, etc. are valid,
as are the equivalent d commands. For quick cuts and pastes, no
register need be specified. In this case, no appending is allowed, and
the register will be removed if another delete command is given.
(Including x).
For example, to move the previous paragraph, one would go to the top
of the paragraph, type "a13dd, move to the position in which one wishes
to put the paragraph, and then type "ap to put it below the current
line.
Now, presumably you want to be able to cut and paste into areas that
are not just the end of the line. In order to do this, use m<letter>
to mark an area. This letter may be the same as a cut/copy register,
they are stored in different area of memory. Then, type
"<register>`<letter>[yy or dd]. Where <register> is the register to
put the text into, <letter> is the letter used to make, and yy or dd as
appropriate.
(Or, I've just made a mistake, what should I do?)
u will undo the last command. U will undo changes to the current
line. (Granted, a one line dissertation would not be much.) :e! will
reload the current document without saving any changes. In addition,
deleted text gets stored in the registers numbered from 1 to 9. "<n>p
will put the last nth deletion. You can quickly search the registers
by trying one, hitting u, and trying the next. (In order to expedite
this, vi uses . slightly differently than normal. Instead of repeating
the last command, it will try the next register, so all you need to do
is: "1p u . u ., etc. until you undo the delete you want to undo.)
:[m],[n]w <filename> will save between lines m and n to <filename>.
This line numbering method works for almost every : command. If you
use :[m],[n]w >> <filename> it will append it to the file.
The commands that follow a : are commands from the ex editor. These
allow a lot of flexibility and power. For example, there are many
different ways to search and replace, all of with have some
similarities (in fact, they are in some ways the same...)
{HOME}
Send your comments, suggestions, flames, and praise to:
Webmaster@roxanne.org
|