UNIX Unbounded: A Beginning Approach

Chapter 4: The vi Editor: First Look


Objectives:

This chapter introduces the vi text editing program. The objectives important to this chapter are:

  • modes of the vi editor
  • memory buffers
  • editing files
  • saving files
  • quitting vi
Concepts:

The chapter begins with background on different types of editors. It divides the field into two types:

  • Line editors - these apply changes to one line of text at a time
  • Screen editors - these allow a user to move the cursor about on the page and change anything in any order
The main reason for mentioning this is to show that vi is a screen editor, and more friendly than its ancestors. It is called vi because it is meant to be more visually oriented than older editors.

The book then tells us that vi is available in two versions:

  • the view editor - this allows reading files, but not changing them
  • the vedit editor - this one lets you change files
Which one do you think we need to actually write files?

When editing files with vi, you should be aware which of its two modes you are in:

  • the command mode - this mode allows you to issue commands to vi, like save and quit
  • the text entry mode - this mode allows you to work with the contents of the file
The problem is that vi does NOT tell you visually which mode you are in. (Well, I guess they couldn't think of everything.) It is in the Command mode when it starts. Let's follow the author's flow.

Basic Editing Tasks

  • Creating or opening a file
  • Entering text
  • Deleting text
  • Searching for text
  • Changing text
  • Saving the file
  • Quitting vi

You can start vi two ways. Just enter

vi
alone on the command line to start it with a blank file. Enter "vi" followed by a space and the name of a file to open that file (or create it, if it does not exist).
vi filename

To enter the Input mode, press the letter i in the Command mode. (Remember, vi starts in the command mode.)

Use the Enter key at the end of lines in vi. vi does not word wrap.

To save a file in vi, switch to the Command mode by pressing the Escape key. You can press it a number of times to be sure. If the computer beeps when Escape is pressed, you are in the Command mode. Now comes the fun part. Some vi commands (not all) start with a colon. The save command is one of them. Type the colon, and follow it with the letter w (for write) and, if you want to quit vi, the letter q. Either of these commands may be issued alone. Doing them together is a time saver when you are done word processing, but the :w command alone is better if you just want to save every few minutes.

There is a chart of vi single-key commands in section 4.3.2 which you should practice using. It is recommended that the student will benefit from carrying out the instructions on the next several pages. Note that on some UNIX systems the arrow keys will not work in vi. An odd feature for a screen editor, but not all keyboards had arrow keys when vi was written. To move the cursor, enter the Command mode, and press h for left, j for down, k for up and l for right.

There are several deletion-related commands in the Command mode. See Table 4-4. Noteworthy ones:

  • x - deletes the character the cursor is under
  • dd - yes, twice. deletes the line the cursor is in
  • r - puts cursor in overstrike mode for one character
  • R - overstrike, and enter Text entry mode
You may forward search in vi using the / key, and backward search using the ? key. Press the key for the direction you wish to search from the current cursor position, then type the text to search for, then press Enter. The cursor moves to the first occurrence of the search text, and may be moved to the next occurrence of it by pressing the letter n.

Quitting vi is not difficult. As noted above, you can enter the Command mode and issue the :q command. You can also use:

  • :wq - this writes all changes and quits
  • ZZ - this is the same as :wq, but does not need the colon or the Enter key
  • :q! - this one quits and saves nothing changed since the last save

A memory buffer is a temporary space in memory for working storage. Vi opens such a buffer and stores your file in it while vi is running. Changes all go into the buffer, and not to disk, until a write command is issued.