oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

A Quick Guide to vi

by Wei-Meng Lee

Jaguar includes a few text editors for your editing work. Of these applications, many experienced Unix users will prefer vi (Visual Editor). While users of GUI systems are more familiar with "user friendly" word processors and text editors, vi is a powerful and feature-rich text editor that is lean and efficient.

However, learning vi requires patience and a little practice; hence, the learning curve is steeper than that of a text editor such as BBEdit. But programmers who regularly use vi swear by it. For some, using vi is a symbol of strength, differentiating the men from the boys.

But what if you need to edit a file quickly in Terminal and don't have the time to learn and practice vi? In this article, I will present a quick guide to getting you started using vi. Do remember though, topics on vi are enough to fill an entire book, and hence this article is just a tip of the iceberg. Hopefully, it will shed some light on this powerful editor and tempt you to learn more.

Starting vi

To use vi, you need to launch Terminal and at the prompt. Type:

vi textfile

When vi is launched, you should see a window such as the following:

Screen shot.
Figure 1. Starting vi.

The first important thing you should know is that vi operates in two modes--Command mode and Insert mode. Use Command mode to issue commands, such as to delete a line or character. To start typing, you need to use Insert mode. When you first run vi, you will be in Command mode.

Navigating the Cursor

While most terminals support the use of the cursor arrow keys to move the on-screen cursor around, you might want to know that you can also use the normal keys to move the cursor. As shown in Figure 2, you can use the h, j, k and l keys to navigate the cursor:

Screen shot.
Figure 2. Navigating the cursor using the h, j, k and l keys.

At first, this key assignment might feel odd. But with some practice, you'll get more comfortable. And best of all, you can navigate the cursor without lifting your hand!

Interestingly, to move the cursor five spaces to the right, you can type 5l. To move down three lines, you type 3j, and so on.

To jump to the beginning of a line, press 0 (numeric zero). To jump to the end of a line, press $.

Let's try to type some text into our file.

Inserting Text

To start typing, we need to go into Insert mode. Press i (to insert a character) and type in the following:

vi is an editor in Unix

Related Reading

Learning Unix for Mac OS X
By Dave Taylor, Brian Jepson

What happens if you make a mistake and want to change it? Chances are, you'll want to position your cursor at the character to be edited, and by doing so, you use your cursor keys and some strange characters appear. If so, do not panic. Simply press the Esc key to return to Command mode. Pressing the Esc key will switch from Insert mode to Command mode.

Appending Text

Now that you have typed in your first sentence, let's edit this sentence. I want to sentence to read: "vi is a powerful editor in Unix". So, position your cursor at "a" (the underline indicates the position of your cursor):

vi is an editor in Unix

Press a (for append) and type " powerful". You should now have:

vi is a powerfuln editor in Unix

So what is the difference between a and i? If you pressed i at "a" and type " powerful" you will get:

vi is powerfulan editor in Unix

See the difference?

To insert a new line, simple press the enter key or use o to insert a new line between two lines.

Deleting Text

To delete a character, position the cursor over the character and press x.

To delete an entire word, say, "powerful", position the cursor over the start of the word and press dw, such as:

vi is a powerful editor in Unix

To delete an entire line, position the cursor on the line that you want to delete and press dd. To delete multiple lines, press dnd, where n is the number of lines to delete. For example, d2d will delete two lines.

Pages: 1, 2

Next Pagearrow