Elementary Vi editing
=============================================================================
Overview: Editing files is how most of us spend our time on a computer
system. "Vi" is one of the commonest and most widely used editors
and this module gives you enough information to get started.
=============================================================================
Section Topics
------- ------
Introduction to vi editing
Setting your terminal type
Your screen
Starting and Stopping Vi
Input and Edit modes
Moving the cursor around
Inserting text
Text changing commands
Searching for text
Replacing text
Introduction to vi editing
--------------------------
Vi is an editor, a program that allows you to view and change the contents
of files. "Vi" stands for "visual editor" since it is a full screen editor
rather than a line editor. Thus, it presents your file on the screen and lets
you move around in the file, much as you are used to with word processors on
Macintoshes or MS-DOS computers.
Several things should be kept in mind when using vi. First, it makes a
copy of your file in the main memory of the computer and does all the changes
there, rather than on the disk itself. Thus, you have to explicitly save the
memory copy back to the disk before you quit or you will lose your changes.
Second, vi is not a word processor. It will not format or "prettify" your
text like Microsoft Word will. Vi's purpose is much more limited -- just to
allow you to change the contents of a file so that you can enter programs,
data, or text.
Third, vi is not the only editor but it is the commonest. There are
versions available for MS-DOS computers and Macintoshes, and it is widely
available on other computers, like the VAX/VMS systems.
Fourth, vi was built on top of an earlier line editor called "ex", so some
of the commands are hold-overs from that editor. This may explain why some
commands seem out of place or inconsistent.
Setting your terminal type
--------------------------
Vi sends special commands to your terminal in order to place the cursor
at specific spots and to do other things. In order to send the right commands,
UNIX has to know what kind of terminal you are using. Here are the two commands
you need to do this:
% set term=vt100
% setenv TERM vt100
Note that TERM is capitalized in the second command. You only need to tell
UNIX once -- it doesn't forget, until you log off. In fact, it may not be
necessary to tell UNIX explicitly if these commands are put into your .login
file. At Canisius, your .login script tries to determine where you are and
what kind of terminal you are using, so it does all this setting for you.
But if something goes wrong, or you are using a weird type of terminal, try
the above commands before seeking human help.
Your screen
-----------
Vi is rather strange in that it doesn't clutter up your screen with a lot
of extraneous information. In fact, all that you see on your screen is just
the contents of the file. You don't get any menus or status lines or command
prompts.
When you are looking at the end of the file, you will see a number of
lines with a single tilde at the beginning. These lines are not in the file --
they are just indicators saying that the file is too short to fit on the screen
so the empty part of the screen, the part that contains no information from
this file, is indicated by these tilde lines.
Even though there is no status line in vi, the bottom line is special as it
is used for various purposes. When you type a colon, vi puts you at the bottom
line and shows a colon. There are a number of special commands that are typed
from this line, and you must always press RETURN after them to activate them.
Other commands use the bottom line even though they do not begin with a
colon. Included is the search command (which is started by a slash).
Starting and Stopping Vi
------------------------
If you want to modify an existing file, just type vi and its name:
% vi bomb.c
Move around in the file, make changes, and then save and quit. To save, type
the following and press RETURN
:wq
The colon is necessary since it tells vi that you are doing a special command
rather than just moving the cursor around. w means "write" and q means "quit".
After this, you will see the shell prompt again.
To create a new file using vi, again follow vi with the name of the new
file you want to create:
% vi junk.c
Insert your text and then do the :wq as above.
If you start to edit an existing file, but discover that you made some
mistakes, then you should stop and throw away the memory copy. Remember, vi
makes a complete copy of the disk file in memory when it starts precisely for
this reason (and also for reasons of speed). To quit without changing the disk
file, use the following command:
:q!
Notice that the "w" is conspicuously absent and the exclamation mark means that
you are not brain-dead and that you know what you are doing. If you were just
to type :q and you had made changes, vi would warn you that you were about to
lose something that may be important. If you hadn't made any changes, :q would
not have warned you and you could have exited vi quickly.
Input and Edit modes
--------------------
Vi is a very portable editor since it does not need function keys. It uses
the letters and numbers of the terminal's keyboard to give editing commands.
But that same keyboard is also used to enter text. In order to distinguish when
the keyboard is functioning as a text input device versus function keys, vi
defines two modes. These are:
1. Insert mode -- the keyboard enters text
2. Edit mode -- the letters of the keyboard are function keys
When you start vi it is always in edit mode. To switch to insert mode, type the
single letter "i". Do NOT press RETURN. In general, vi does not want you to
press RETURN after most commands (except for those that begin with the colon.)
Now type some text. Whatever you type is put into the file and on the
screen. You can enter multiple lines by pressing the RETURN key. If you make
a mistake, you can use the backspace key, but you may NOT use the cursor (arrow)
keys. When you are done inserting text, press the ESC (escape) key.
In summary, to get into insert mode, press the i key. To get into edit
edit mode, press the ESC key. If you press ESC while already in edit mode,
vi beeps at you, but it causes no damage.
Moving the cursor around
------------------------
In the edit mode, you may use the cursor or arrow keys to move around in
the file. If your terminal does not have cursor keys or they do not seem to
work correctly, you may use the letters h, j, k and l.
h -- move cursor left one character on current line
j -- move cursor down one line
k -- move cursor up one line
l -- move cursor right one character on current line
There are many other commands that let you move around in the file, usually
involving combinations of the CONTROL key and letter keys. These are notated
below by using the caret symbol, which means hold down CONTROL while pressing
this key.
^f -- move forward one page
^b -- move backward one page
^u -- move forward 1/2 page
^d -- move backward 1/2 page
To immediately go to the end of the file, type a capital G. Remember that
UNIX is case sensitive, and so is vi. A letter in capital form is usually a
different command than one in lower case. G does not require the CONTROL key.
G -- goto the end of the file
1G -- goto the first line of the file
57G -- goto line 57
Another way to move the cursor around in the file is to press colon and
then type the line number you desire. Then press RETURN and the screen will
change to display that line. You can go to the top of the file quickly by
typing
:1
Likewise you can go to other lines by typing their numbers after the colon.
To find out which line you are on, use
:f
It also tells you the name of the file (in case you've forgotten), how many
lines total there are in the file, and whether you've modified it or not.
Inserting text
--------------
Much of the time you want to insert text. To begin typing on the next
line, type "o" to get into insert mode. "o" stands for "open a line" and if
you are in the middle of existing text, "o" will indeed open up a blank line
and put you in insert mode. You can type as many lines as you like -- subse-
quent text just moves downward to permit you room. When you are done, press
ESC to get back to edit mode.
If you want to insert text in the middle of a line, move your cursor to
a spot and press "i". Whatever you type will be inserted where the cursor is,
shoving the existing text to the right.
Sometimes you want to add text after the last character on the current
line, which is impossible to do with "i". Instead, use "a" for "append".
The single-letter commands
o, i, a
put you into insert mode. The keys that you press will cause characters to
be placed in the file, so a colon (:) is truly a colon, not the beginning of
a vi command. Press ESC to end this literal interpretation of the keys.
Text changing commands
----------------------
Here are some editing commands you will often use:
x -- delete the character which is under the cursor
dd -- delete the line which the cursor is on
dw -- delete the word which the cursor is on
u -- undo the last editing change
Do not press RETURN after you enter any of these commands.
When you delete a line, it goes into a special buffer and disappears from
your screen. You can then tell vi to put this line somewhere else. For
example, after you delete it by using the dd command, move to some other line
and press the p key. This stands for put. You can move many lines at one time
by typing the number of lines to delete before pressing dd. For example, to
delete the line which the cursor is located on and the next four as well (a
total of 5 lines), type
5dd
Then move to the new location and press p.
The p command can be used to copy one or more lines. Just put the cursor
on the first line to copy, and type yy, for yank instead of dd for delete.
This copies the lines to the secret buffer without removing them from the
screen. Then move to the new location and type p. To copy more than one line,
precede yy by a number of lines, making sure that your cursor is on the first
line in the range:
17yy
Then move to the new spot and type "p" to place the copy of the lines there.
Searching for text
------------------
Searching for strings is one of the most common tasks in an editor. To
search in vi, type the slash character, /, which will cause the cursor to
temporarily jump to the command line at the bottom of the screen. Now type the
text you want to search for and press RETURN.
For example, suppose that you want to find the word "UNIX" in a file that
you are editing. Type a slash, and the cursor jumps to the bottom of the
screen.
/UNIX
Type "UNIX" and press RETURN and vi will move to the next line (from where your
cursor was before you typed the slash) which has "UNIX" in it. If no such lines
were found, vi tells you so in a highlighted message at the bottom of the
screen.
The search wraps around to the beginning of the file, so if you type this
search command at the end of the file, it restarts at the beginning. To locate
the next occurrence, just type the slash followed by the RETURN, or just press
the n key.
Vi is case sensitive, so the command o is not the same as O. You should
never use vi with the CAPS LOCK key down. Many of the commands mean something
different in their upper case versions. Often, they mean the same thing as
lower case, except that they mean "do this before the current line" instead of
after it. For example, O opens up a new line in front of the current line,
instead of after it.
Replacing text
--------------
Another command operation which is done in vi is global search and replace.
This is done by using the substitute command on the command line. For example,
to replace all occurrences of the misspelling "computor" by the correct
"computer", type the following:
:%s/computor/computer/g
and press RETURN.
The colon means this is a command line command. The percent sign is
actually a line range. In vi, % stands for every line in the file. If you
wanted to make this change only for lines 10 through 20 inclusive, you would
have typed instead
:10,20s/computor/computer/g
The s stands for substitute, which is the name of the command. The slashes just
separate the command from the words. The old word is placed first, followed by
a slash, followed by the new word. The little g stands for global, and means
that within every line, replace every occurrence. If you do not include the g,
then only the first occurrence is replaced. If you make a mistake or change
your mind, type u for undo and all the substitutes will be undone.