Basic Editing with VI

Vi (VIsual editor) is a full-screen text editor, meaning that it allows you to see an entire screen of text rather than just the current line. It has three modes of operation: insert, edit and command. Insert mode allows you to enter text or data into a file.

Edit mode allows you to position the cursor anywhere on the screen to manipulate text.

Command mode allows you to enter complex commands such as those that affect sections of text.

Starting Vi

To start Vi and open a file, at the indyunix$ prompt, type: vi filename

Press ENTER

If you supply:

An existing filename - vi opens the existing file.

A new filename - vi opens an empty file.

For example, to open an existing file named course.stats, at the prompt, type: vi course.stats
To begin a new file named update.test.scores, at the prompt, type: vi update.test.scores

NOTE: UNIX is case sensitive. Therefore, course.stats and COURSE.STATS represent two distinct, different files.

Inserting Text

When you open a file, vi is in the edit mode and must be put into insert mode to type text.

To switch to insert mode, press:

i (to insert text in front of the cursor location)
a (to insert text after the cursor location)
SHIFT-A (to insert text at the end of the current line)

Type the text or data. NOTE: You must press ENTER at the end of each line.

Correcting Errors

If you have not yet pressed ENTER and notice a typing mistake on the current line, press BACKSPACE as many times as necessary to move to the mistake. Retype from the correction to the end of the line.

If you have already pressed ENTER, you cannot move to correct the error while in insert mode. You must change to edit mode, then use the movement commands to move to the correction area (see Moving the Cursor ).

Switching Modes

To switch from insert to edit mode, press ESC

To switch from edit to insert mode, see the commands in Inserting Text.

Moving the Cursor

NOTE: You must be in edit mode in order to move the cursor.

The following identifies the movement key(s) and the direction of the move:

Key/Direction

h or LEFT ARROW to the left one character

l or RIGHT ARROW to the right one character

j or DOWN ARROW down one row

k or UP ARROW up one row

( to the first character of the current sentence

) to the last character of the current sentence

Deleting Text

NOTE: You must be in edit mode in order to delete text.

If you have already pressed ENTER to end a line, you must use the movement commands to move the cursor to the location of the text to be deleted.

To delete individual characters:

Move to the place where you want the deletion to start.

Press x (Pressing x may be preceded by pressing a number key to indicate multiple consecutive characters.)

For example, to delete one character, press x To delete three consecutive characters, press x x x or 3 x

To delete complete lines:

Move to the line you want to delete.

Press d d (Pressing dd may be preceeded by pressing a number key to indicate multiple consecutive lines.)

For example, to delete one line, press d d To delete three consecutive lines, press d d d d d d or 3 d d

Replacing Incorrect Text

Follow the steps for deleting text.

Change to insert mode (seeInserting Text ).

Type the new text.

Saving a File

NOTE: To save a file, you must be in edit mode.

To save and exit a file, hold SHIFT and press Z

To save a file and continue working in the file:

Press SHIFT-:

Press w

Press ENTER

To save a file under a different name:

Press SHIFT

Press w

Press SPACEBAR

Type: new filename

Press ENTER

You may receive a message during a save that there is too little disk space to save. If this occurs, do not simply leave vi or you will lose any work that vi was unable to save. Instead, make sure you are in edit mode, then:

Press CTRL-z

Delete any unnecessary files.

Press f then press g

Perform the save process as previously described.

Including Another File in the Current File

You may want to include the contents of a different file within the one you are working. With vi, it is easy to do. Make sure you are in edit mode, then:

Position the cursor on the line ABOVE where you want the file's contents inserted.

Press SHIFT-:

Press r

Press SPACEBAR

Type: filename to be inserted

Press ENTER

Exiting Vi Without Saving Your File

NOTE: To exit vi, you must be in edit mode.

To exit vi without saving the file on which you are working:

Press SHIFT-:

Press q

Press SHIFT-:

Press ENTER

-----------------------------------------------------------------

Tech support may be reached at (317) 876-6000, please leave a voice mail for us after hours. You may reach us by e-mail at helpdesk@on-net.net.