Chapter 15. Editing and Viewing Files and Strings

15.1 The vi File Editor

The vi editor is a full-screen file editor that was developed from the ex editor. It is also common to all versions of UNIX. The vedit command starts a version of the vi editor intended for beginners. In the vedit editor, the report option is set to 1, the showmode option is set, and the novice option is set, making it a line editor.

15.1.1 vi Editor Limitations

The following list provides the maximum limits of the vi editor. These counts assume single-byte characters.

Running the vi editor on a file larger than 64 MB may cause the following error message to display:

     0602-103 file too large to place in /tmp

15.1.2 vi Editing Modes

The vi editor operates in the following modes:

command mode
When you start the vi editor, it is in command mode. You can enter any subcommand except those designated for use only in the text input mode. The vi editor returns to command mode when subcommands and other modes end. Press the Esc key to cancel a subcommand.
text-input mode
You use the vi editor in this mode to add text. Enter text input mode with any of the following subcommands: a, A, i, I, o, O, cx (where the x represents the scope of the subcommand), C, s, S, and R . After entering one of these subcommands, you can enter text into the editing buffer. To return to command mode, press the Esc key for normal exit or press Interrupt (the Ctrl-C key sequence) to end abnormally.
last-line mode
Subcommands with the prefix : (colon), / (slash), ? (question mark), ! (exclamation point), or !! (two exclamation points) read input on a line displayed at the bottom of the screen. When you enter the initial character, the vi editor places the cursor at the bottom of the screen, where you enter the remaining characters of the command. Press the Enter key to run the subcommand, or press Interrupt (the Ctrl-C key sequence) to cancel it. When the !! prefix is used, the cursor moves only after both exclamation points are entered. When you use the : prefix to enter the last-line mode, the vi editor gives special meaning to the following characters when they are used before commands that specify counts:
%
All lines regardless of cursor position
$
Last line
.
Current line

15.1.3 The vi Command Flags

Table 23 shows the flags that can be used with the vi command.


Table 23: The vi Editor Flags

15.1.4 vi Editor Subcommands

The list of subcommands is very extensive, so you are only going to learn some of the more common subcommands used. You would use the subcommands to perform these kinds of actions:

Enter the following subcommands in command mode. You can cancel an incomplete command by pressing the Esc key.

15.1.4.1 Moving the Cursor

Use subcommands to move the cursor within a file in these ways:

15.1.4.2 Editing Text

The subcommands for editing enable you to perform the following tasks:

15.1.4.3 Manipulating Files

The subcommands for manipulating files allow you to do the tasks outlined in the following sections:

15.1.4.4 Other Actions

The vi editor provides the subcommands described in the following sections:

15.1.4.5 Other Actions

To set the vi editor up as your default editor, use the EDITOR command. The EDITOR variable can also be put into the user .profile or the /etc/profile file.

# EDITOR=vi

Note

The EDITOR command sets a variable and must not be confused with the set -o vi command that sets up command line editing.

15.2 Editing a File Using the vi Editor