Previous | Table of Contents | Next |
DESCRIPTION
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:
COMMAND FORMAT
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. |
Options
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. |
Arguments
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
FUNCTIONAL OVERVIEW
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.
Pros
Cons
The vi editor was developed by The University of California at Berkeley, Computer Science Division, Department of Electrical Engineering and Computer Science.
GETTING STARTED
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.
CAUTION:
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.
TERM=vt100 echo "TERM=$TERM:\c" read xterm TERM=${xterm:=$TERM} 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 |