The VI Editor
from www.cs.colorado.edu
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 line
exercises
#1
login as root
pwd
should be in /root
lets create a .forward file...
type vi
i
to insert
enter email address you want to forward to root@t1-noc.t1.ws.afnog.org
hit escape
to exit from insert mode
enter :wq .forward
to write the file out as .forward and exit
#2
lets add a second email address to the .forward
file
vi .forward
$
to move to end of line
a
to start inserting text to the right of the cursor
hit enter
enter email address root@noc.ws.afnog.org
hit escape
enter:wq
to write and quit
#3
Remove the .forward file
rm .forward
joelja@darkwing.uoregon.edu