This handout contains a brief explanation of the vi text editor. You will use vi for the laboratory and programming assignments in this and other courses, so you may want to keep the handout with you when you are working on the assignments.

The vi editor is a full-screen editor. This means that vi allows one to move the cursor around on the screen to make additions, deletions, and corrections rather than by explicitly referring to a line number, as in the UNIX editor edit. It is important to remember that vi is case sensitive, so make sure to enter commands as shown, whether in upper or lower case.

One characteristic that will become obvious is that there are many editing functions that are defined in terms of unique key sequences--more editing functions than can be learned in a short time. Only the more common editing functions will be covered here.

vi has two modes of operation, namely, the command mode and the insert mode. While in command mode, vi accepts keystrokes as commands and responds to each command when the <cr> key is pressed. In the insert mode, vi accepts keystrokes as text and displays each keystroke (except for shift, ctrl, lock, etc.) as it is entered. You will make use of many of the following vi commands in laboratory assignment #1.

 

STARTING VI, ENDING VI, AND SAVING YOUR WORK

vi Invoke the vi editor to create a new file. The file name must be supplied by a subsequent write command.
vi <filename> Initiate an editing session on the file specified by <filename> if it already exists, or create a new file otherwise.
a Append text to the work buffer after the cursor position until the <esc> or <escape> key is pressed.
i

Insert text into the work buffer before the cursor position until the <esc> key is pressed.

NOTE: If you append to existing text, the new characters may appear to write over existing text. However, when you press <esc>, the old text will reappear.

<esc> If in the insert or append mode, return to the command mode; if already in the command mode, remain in the command mode.

The vi does all its work in a work buffer that is not written to disk until a write command is issued. Thus, it is a good idea to save your work at frequent intervals to avoid losing hours of input time accidentally. The following are associated with the command mode, not the insert (or append) mode.

ZZ Write out the work buffer to the disk and end the vi editing session. Upper case ZZ must be used.
:w Write out the work buffer to the disk.
:w <filename> Write out the work buffer to the newly created file specified by <filename>.
:w! <filename> Overwrite an existing file with the work buffer contents.
:wq Write then quit. Write out the work buffer to the disk and end the vi editing session.
:q! Quit without writing (i.e., saving) the contents of the work buffer.

 

POSITIONING WITHIN A FILE

NOTE:"^" represents the control or <ctrl> key, which is held down while pressing the other key.

^F Scroll forward one full screen.
^B Scroll backward one full screen.
^D Scroll down half screen.
^U Scroll up half screen.
G Go to end of file (last line in the file).
nG Go to line n where n is an integer.
/pat Find next line containing the pattern pat.
?pat Find previous line containing the pattern pat.

 

LINE POSITIONING

H Home window line (the first line on the screen).
L Last window line (the last line on the screen).
M Middle window line (the middle line on the screen).
+ Position cursor at the first non-white position on the next line.
<cr> Position cursor at the first non-white position on the next line.
- Position cursor at first non-white position on the previous line.
j Position cursor on the next line in the same column.
k Position cursor on the previous line in the same column.


CHARACTER POSITIONING

0 Beginning of line. (This is the digit "zero").
$ End of line.
h Move cursor one position towards left.
l Move cursor one position towards right. (This is the letter "ell").
fx Find the next occurrence of the character x.
Fx Find the previous occurrence of the character x.
% Match braces and parentheses.


INSERT AND REPLACE (use <esc> to terminate input)

a Append text after cursor.
i Insert text before cursor.
A Append text at end of line.
o Open a new line below the current line.
O Open a new line above the current line.
rx Replace the single character under the cursor by character x.
R<string> Characters starting with the character under the cursor are replaced by entries from the keyboard until the <esc> key is pressed.

NOTE: Text accessed by delete, change, and yank operations (described below) is placed into a general purpose buffer by default, or into a named buffer if a named buffer is specified. The contents of this buffer may be copied elsewhere in the work buffer by a put operation (also described below).

 

DELETE OPERATORS

dd Delete the current line.
d<cr> Delete current line and the following line.
D Delete the rest of the current line (to right of and including the cursor).
dw Delete word (to right of and including the cursor).
x Delete the character under the cursor.

 

CHANGE OPERATORS

cw Change word (to the right of and including the cursor).
cc Change the current line.
C Change the rest of the current line (to right of and including the cursor).


YANK OPERATORS

y<cr> Copy the current line and the following line into a buffer.
Y Copy the current line into the work buffer.
nyy Place a copy of n lines, starting from the current line, into the work buffer.


PUT OPERATORS

p Copy the contents of the general purpose buffer into the work buffer after the line containing the cursor.
P Copy the contents of the general purpose buffer into the work buffer before the line containing the cursor.


UNDO OPERATORS

u Undo last change.
U Restore the current line.