The VI Editor
This is an introduction to vi, a text editor that is available on almost all
Unix machines. This is what you use to create a file or to change the
contents of a file. This is only an introduction; if you have the basics
and want to learn more, look at one of the vi summary sheets in the handouts
part of the bookcase in the lab. We also have a vilearn command on
the lab systems that is a good tutorial covering both basics and advanced
features.
To start up vi, use the command 'vi filename', where filename is
the name of
the file that you want to change. If you want to create a new file, you can
just say 'vi newfilename', where newfilename is the name that you
want to give the file.
If you get mail or something else messes up the screen, you can press
[ESC]
and then ^l (control-L) to redraw the screen. This will not affect
your file, only your display.
The tricky part about vi is that it has two modes, insert mode and command
mode. When you are in insert mode, all you can do is type text, which will
go directly into the file wherever your cursor is. When you are in command
mode, you can do everything else, including moving the cursor, searching,
and setting options.
To move from insert mode to command mode, use the [ESC] key. If
you forget
to do this and end up with a ':wq' or something else that you don't
want
at the end of your line, just backspace over the characters that you don't
want, then hit [ESC]. If you hit [ESC] when you are
already in command mode,
the terminal will beep at you, but it won't do anything to your file. When
in doubt, hit [ESC].
To move from command mode to insert mode, use the i, a, o, or O command
as described below. There are a few others, but those are the most common.
To use the following commands, you must be in command mode:
Cursor Movement:
A handy thing about these commands is that you can type a number first, and
the editor will do the command that many times. For instance, h moves the
cursor one character to the left, and 12h moves the cursor twelve characters
to the left. You shouldn't see the number or command that you type, by the
way. If you do, you are in insert mode; you should backspace over the number
and press the [ESC] key, then try again.
- h - moves cursor one character to the left
- j - moves cursor one line down
- k - moves cursor one line up
- l - moves cursor one character to the right
- ^f - moves cursor one screen forward
- ^d - moves cursor a half screen down
- ^b - moves cursor one screen backward
- ^u - moves cursor a half screen up
- ^ - moves cursor to the beginning of the line
- $ - moves cursor to the end of a line
- w - moves cursor one word forward, with punctuation and braces as new words
- W - moves cursor forward to the next word
- b - moves cursor one word backward, with punctuation and braces as new words
- B - moves cursor backward to the next word
- /pattern - searches for pattern and moves the cursor there
- ?pattern - searches backwards for pattern and moves the cursor there
Deleting Text:
- x - deletes the character the cursor is on
- X - deletes the character to the left of the cursor
- dd - deletes the entire line the cursor is on
d followed by a cursor movement command deletes that much text. For instance,
w moves the cursor forward a word, and dw deletes to the end of the word.
The 5h command moves the cursor five characters to the left, and the d5h
command deletes five characters to the left.
Inserting Text:
These commands may seem a little confusing at first. The i command means that
everything you type until you hit [ESC] will be inserted to the left of the
cursor. In other words, this command leaves you in insert mode with the
insertion point to the left of where the cursor was when you hit 'i.'
- i - inserts text to the left of the cursor (leaves you in insert mode)
- a - appends text to the right of the cursor (leaves you in insert mode)
- A - appends text at the end of the line (leaves you in insert mode)
- o - opens new line under the line the cursor is on (leaves you in insert mode)
- O - opens new line above the line the cursor is on (leaves you in insert mode)
How Do I Get Out Of This Thing, Anyway?
When you hit the colon (:), you will see it at the bottom of the screen (unless
you are still in insert mode, in which case you should back up over it and
press [ESC], then try again). You will see anything you type after the colon
at the bottom of the screen. After commands that use a colon, you have
to hit the key.
- :q! - exits without saving changes
- :w - write changes
- :wq - write changes, then quit
Line Number Commands
(These are useful for programmers)
- ^g - shows what line you are on at the bottom of the