Previous Table of Contents Next

Module 151


The vi editor is a powerful interactive full-screen editor built on top of the ex line editor. Thus it provides the user with a powerful line editor for substitutions and interfacing with UNIX and a powerful screen editor for visual editing. The vi editor acts as a window into a file being edited and reflects changes as they are made.

There are various methods of editing files using vi. They are:

*  Single file editing.
*  Edit multiple files without leaving the editor.
*  Specify ex commands to be interpreted and executed before full screen.
*  Recover files lost because of abnormal termination.
*  Using the tags file to edit multiple files based on tag names.
*  Edit a file in read-only mode; view a file.
*  Enter vi from the ex editor.


Following is the general format of the vi command.

     vi     [ -ClLRx ] [ -rlost ] [ -ttag ] [ -wn ] [ +cmd ] [ file_list ]

     view   [ -ClLRx ] [ -rlost ] [ -ttag ] [ -wn ] [ +cmd ] [ file_list ]

     vedit  [ -ClLRx ] [ -rlost ] [ -ttag ] [ -wn ] [ +cmd ] [ file_list ]

BSD (Berkeley)
vi [ -l ] [ -rlost ] [ -ttag ] [ -wn ] [ +cmd ] [ file_list ]

vi Invokes vi in full screen mode with default settings.
view Invokes vi in read-only mode, no updates allowed.
vedit Designed for beginners. The report flag is set to 1, so all changes are reported. The showmode and novice options are set for more interactive information.


The following list describes the options and their arguments that may be used to control how vi functions.

-C Encryption, the data is assumed to be encrypted. You are prompted for an encryption/decryption key.
-l Sets the showmatch and lisp mode options. The (), {}, [[ and ]] commands are changed to indent for lisp code.
-L List all preserved files. Files saved as a result of an editor abort or a system crash.
-r [lost] Recover lost file that was preserved during abnormal termination of vi. Use -r option alone for list of preserved files.
-R Read-only mode, no updates allowed.
-t tag Starting at tag, edit the file that is referenced by tag in the "tags" file.
-wn Set default window size to n lines.
-x Edit encrypted files. You are prompted for an encryption key.
+cmd Display buffer after interpreting an ex command cmd. Plus (+) with no cmd begins editing at last line of buffer. Enclose commands with special characters in single quotes ('cmd') to escape ksh interpretation.


The following argument may be passed to the vi command.

file_list One or more files to edit. If you specify more than one file, vi edits the first file on the command line. To edit the next file you enter :n and press Return.

Table of Contents

Interface capabilities 592
Moving around the buffer 592
Manipulating Text 592
The Design of vi 592
Editing with vi 593
Pros 593
Cons 593
Determining Your Terminal Type 593
Defining the Terminal Type 593
Defining the Terminal Capability Database 594
Automatically Defining TERM 594
Emulating Terminals and Software 594
Killing a vi Session 595
Delete/Interrupt Key 595
Units (Objects) 597
Regular Expressions 597
Registers 597
Buffers 598
Unused Characters 598
Limits 599
vi 599
Character Movement 599
Word Movement 600
Line Movement 600
Vertical Movement 600
Horizontal Movement 600
Sentence 600
Paragraph 600
Section 600
Screen 600
Character Search by Line 601
String Search 601
Marking and Returning 602
Buffer Movement 602
Scrolling 602
Go to Location 602
Screen Adjusting 602
Scrolling 603
Ex Mode 603
Miscellaneous 603
Inserting Text 604
Special Insert Command Characters 604
Operators 604
Changing Text 605
Deleting Text 605
Replacing Text 605
Indenting Text 605
Filtering Text 605
Moving Text (cut & paste) 606
Yanking (cutting) 606
Putting (pasting) 606
Moving text between files 606
String Macros (Mapping) 607
Command Mode String Macros 607
Insert Mode String Macros 607
Input Mode motion definitions 608
Register Macros 608
LEAVING vi 609
TAGS 610


Once you have entered vi the cursor is like a pencil and eraser that may be moved around on the screen to write (insert) or erase (delete/change) as needed. The buffer can be thought of as a scroll of paper with a picture frame placed over the flat exposed part of the scroll. The data inside the frame can be visually manipulated using vi. The data on the entire scroll can be globally manipulated using ex, the line editor.

Interface capabilities

The vi editor can interface with ex commands to perform global substitutions and file utilities. A subshell may be entered from vi and UNIX commands executed until a Ctrl-D or exit is entered. UNIX commands can be executed from vi without leaving the editor. UNIX filters can be interfaced to allow for special editing capabilities.

