Plan Zero

An introduction to the vi editor

The history of vi

The vi text editor originated at the University of California in Berkeley. Created by Bill Joy in 1976 as a replacement for the frustrating ed line editor, it was designed to be usable over a 300 or 1200 baud modem, and as such quickly evolved into a powerful yet light text editing application, cramming functionality into every keystroke.

In a 1999 interview talking about what inspired him to write vi, Bill Joy explains:

… we got frustrated with the editor we were using which was named ed. ed is certainly frustrating. We got this code from a guy named George Coulouris … called em - Editor for Mortals - since only immortals could use ed to do anything. So we modified em and created en. I don't know if there was an eo or an ep but finally there was ex. [laughter] I remember en but I don't know how it got to ex. So I had a terminal at home and a 300 baud modem so the cursor could move around and I just stayed up all night for a few months and wrote vi.

Why vi?

Despite its age, vi is still an important tool today. This is demonstrated by the fact that every unix/linux distribution released over the last twenty years is likely to have a copy installed by default. Since the original release of vi, many derivatives have been written (such as vim, vile and elvis), with usability and functionality added along the way. Because of this, vi is probably one of the most evolved and stable text editors in the world.

Many of the more recent vi clones are also intelligent enough to know about the type of document you are editing, and will help you out by automatically indenting your text (in the case of HTML or C, for example) or by highlighting the syntax to make your work much clearer. In the great Unix tradition, features that bug you or help you can be turned on and off at your preference, and spending a few minutes customising vi to your liking can save you hours of work in the long run.

Operation modes

Most common text editors have just one mode, and use a menu system or multiple key combinations to allow you to perform functions on your document, such as saving or searching and replacing text. The vi editor has two modes: insert mode, in which text is edited and entered, and command mode, where editing functions and document navigation take place. If this seems strange, don't let it put you off; you'll get used to the concept fairly quickly!

In command mode, you can tell vi to use any number of functions, from pasting text to quitting or opening up a shell. Most commands in vi are short, and many are single letters. Command mode is also where you enter insert mode. In insert mode, you can only enter text. Nothing more, with the exception of using the backspace key to go back and correct mistakes in the newly entered text. In insert mode you cannot copy and paste, search or save your document.

You will find yourself using both modes extensively in vi, and once you get the hang of using the two modes properly, you will see that you can edit documents much faster than you ever could using your current text editor. Personally, I use vi for almost all of my console-based text editing and coding, including to write this tutorial :-)

Using modes and adding text

Okay, enough theory, on to the useful stuff!

When you first fire up vi (by opening up a shell and typing vi), you will be presented with a screen that looks something like this:

~
~
~
~
~
~

Each tilde represents a line with no content. The tildes are used to distinguish between a line with no content whatsoever, and a blank line; i.e. one with a newline character (what you get when you press enter) but no visible text. When vi first starts up, you will be in command mode. It is important to note that commands in vi are case sensitive - a lower case i does not do the same thing as an upper case I at all!

To enter some text into the document, start off by typing i, which will put you into insert mode. Everything that you type from now on will be taken as text to be entered into the document. Try typing a few words and practice using the backspace key to modify and type over some of the text.

From insert mode, pressing the escape key will put you back into command mode. Play around with changing modes a few times until you get the hang of it. Now, to quit vi, make sure that you are in command mode, then type :q (colon q) and press enter. This probably won't quit as expected, but will give you a warning about the document being unsaved. As we don't want to save this document, we can force vi to enact a command without warnings by using the force command. Try typing: :q! (colon q exclamation mark) and pressing enter. The vi editor should now oblige and drop you back to the shell.

Navigating around

For those who are used to using the mouse to navigate around documents, you may find navigation in vi a little tricky at first, however the power of the vi navigation commands far outweigh the advantages of using a mouse. To try out some of the navigation methods, it may be best to open a file which already has some content, or to create one to mess around with. You can open an existing file by running vi <filename>, or just fire up vi, enter insert mode and type a few lines of random words.

The navigation keys in vi are as follows:

Easy to remember, huh? Although most modern clones of vi will allow you to use the arrow keys to move around your document in addition to the keys above, it is always good to