A General Guide to the Vi
Editor
The Vi
(pronounced vee-eye) editor is a full screen editor
designed for use in developing software. It's an older program, written in the
late 70's, which means it's not based on the GUI interfaces popular today.
Instead, you use keystrokes to perform commands. This may take some getting
used to, but you'll find it's pretty quick once you have some practice.
Students typically resist learning Vi because it’s a hassle to learn all of the commands at once, and using a simpler Unix text-editor like (god forbid!) Pico is easier. For this reason, this guide starts with a rock-bottom intro to Vi, showing you just the commands needed for basic editing, and then follows with a more complete intro, which you may pursue after getting the basics.
To start the Vi editor from the Unix system, type vi <filename> in response to the Unix prompt, where <filename> is the name of the file you want to edit. If you give the name of a non-existent file, Vi will create the file for you as a new, empty file. Once you do this command, you are “in” VI and the Unix commands no longer apply. The Vi program has its own set of commands and behaviors, and those will be in force for as long as Vi runs. You return to Unix by ending or “exiting” the Vi program (see below). You will then be “back in the shell” – the Unix program that supplies the Unix command interface.
This paper assumes that you have a file to play with as a sample. If you do not, then start with a new file instead and do the following steps immediately after entering Vi.
1. Hit the a key.
2. Type several (at least 5) full lines of text. You will find that whatever you type is added to the file, verbatim. Five lines wont be enough for some commands below to work well, so it is recommended that you type around 50 more short lines. Typing the first paragraph of this document, one word to a line, would be one possibility.
3. Hit the escape key (marked with the letters esc).
In either event, you should
now be in Vi with some sample lines to toy with. Do not
touch any more keys. Read below
first.
The first thing you may
notice is that Vi gives you no prompt. Instead, most
of the keys on the board have a command attached to them; hitting the
appropriate key causes Vi to take some action. This is why you don’t want to touch any keys
until you know a little more. Modifications
to the file typically take place at the point marked by the cursor (blinking
white box). Thus if you were to tell Vi to remove a line from the file or
insert a word, it would do so at the present location of the cursor. For this
reason, a great number of commands in Vi deal with moving the cursor about on
the screen. In general, making alterations or additions to a file involves
first moving the cursor to where you want to make the change, and then telling Vi what change to make.
Very Basic Vi
Basic Movement
Let’s start with basic movement commands. Try each one out to be sure of its usage. The notation ^x, where x is any letter, means control-x. The current line is whatever line the cursor is on.
return and -
Hitting the return key moves the cursor to the beginning of the next line. Hitting the - key moves it to the beginning of the line above. Note that if you try to move the cursor below the last line of your file or above the first line, you will get a beep.
h, j, k, l, space
Although they are not as useful as you might think, the h, j, k, and l (letter ell) keys will move the cursor one space left, down, up, and right, respectively. Remembering that the h key goes left and the l goes right is easy because of their position on the board. Remembering which of the j and k keys goes up and which goes down is another matter. The best mnemonic Ive got is that k stands for klimb as in klimbing up a tree. If you can think of something less silly please let me know. The up, down, left, and right arrows on your keyboard will also work.
Hitting the space bar is a
synonym for hitting l. You move right one space.
Basic Editing
x and dd
The x key deletes whatever
character the cursor is on. The dd (hit d twice) command deletes the line the cursor is on.
i
This is the first command for
entering text, and the only one we’ll discuss in the basic section, but it
brings up a critical point about Vi. Vi is a modal program, meaning it can be in one
of two “modes”. Vi
starts in command mode, in which
keystrokes cause actions. Certain
keystrokes, including “i”, put Vi
into an insert mode in which
everything you type goes into the file, and is not interpreted as a
command. You must keep track of which mode you’re in. Otherwise, you’ll shift from typing text to
typing commands, and end up with file content like “Hello, this is a testhhhhhjjkkxx”. If
you’re in insert mode and want to return to command mode, hit the Escape (Esc)
key. Then do your commands, and return
back to insert mode with the “i” command, or others
we’ll introduce below. A Vi session is a series of switches back and forth from
command mode to insert mode and back to command mode.
The i command
inserts any text you type immediately to the left of the cursor. You may add new lines by hitting return in
insert mode, and may erase typed characters within the current line by using
backspace. But any more sophisticated
editing requires a return to command mode.
To exit Vi, hit ZZ. This will save all the work you’ve done under the filename you started with when you typed vi filename.
And
that’s about it. With these basic
movement and editing commands, you can get simple text editing jobs done in Vi. But, you’ll want
to learn the other 100 or so commands to get full value out of Vi, so once you’re
bored with these commands, move on to the more advanced set below.
More Powerful Vi
As
before, please try each of these commands as you read – they’ll stick better in
your mind that way. And note that there
is a myriad of Vi “cheat sheets” that list all the Vi
commands in shorthand form. Just type “vi
cheat sheet” at Google – as of this writing that search generates over half a
million hits.
More Powerful Movement
$, 0, w, and b
Hitting $ moves the cursor to the end of the current line. Hitting 0 (zero) moves it to the beginning of the line. Hitting w moves you forward one word. Hitting b will move you back one word.
^d (control-d) and ^u (control-u)
Moving around with the return and the - keys can be tedious in a large file. Hitting ^d will cause the display to scroll down about 12 lines (1/2 the height of the screen). ^u does the same in the upward direction. The ^u and ^d are mnemonics for up and down, of course. You may find it difficult to test these two commands and the next two unless you have a reasonable number of lines in your file. If there are insufficient lines the ^d and ^u will simply move the cursor up and down with scrolling the display.
^f and ^b
These are just like ^d and ^u, except that ^f moves you forward an entire screen and ^b moves you back an entire screen. The mnemonics here are forward and back. Again, these behave a little oddly on very small files.
/, ?,
and n
Moving up and down is fine, but if you want to jump the cursor to a particular word or pattern of characters, like a “find” command in a modern word processor, you use the / and ? keys. When you hit the / key, the cursor moves to the bottom of the screen and shows a / as a prompt. Type the pattern of letters or word you want to look for and hit return. Vi will look for your pattern from the point where the cursor was when you hit /. It will look in a forward direction until it finds the pattern. If it reaches the end of the file without finding the pattern it will cycle back to the top and continue looking until it reaches its original position (the point where you started all this by typing /). If it doesn’t find the pattern, you get an error message; otherwise the cursor stops where it found the pattern. The ? works the same way as the /, but searches upward instead of downward. In either case, if you just did a search for a word of pattern and want to repeat the same search, hit the n key to find the next occurrence of the pattern.
These keys also introduce
another behavior-pattern in Vi. Some keystrokes cause the cursor to drop to
the bottom of the screen, where you enter other data, like a search pattern,
before proceeding.
Those used to an editor that works with line numbers will like the G command. Vi shows no line numbers on the screen, but it still views the lines as numbered from the top down, starting with 1. To jump to a line by line number you first type the number of the line you want to go to. The number you type will not appear on the screen. Then hit G (note the capital) and the cursor will move to the line whose number you typed. Don’t hit return while using this command.
To go to the bottom line (usually an unknown line number) type G without typing a number first. To find out the current line and position in that line, type ^g. The line and column number appear at the bottom of the screen.
These two commands work like the / and ? commands above, but only within the current line and only for one-letter search patterns.. You can search for a particular letter on the current line by hitting f and then the letter. The f and the letter will not appear on the screen, but the cursor will move to the next occurrence of the letter on the current line. The mnemonic here is find. f only looks to the right of the cursor. If the cursor is in the middle of the line and you want to look to the left, use F, which works just like f but in the opposite direction. Remember that these two find commands are limited to the current line; they will not look on other lines for the letter in question. If the letter cannot be found on the current line, you get a beep.
The pattern of using the capitalization of a command to signify the reverse of the command (F vs. f, for instance) is another common pattern in Vi. Note, for instance, that ? is the capital of /.
As with the / and ?, you can repeat a find command quickly. Hit the ; key to repeat the most recent find command.
More Powerful Editing
Typing o (for open) opens up
a new blank line below the line you are currently on. The cursor moves to the
beginning of this blank line and you are in insert mode. You can type several
new lines if you like; just hit return while you’re typing and the lines below
you will move down to make room. Return
to command mode via the Esc key, as with the “i”
command above.
The O command is just like the o command, but it opens a blank line above the current line instead of below. Here again, the capital letter does the reverse of the lower case.
The a command is identical to the “i”
command from above, but inserts text after (to the right of) the cursor, not
before it. The A command acts much like “a”
except that it moves the cursor to the end of the line and lets you insert text
there.
dd
The basic editing section showed you that hitting the d twice (dd) will cause the current line to be deleted. You can precede the dd with a number and that number of lines from the current line downward will be deleted. As usual, neither the dd nor the number will appear on the screen, but the action will be taken. You can also delete sections of a single line by using the d followed by something to describe what you want deleted. For instance dw will delete the next word after the cursor on the present line. Following the d with fx, where x is any letter you want, will delete up to the next occurrence of x. In general, following d with a symbol that would normally move you in the line will delete that part of the line instead. For instance, fx will move you to the next occurrence of x; so dfx will delete up to that point. You can get quite creative with this. d3w, for instance, will delete 3 words, as you might expect. d$ will delete the rest of the line. (Recall that $ alone will move you to the end of the line.) This sort of thing also works with other commands that we will come to.
And the ability to add a repeat count ahead of a command, causing
the command to repeat that number of times, is yet another Vi
pattern, which shows up on a number of commands. For instance, 5x
deletes 5 letters.
At this point it might be nice to introduce a couple of useful commands for undoing accidental deletes. u will undo the last change you made to the file, either an insert or a delete. Thus, if you type 100dd and 100 lines of your file vanish, u will make them reappear just as they were. If you make an insertion you dont like, just hit u after youre done and it will vanish. u stands for undo in case you havent guessed. If you hit u and undo something and then immediately hit u again, you will undo the undo (!?) and be left with what you had to begin with. Sadly, Vi lacks the infinite-depth undo features of modern word-processors. It was designed when 100K was a huge amount of RAM, and one couldn’t afford the large data needed for infinite-depth undos.
Hitting p will put whatever you last deleted back in the file at the point where the cursor presently is. If you delete 10 lines (10dd) and then move the cursor elsewhere and hit p, your 10 lines will reappear right below the cursor. This is how you move large sections of text in Vi.
As described earlier, x deletes the letter at the cursor. You can precede it with a number to delete several letters. 5x will delete 5 letters. r will replace the letter at the cursor. You hit r and then the letter you want instead. rx, for instance will replace the letter at the cursor with an x. These are both useful for small typographical mistakes. Finally, c will replace an entire section of the current line. You tell it what you want to replace in exactly the same way you tell d what to delete. cw will replace the next work, cfx will replace up to the next occurrence of x, etc. When you do a c command, the end of the section you have designated is marked off with a $ to show you what will be changed. At this point you will be in insert mode automatically and can type whatever you want to have in place of the section being changed. Hit escape when you are done typing the replacement. The replacement does not have to be the same length as what was replaced. You can, for instance, type cw to replace a word and then type three words as a replacement, hitting escape when youre done. Vi will make room for the longer replacement.
Two miscellaneous commands, here. J will join the current line and the next line together into one large line. (To break a line, you move the cursor to where you want to break, hit a and insert a return.) The Y command is used to copy lines. Y will yank the current line, meaning that the line will be left just as it is, but the p command will put the line back wherever you like, just as if the line were the most recent deletion. You can precede the Y by a number to yank several lines. For instance, to copy 5 lines, move the cursor to the top line of the five, hit 5Y, move the cursor to wherever you want the copy, and hit p.
More options on leaving Vi
If you dont want to save your work, hit : (this will put a : prompt at the bottom of the screen) and then type q! followed by a return. To write to a different filename hit :, then w filename and return. In this case, leave with the :q!, not ZZ, or Vi will also write to the original filename.
Learning Even More Vi
We’ve covered many of the
most useful Vi commands. Further exploration is best done by web
searches (there’s a ton of Vi information out there)
and by perusing a cheat sheet to find out about all the commands not covered
here.