Moving around the buffer

There are motion commands to move the cursor one of four directions (i.e. up, down, left, right) in multiples of units. Units are defined as:

characters words
lines sentences
paragraphs sections
the screen pattern searches
marked addresses the entire buffer

For example, to move forward a word you press w; to move to the top of the screen you press H. There are key sequences that allow you to perform various motion commands.

Manipulating Text

There are operators which may be combined with a motion command to perform the following text manipulations:

Operator Description

c change
d delete
< remove indent
> indent
y yank (same as cut)
! filter (send text to UNIX filter and retrieve output)

For example, to change a word you would type cw; to delete everything from the current line to the top of the screen you would type dH. The c in cw is used for changing text and the d in dH is used for deleting text.

A filter is a UNIX command that can read from the standard input and write to the standard output; therefore, it can be used in a pipe. A pipe is a combination of UNIX commands with the output of one command being used as the input of the next command. Any UNIX filter command may be used by vi to manipulate the text stored in the session buffer, thus vi has all the text manipulation features of UNIX itself.

The Design of vi

There are a few differences between vi and PC type editors or word processors that are worth noting. When comparing and using vi, it helps to remember that it was designed by programmers for programmers who were using terminals and keyboards with limited capabilities. The vi editor was designed before fancy keyboards were available and thus its keystrokes are usually related to the functions they perform. For example, pressing i places you in insert mode.

Editing with vi

The structure of vi is based on units of text and operators. The main focus behind vi is single keystroke commands to move around the file and the use of operators to allow text manipulations based on these commands. Any operator may be used to manipulate any unit of text. Thus vi provides a rapid way of making changes but is not user friendly.


*  Motion commands and text operators can be combined.
*  Interfaces with any UNIX filter command.
*  Provides a way to change a unit of text, instead of inserting new text and deleting the old text, when overlaying will not suffice.
*  26 registers to store data in and retrieve at a later time.
*  Multiple methods of entering Insert mode; based on units of text.


*  Must toggle between modes (Command/Insert) to move or make changes.
*  No true overlaying of text mode. You must be in Command mode to move around the buffer.
*  Lack of inverse video for certain editing functions. Addresses marked in text are not visible.

The vi editor was developed by The University of California at Berkeley, Computer Science Division, Department of Electrical Engineering and Computer Science.


There are a few shell environment variables that must be set before you start using vi.

Determining Your Terminal Type

List the terminfo directory for terminal types beginning with the first letter of your terminal's model number. For example, if you have a terminal with a model name of tvi955 then you would type the following command to list all possible entries.

     $ ls -C /usr/lib/terminfo/t/tvi*

You now have to choose one of the listed terminal types. For further information check with your system administrator and read the terminfo entry in section 4 of the Programmers Reference Manual distributed with your system.

Defining the Terminal Type

vi must know what type of terminal you are using so it can control your terminal screen correctly. The shell variable TERM is used by vi to know what terminal type you have. Therefore, you must set and export the TERM variable before invoking vi. For instance, the following command sets and exports TERM to be a vt100 type terminal.

     $ export TERM=vt100

The model number or name label on the front of your terminal is usually used in the database if a definition exists.

If you do not define your terminal type correctly, vi may send the incorrect screen control sequences and your terminal screen may become garbled. If this happens type capital Q followed by set term=type and press Return, where type is the correct terminal type. Then type vi and press Return to reenter vi full screen mode.

Defining the Terminal Capability Database

vi must also know where to find the control codes used to control your terminal. These codes are stored in a database referred to as TERMINFO. The TERMINFO database is located in /usr/lib/terminfo/?/*. Where the ? is the first character of your terminal type and the * represents all terminal type definitions starting with letter ?. If you wish to use your own definition of TERMINFO you can define the TERMINFO variable to the directory containing the entry. For example,

     $ export TERMINFO=/u1/tech/mylogin

Csh Shell
setenv TERMINFO /ul/tech/mylogin

sets the TERMINFO variable so vi can use your copy of the terminal capability definitions instead of the system default definitions.

Automatically Defining TERM

You should define your TERM variable in your .profile file. If you login from different terminals, you may want to prompt for the TERM type. The following code prompts for the TERM type and sets it accordingly.

     echo "TERM=$TERM:\c"
     read xterm
     export TERM

If you press Return, vt100 is used; otherwise, he new type you entered is assigned to TERM. If you always use the same terminal to log in to the system, then you may want to use the code that sets your terminal type and remove the prompting code.

Previous Table of Contents Next