Vi is the standard Unix text editor. This tutorial will gives the basics of using vi.
To invoke vi, type vi filename [ENTER], where "filename" is the name of an existing or new file. If "filename" is an existing file, the screen will show its contents. If "filename" does not already exist, the new file will initially appear on the screen with one tilde (~) on each line in the first column. The tilde is used primarily to fill up the terminal screen after the last line of the file, if there is any existing text. The last line on the screen is reserved for echoing certain commands as they are entered, or for showing informative messages given by vi. See Figure 1.
Figure 1: Vi Editor
The most important thing to know about "vi" is that it is always operating in one of two modes: Insert mode or Command mode. In insert-mode, everything you type will be inserted into the file you are editing, up to the point when you exit insert mode by typing <Esc>. In command mode, you can cut and paste, move the cursor, search, delete lines, delete characters, modify words, etc.
Almost every key on the keyboard (lower-case, upper-case and control-shifted) does something in command mode. So, be careful what you type. It's unlikely you'll ever discover anything useful about "vi" by accident.
A copy of your file is placed into a buffer when you open it in vi. This buffer is where you do your editing. The buffer is not written to disk until you give a write command, which replaces the previous version of the file on the disk or creates the file. While in command mode, the command “:w” will save the current file without exiting vi. There are other commands that can be used to save the file while exiting vi. To write the file to disk and exit vi, enter “ZZ”, “:wq”, or “:x” while in command mode. To quit vi without saving any changes, enter “:q!” in command mode. Remember that commands beginning with a “:” (colon) must be followed by the [ENTER] key.
Since everyone makes typing mistakes (some having disastrous consequences in command mode), the most useful commands are the "undo" commands: "u" and "U" (capital-U). Lower-case "u" will undo the immediately preceding change, no matter how complex or extensive. Note that 2 undo's will undo the undo (nothing changes). The "u" is definitely one of the most useful "vi" commands. As long as your cursor has not left a line of text, uppercase "U" will undo all changes which have been applied to the current line of text, restoring the line to its original state when the cursor entered the line previously.
You need to master a set of basic commands. When "vi" starts up you will find it in command mode.
You do not need to hit the return key for most vi commands. They execute as soon as you enter the command. When "vi" starts up you'll find it in command mode, so your first commands will normally be to move the cursor and start inserting.
The following commands will take you from command mode to input mode.
All the characters you type in will be put into your file, until you hit the <Esc> key.
i Insert text immediately before the cursor. All characters typed after "i" and before <Esc> are inserted in the file.
a Insert text immediately after the cursor. All characters typed after "a" and before <Esc> are inserted in the file.
While inserting, you'll need to type a carriage-return at the end of each line. Otherwise, your text will be one very, very, very long line. You get from input to command mode by hitting the <Esc> key. You can get from command to input several ways, but let's see how you move around.
There are several commands to move around, depending on how far you want to go. Remember, you must be in command mode to issue these. Most commands can be prefixed by a number, n, that specifies that the command should be repeated n times. For example, 5w means move 5 words forward.
ctrl-d Window moves DOWN by half a screen full.
ctrl-u Window moves UP by half a screen full.
ctrl-f Window moves FORWARD by a screen full.
ctrl-b Window moves BACK by a screen full.
k or <up arrow> Moves cursor up one line
j or <down arrow> Moves cursor down one line
l or <right arrow> Moves cursor right one character
h or <left arrow> Moves cursor left one character
<return> Moves cursor to beginning of next line
- (minus) Moves cursor to beginning of previous line
w Moves cursor to beginning of next word
b Moves cursor to beginning of previous word
^ or 0 Moves cursor to beginning of current line
$ Moves cursor to end of current line
nG move to line n
G move to end of file
ctrl-g tells you the line number of the current line
For the searching commands, you have to hit a return to execute the command. Searches are circular. The search starts at the current location and proceeds down or up the file. If the end or beginning of file is hit, the search goes to the beginning or end of file and continues from there. If the search makes it back to the location it started from, it stops and lets you know it didn't find anything. Searches are case sensitive!
/stringy<return> search forward for string “stringy”
?stringy<return> search backward for string “stringy”
If you want to continue searching for the string, you can use the following commands:
n continue search in the same direction as the original search
N continue search in the opposite direction as the original search
Text can be deleted by the character, word, or line. Remember, you must be in command mode to issue these:
x Deletes character under the cursor
dw Deletes a word (including space after it)
d$ or D Deletes from the cursor until the end of line
d^ (d caret) Deletes from the beginning of the line to the space or character to the left of the cursor
dd Deletes the current line
Remember, these and most other commands can take a number as a prefix to perform multiple actions. So, 5dd deletes 5 lines.
When you delete characters, words, or lines, the items are not immediately gone forever. The discarded string is put into a buffer and is available to be placed somewhere else. The buffer remains intact until another item is discarded or copied into the buffer. The buffer also holds text you want to copy to another location. The following commands will place a copy of the item in the buffer, which can then be placed in other locations.
nyy yanks n lines into the buffer starting at the current line
nyw yanks n words into the buffer starting at the current word
(Remember, n represents a number, not the letter "n")
The next two commands take the contents of the buffer and put it in the file.
p puts contents after current line or cursor position
P puts contents before current line or cursor position
Since the deletion commands put the discarded text into the buffer, the p and P commands can be used to place the text elsewhere.
This tutorial should give you enough information to get started. vi has many more features. There are a variety of papers available describing these additional features, and many Unix books cover vi in some depth.
Monday - Friday:
7 am - Midnight
Saturday:
10 am - 7 pm
Sunday:
12 pm - Midnight
Hours subject to change during holidays, emergencies, and summer semester.