8. The Vi Editor
All Linux configuration files are written in plain English, easy to read and to adapt. You use a text-editor to write or make changes to such files. The two most popular, powerful, and unfortunately "difficult" text editors, both of which are found in every Linux distro, are Vi and Emacs.

Most GUI-based editors, such as Kedit, are easier to manage. But don't make the mistake of thinking that a GUI-based editor is all you need. There are situations that crop up with Linux that require a text-mode editor -- in other words, when you don't have the luxury of accessing a GUI desktop at all. Vi and Emacs are the only tools that come with every Linux distro that work in text mode, so learning one or the other is mandatory. (For Windows and DOS aficionados, think MS-DOS's Edit and Edlin programs.)

Which one's better? Vi and Emacs fans are fond of waging war on that question. In this edition of Linux Explorers, we're picking your text editor for you. This one is all about the Vi text editor, because that's the one we prefer. Vi was originally developed by William Joy at Berkeley University and first officially included in AT&T System 5 Unix. It started out as a line-oriented editor for dumb terminals. The Vi editor, or Vi iMproved (VIM) is an enhancement by Bram Moolenaar; it's the version that people use today.

Getting Started
To start Vi, open a terminal or console and simply type "vi" (without the quotation marks) followed by the name of any existing file or a new file you want to create.

Vi works in two main modes, one for editing text and the other for giving commands. To switch between the two modes you use the I and Esc keys. The program opens in the Command mode, which is used for cursor movements, delete, cut, copy, paste, and saving changes.

The Insert mode is what you'll work in most of the time. You use it to make changes in an open file. Enter the Insert mode by pressing the I key. Newer Vi versions will display the word "INSERT' on the bottom line while you're in Insert mode.

Press the Esc key to switch Vi back to Command mode. As soon as you hit the Esc key the text "INSERT" on the bottom line disappears.

You save your changes to the open file from the Command mode. Press Shift-ZZ to save.

If you make a mistake when saving a file, such as pressing Ctrl-ZZ or closing Vi before saving the file, you'll end up with a swap file (akin to a DOS/Windows temp file) in addition to the original file. Usually the swap file will have the .swp extension. The original file will not contain the recent changes you made; attempting to reopen it will result in an error message.

The swap file is not readable but can be recovered by typing a command like this at the $ prompt and pressing Enter:

vi -r {your file name}

In some extreme cases, recovery is not possible. But in most cases, such as closing Vi before saving, a system crash, or a power failure, recovery works very well. After you recover, you must manually delete the swap file using a command like this at the $ prompt:

rm .{your file name}.swp

12-Step Vi Program
Once you get used to it, using Vi is as easy as eating French fries. Try this exercise to get started. (Please follow all the instructions to the end.)

1. Make a new file called "tessst" by opening a console and typing this line after the $ prompt and press Enter:

vi tessst

2. You'll get an empty console screen since Vi will start with the empty new file. Remember, Vi always starts Command mode, so press the I key to enter the Insert mode. If you're ever not sure whether you're in Insert mode, you can always just hit I again.

3. Next, type this line:

"The quick brown fox."

4. Press the Esc key to return to the Command mode.

5. Save the file by holding down the Shift key and pressing the Z key twice: ZZ.

6. Vi should close and you should see your $ prompt back in the console.

7. Check to see if your additions to the file were correctly saved. At the $ prompt, type:

cat tessst

The Cat command lists the file's contents. You should see:

"The quick brown fox" line, exactly as you typed it earlier.

8. Next, reopen the file you created. At the $ prompt, type this and press Enter:

vi tessst

This time Vi opens up to the text in your file, not a blank screen.

9. Put Vi back in Insert mode by pressing I and add another line of text:

"The sleepy dog did not notice the clever fox."

10. Now save the file again by pressing Esc to enter the Command mode and typing Shift-ZZ.

11. Have a look at your changes again by using the Cat command at the $ prompt:

cat tessst

12. Repeat the entire process again, adding yet another line to your file. When you've done that, review the file contents of your home directory by typing this at the $ prompt and pressing Enter:

ls -a

There should be only one file called "tessst." If there's more than one, no matter what the extension, you made a mistake somewhere along the way. Delete all the tessst files repeat the steps from Step 1. This command will delete swap files you may have created:

rm .tessst.swp

If you made no mistakes, go have some French fries.

Common Vi Commands

Press Key(s):*   Function:
IInsert text before the cursor
AInsert text after the cursor
:Switch to ex mode
$Go to last place on the line
^Go to first place on the line
WNext word
BPrevious word
Shift-GLast line of the file
20 Shift-GGo to line 20
YCopy. (Note: Y3W = copy 3 words; Y3J = copy 4 lines.)
XDelete character under the cursor

* Note: Only capitalize letters preceded by "Shift-".

Related Links
For additional information on Vi, see:

  • Scot's Newsletter Forums, All Things Linux - Tweaking Vi
  • William Joy's An Introduction to Display Editing with Vi
  • Marisa Luvisetto's Introduction to Vi
  • Thomer M. Gil's Vi Lovers Home Page
  • Paul Sheer's Vi
  • Dan Kegel's The Vi Text Editor

    Disclaimer: To those who know their way around Vi, yes we're aware of "Ex mode," with its roughly 10 pages of additional commands. We're just trying to keep things simple. The related links will help readers explore Ex mode if needed.