Previous Table of Contents Next

Emulating Terminals and Software

Certain terminals have the capability to emulate other terminals. If your terminal is not a valid type, check your owner's manual for possible terminal types your terminal can emulate.

Terminal emulating software (e.g. PC communication packages) may be used to interface with vi. Certain software packages use the Esc key for their attention interrupt. You will need to change the definition of this interrupt to one of the keys not used by vi (refer to the section on Unused Characters) or to an <ALT> key on your PC.


If you are in vi and for some reason you get logged off the system, use the recovery feature. Possible reasons are:

*  If the line to your terminal dropped.
*  If vi hung (will not respond) and you killed the session.
*  The system crashed gracefully (don't hold your breath!).
*  The system was shutdown by the super-user.

To recover a lost file you must first know whether vi "preserved" the file. The command to do this is:

     vi -r

A list of all lost files is displayed. If no files are displayed then you have no files available for recovery. If a file you need to recover is displayed, you must first change your present working directory to be the same as where the file is located. Then you can recover the file. For example, if you have a file, myprog.c, in your HOME/bin directory, to recover it you need to enter the following commands,

     cd $HOME/bin
     vi -r myprog.c

The file you are recovering must not have been modified since the date of preserve file. If you recover from an old preserve file, you can cause serious damage to your existing file.

Killing a vi Session

You may have to terminate a session of vi using the kill command. If vi hung or you where disconnected from the system for some reason and vi did not die from the hang-up, you will have to kill the session. To do this you must first find the PID of the vi process. The following code illustrates the necessary commands to find the PID and kill vi.

     $ ps -u mylogin | grep vi | nawk "{print $1}"
     $ kill -15 1506
     $ vi -r oldfilename

The ps command returns a PID number if there is a hung vi process, in this case PID 1506. The PID 1506 is a hung vi session. If there is no vi process hung, only your prompt will return. If a PID is returned, terminate it via the kill -15 1506 command. This causes vi to generate a preserve file for recovery. If no modifications were pending to be saved, then vi exits without preserving. If you had made changes, recover your file using the -r option.

NOTE:  If the system crashed, you might try to recover your lost file by using the -r option. This will work if vi had enough time to preserve your vi session to disk.

Delete/Interrupt Key

Press the Delete key to abort commands in vi. This may garble the screen, so be ready to use the Ctrl-R or Ctrl-L command to redraw the screen.

If you press Delete while making a change (manipulation), you may need to use the u command (undo) to return to the condition before you began the change.


Certain characters and lines are used to present a more understandable screen picture of the session buffer.

Displayed Represents

@ Deleted lines; only used on dumb terminals or if last line of display is too long and wraps off the screen.
~ Lines past end-of-file; an empty file will have a single column of tildes from the cursor to the bottom of the screen.
^x Control characters (x = A-Z, ? or ]); control-I (tab) and control-J (new-line) are always interpreted by vi and cannot be escaped.
Lastline Used for general information, ex mode line, error messages, feedback about i/o, report notices, and to echo back input for search commands.

Command line for vi to create a new file.

     cj> vi text

Initial screen when vi is invoked for a new file:

The default number of lines used for the screen display depends on the baud rate of your terminal. The faster the baud rate, the larger the initial number of lines displayed. The lines beginning with a tilde (~) are not in the session buffer. This file currently has no lines in the session buffer. The last line displays information about the current file.

Since there is no text in the buffer, you cannot move the cursor. Therefore, you must press one of the insert keys and insert text.


The vi editor has three modes of operation. Each mode is a separate entity from the other two. You must be in command mode to issue commands to vi and to access the ex mode command line. You must be in insert mode to insert text. Thus you have to toggle between insert and command mode. The reason for this environment is simple: vi lets you use single keystroke commands. You do not have to twist your hand around to press some special control sequence to move the cursor around the buffer. Indeed there are some control characters used, some in command mode and a few in insert mode.

viMode Description

