Section 10: Editing Text with vi

One of the most basic operations you will need to perform on a UNIX system is text editing. Whether you are preparing a document, writing a program, or sending email to a colleague, you will need a utility to allow you to enter and edit text.

There are many editors available for UNIX systems, but this discussion will focus on the vi text editing program because of its power, flexibility, extensibility, customizability, and prevalence. No matter where you get a UNIX account, you are likely to have vi at your disposal. Here is an index to the topics in this section:

General features of the vi editor

Vi is a visual editor. That means that you have a representation of your entire document on your screen, and you can move around freely, editing any part of the document you wish. Older editors, referred to as line editors, required all changes to the file to be made on a line-by-line basis. Each command to a line editor specified a line number, and the changes to be applied to that line. Line editors are truly horrible things, and you should feel lucky if you have never seen one.

Vi uses control and escape characters as well as text characters in commands. Thus, to distinguish text commands from text to be inserted in the buffer we will use bold lettering as in j. In this document, the notation "Control-X" means to hold down the control key, and type the letter x. You don't need to capitalize the x, or any other control character, by holding down the shift key.

Working with buffers

When you edit a file in vi, you're not really editing the file itself, as it sits out on a disk somewhere. Instead, vi makes a copy of the file, and stores the copy in a part of RAM memory called a buffer. All the changes you make to the file are applied to the buffer. When you save the file, vi writes the contents of the buffer to the disk.

Because the buffer exists in RAM memory, it disappears if the power is turned off, or if the system crashes. Thus, you should use the save command often, flushing your current buffer to disk. Once the file is on disk, a power outage or system crash shouldn't harm it.

Basic operations in vi

Here are some of the fundamental things you'll need to do when you edit a document in vi.

Starting vi

To start vi, just type the command

vi

to the UNIX shell. If you want vi to start with a file already loaded into a buffer, type

vi <filename>

where <filename> is the name of the file you want to edit.

Quitting vi

To exit vi and return to the UNIX shell, type :q. If you have made changes to the buffer since the last time you saved it to disk, vi will say "No write since last change (":quit!" overrides)". Type :q! to exit without saving or :wq to exit with saving.

Getting help

vi does not have an on-line help system.You can temporarily exit to the shell and see the man page by typing the command :!man vi.

The vi display

The display in vi is divided into two basic areas. The top area is called the text window. The text window takes up most of the screen, and is where the document being edited appears. At the bottom of the text window, there is a single last line mode line. The last line mode line allows you to see a command prefixed by : / ? or ! as you type it and terminate it with your Enter key.

Aborting a command

You can abort any last line mode command by typing Control-U or whatever is defined as kill in your stty settings..

Working with files

To read a disk file into an vi buffer, type the command :r <filename>. When you have entered the file name, press the return key, and vi will load the file into a buffer, and display it in the text window.

The command to save the contents of the buffer to a disk file is :w. The save command overwrites the old version of the file. You may also write the contents of the buffer to a different file with the command :w <filename>.

To create a new file, enter vi at the UNIX prompt and vi will display an empty buffer for you to insert text in.

Cursor motion

On well-configured systems, you will find that the keyboard arrow keys will function correctly in vi, moving you forward or backward one character at a time, and up or down one line at a time. If the arrow keys do not work, here's how to accomplish the same functions:

In addition to basic cursor motion, vi provides some other handy cursor motion functions:

Here are some ways to search for a text string which will reposition the cursor. Wild cards include . (period) to match a single character and * to match any string:

Command marked with asterisk (*) can also be used with the more utility to view files.

Inserting and deleting text

Here are some ways to delete text. All of these commands put the editor in insert mode which is terminated with the Escape key:

If you want to insert the contents of another file into the current buffer, place the cursor at the desired insertion point, and type :r <filename>.

You may also insert text by cutting it from one place, and pasting it at the insertion point. See the next section for information on cutting and pasting.

Deleting text is easy but neither the Backspace nor Delete key will delete a character. Here are some ways to delete text:

Cutting and pasting text regions

vi allows you to select a region of text, and perform cut and paste operations on the region. It uses a temporary storage area called the unnamed buffer to allow you to store and retrieve blocks of text. There is only one unnamed buffer in vi, which means that you can cut text from one document, and paste it into another.

To cut a region of text, and place it in the unnamed buffer, use a single delete command.

The paste command is p. It takes the block of text from the unnamed buffer, and copies it where the cursor rests. The p command only retrieves the most recently-cut block of text.

You may copy a region of text into the unnamed buffer without cutting it. Here are some ways to copy text using y (yank) commands:

Changing text

Here are some ways to replace text using c (change) and r (replace) commands:

Undoing changes

It is possible to undo the change you have made to a file by entering the command u.