Appendix A. vi Text Editor

The vi text editor can be used to edit ASCII text files while you are working in a shell window. It is a standard editor used in IRIX.

This appendix includes the following sections:

Opening a File in vi

To open a file in vi, open a shell window and type:

vi filename

If you are creating a new file, type in a name for the file. If you are editing an existing file, type the current filename.

The file appears in your shell window.

Understanding vi

In vi, you use your keyboard in two different ways—to add text to a file or to navigate and move text around in the file without adding text. These two keyboard modes are called:

  • Text Input mode

  • Command mode

Use simple keystrokes on your keyboard to switch back and forth between the two modes while you work in a file.

When you first open a file in vi, you are in Command mode. Switch to Text Input mode to begin adding text. To do this, type:

a

and begin typing. To return to Command mode, hit <Esc> or <CTRL 3> on your keyboard. You need to be in Command mode before you can quit out of a file.

vi Commands

Use the following commands in Command mode to cut, paste, and move around in text. Use them also to switch from Command mode to Text Input mode. Remember to hit <Esc> or <CTRL 3> whenever you want to switch from Text Input mode to Command mode.

Adding Text in vi

Use these Command mode keystrokes to switch to Text Input mode (hit <Esc> or <CTRL 3> to return to Command mode):

a 

Add text after the cursor.

A 

Add text to the end of the current line.

i 

Insert text at the cursor.

I 

Insert text at the beginning of the current line

o 

Open a new line below the current line and add text.

O 

Open a new line above the current line and add text.

s 

Substitute the letter underneath the cursor with letter you type, and insert text.

S or c 

Delete the current line and substitute it with text you type.

R or C 

Replace current text with text you type.

Cutting and Pasting Text in vi

Use these Command mode keystrokes to cut and paste text:

x 

Delete the letter beneath the cursor.

dw 

Delete the letter beneath the cursor and the rest of the word.

# dw 

Delete the following number of words, including the current word.

D 

Delete from the cursor to the end of the line.

dd 

Delete the current line.

# dd 

Delete the following number of lines, including the current line.

yy 

Copy or “yank” the current line.

# yy 

Copy or “yank” the following number of lines, including the current line.

p 

Paste the current copied or deleted letters, words, or lines after the cursor.

J 

Join the next line with the current line (erases a carriage return).

u 

Undo the last edit.

Moving Around in a vi Text File

Use these Command mode keystrokes to move around within the file:

j 

Move down to the next line.

k 

Move up to the previous line.

h 

Move backward by letter.

l 

Move forward by letter.

w 

Move forward by word.

b 

Move backward by word.

e 

Move forward to the end of a word.

CTRL F 

Move forward to next screen.

CTRL B 

Move backward to previous screen.

CTRL D 

Move forward one-half of a screen.

G 

Move to the end of the file.

# G 

Move to the specified line number in the file.

$ 

Move to the end of the current line.

0 

Move to the beginning of the current line.

/word 

Search for specified word.

n 

Search for next occurrence of specified word.

N 

Search for previous occurrence of specified word.

Saving or Quitting vi

Use these Command mode keystrokes to save or quit out of a file:

:wq or ZZ 

Save and quit out of the file

:w 

Save the current file without quitting.

:q 

Quit if no edits have occurred.

:q! 

Quit without saving edits.