Command Interprets keystrokes and performs specified functions. The movement of the cursor by units within the buffer. The deleting, filtering, and moving of text is done in command mode.
Insert The changing, inserting, and replacing of text by units within the buffer.
Ex/Last_Line The movement, changing, replacing, moving, and deleting of text in conventional line editor commands within the buffer. File handling commands, status information, and substitutions are performed in ex mode.


Before continuing the discussion of vi it is necessary to understand the terminology relating to the editor. The following sections clarify some of the definitions and concepts of vi.

Units (Objects)

Units (often referred to as objects) of text are predefined amounts of text used by motion commands.

Units Description

character Any single ASCII character (000-177 octal).
word Text delimited with space, tab, or new-line characters.
line Physical (UNIX) line ending with new-line or Return.
sentence Text ending with a ., !, or ? followed by two spaces or a blank line.
paragraph A Memorandum Macro (mm) or Typesetting (troff) token; .IP, .LP, .PP, .QP, .P, .bp or a blank line.
section A Memorandum Macro (mm) or Typesetting (troff) token; .NH, .SH, .H, {, blank line, form feed or new-line.
screen The visually displayed data, from the top left corner to the bottom right corner.
search The unit of text from the current position up to but not including the next occurrence of a given search pattern.
mark From the current position to the position marked by single letter address.
buffer A copy of the entire buffer, from line 1 to the end of the buffer (file).

A character preceded by a caret (^) that cannot have the cursor positioned under the ^ is a control character placed in the buffer.

Regular Expressions

Regular Expressions are patterns used to match strings in searches and substitutions. Refer to Module 113 on Regular Expressions.


The vi editor maintains various registers where text may be stored. Registers are used to save and retrieve text. You reference named registers with a single alphanumeric name. To access a register you place a double quote (") before the desired register name. For example, if you want to store a line of text in register "a" for later use, you type the following command,


This informs vi to place the next unit of text in the "a" register. The unit of text is the current line (yy, yank one line). Possible register names are unnamed/undo, numbered, and named.

The following table describes all of the registers and buffers used by vi.

Name Type Description

0 Unnamed Register Where the last text that was manipulated is stored. The undo register. You cannot address this register with the " notation.
1-9 Number Register Stores whole lines of text deleted from the session buffer. As new yanks or deletes are performed, the text in registers one through nine is shifted up to the next higher register, thus the data in register nine is lost but replaced by the data in register eight.
a-z Named Register Save to register whose name is a single alpha character for later use. Used to move text within and between buffers (files).
A-Z Named Register Append to register for later use; single capital alpha character name.
REG Register Refers to any of the above registers.
"REG Register Address the specified register. Use the double quote to address any of the above registers, except the undo register.


The vi editor uses a session buffer to maintain a copy of the file you are editing. There are two session buffers that you can switch between. For example, if you enter vi you can edit another file without leaving vi and then switch back and forth between the two session buffers. The following commands illustrate how you perform this simple task:

vi file1 # edit the first file
:e file2 # while in vi edit the second file
:e # # toggle back to edit the first file
:e # # toggle back to edit the second file

There are two session buffers. The current session and the alternate session. You reference the alternate session buffer by using the :e# command.

The following table describes the buffers used by vi.

Buffer Name Description

session The editing buffer vi uses to store the current file being edited. vi does not use the name session; when you enter vi you're in an editing session and thus the buffer is called the current session buffer.
% Name of current session buffer or filename.
# Name of alternate session buffer. The last filename referenced in ex mode will become the alternate file.

Each of the above buffers are dynamic ranging in size. The undo and numbered buffers are only affected by line changes and cannot be addressed for text storage.

Unused Characters

These characters may be used for the name of a string macro without interfering with existing commands. String macros are created using the :map command.

Unused Command Mode Characters
Ctrl-A Ctrl-C Ctrl-I Ctrl-K
Ctrl-O Ctrl-Q Ctrl-S Ctrl-T
Ctrl-V Ctrl-W Ctrl-X Ctrl-Z
Ctrl-\ Ctrl-_ * \
K V g q

Ctrl-Q is used for restarting output to terminal. Ctrl-S is used for stopping output to terminal; see Module 126 on stty. Systems with job control use the Ctrl-Z to suspend jobs.

Previous Table of Contents Next