Editing files

A quick tour of vi

vi is the standard UNIX system tool for editing text. There are a few simple commands that you need to learn before you can use vi. These commands allow you to:

The following example uses vi to enter and correct the first five lines of the poem ``Kubla Khan'' by Samuel Taylor Coleridge. Commands are shown at the left-hand side and described at the right:

vi kubla_khan
Start editing a new file called kubla_khan. vi displays a nearly empty screen with the cursor in the top left-hand corner. See ``Starting vi'' for more details about the options available when starting vi.

Switch to insertion mode so that you can start to enter text. See ``Entering text'' for more information about commands that you can use to enter text.

Type the following lines of text, pressing <Enter> to go to a new line. If you type a wrong letter, either leave it for correction later or press <Bksp> to move the cursor over it and enter the correct character:
In Xanadu did Kubla Khan
A stately pleasure-dome decree:
Where Beta, the sacred river, ran
Through caverns measureless to man
Down to a sunless sea.

Return to command mode.

Search for the word ``Beta''. The cursor is placed under the ``B''. See ``Searching for text'' for more information.

Delete the word ``Beta''. Each time you press ``x'', vi deletes the character under the cursor. See ``Deleting and restoring text'' for details of other commands that remove text. ``Using buffers to cut and paste text'' describes more sophisticated techniques that you can use to move blocks of text in vi.

Insert the word ``Alph'' and return to command mode. If you make a mistake while entering ``Alph'', press <Esc> to return to command mode and press u to undo the change.
Pressing u again undoes the undo. You should also refer to ``Replacing and modifying text'' for information about replacing a single text string, and ``Substituting text'' for details of how to search for and substitute several instances of a text string.

Save the changes you have made to the file kubla_khan. You should do this fairly frequently while you are editing to protect your work against accidental loss. See ``Saving files and quitting vi'' for more information about saving to files.

Move to the previous line. This is one of the cursor movement commands available in command mode. The effect of pressing ``k'' is immediate; you do not need to press <Enter>. See ``Moving around a file'' for more details.

The edited file should now look like the following:
In Xanadu did Kubla Khan
A stately pleasure-dome decree:
Where Alph, the sacred river, ran
Through caverns measureless to man
Down to a sunless sea.

Save the changes to kubla_khan and quit vi.
vi provides many more commands, shortcuts, and other features that you can use. The following sections cover these aspects of vi in greater detail.

Next topic: Starting vi
Previous topic: Editing files

© 2003 Caldera International, Inc. All rights reserved.
SCO OpenServer Release 5.0.7 -- 11 February 2003