Previous Table of Contents Next


UNIX provides multiple ways to enter text into files. The most common way is to use one of the editors. The editors are ed, ex, and vi. The ed editor is the most basic editor. It is a line editor. A line editor does not display the text on the screen while you are editing. You have to display the text, then perform editing functions on a line-by-line basis.

The ex editor is an enhanced version of the ed editor. It provides much better substitution and addressing capabilities. But it is still a line editor.

The vi editor is a visual editor built on top of the ex line editor. Thus vi has the power of visually displaying the text you are working on while also having a powerful line editor to perform substitutions and other necessary functions.

The following steps show you how to create a file using the vi screen editor. It is advisable to learn the ex and vi editors because of there power and popularity.

1.  Before you begin using vi you must have your environment set properly. First type env and press Return. Notice that a list of variables is displayed on your screen. Look for the TERM= variable in the list. This variable must be set before vi will perform correctly.

C Shell
Type printenv and press Return.

2.  To set TERM to a desired terminal type, type TERM=VT100 and press Return. Replace VT100 with the type of terminal you are using.

C Shell
Type set term=vt100 and press Return.

3.  Begin editing a file by typing vi file1 and pressing Return. Notice the screen is cleared and tildes (~) are placed along the left side. The filename is placed on the bottom line along with status information. Your screen looks like the following display.
4.  To enter text, type i; this puts vi in insert mode. Now type This is a new file created by vi. and press Esc. Notice you entered a line of text and now the cursor is at the end of the line.
5.  To duplicate the line, type yy to yank it to a register and type p to put it back on the screen. Notice you now have 2 lines of text that are identical.

6.  To move up 1 line type k, to move down 1 line type j, to move right 1 character type l, and to move left 1 character type h.
7.  To jump to the beginning of the current line type 0. Notice the cursor moves to the beginning of the line.
8.  To jump to the end of the current line type $. Notice the cursor moves to the end of the line. Remain in vi to continue the following steps.

The following steps show you how to interface with ex commands while in vi.

1.  Type :ver and press Return to display what version of vi/ex you are using. On some systems you will need to press Return to redisplay the vi screen.
2.  Write the file to disk by typing :w and pressing Return. On some systems you will need to press Return again to redisplay the vi screen.
3.  To substitute a string throughout the file (global substitute) type :%s/a new/an old/g and press Return. Notice that both lines changed from having the string "a new" to having "an old." The percent sign (%) tells ex to search lines 1 through end-of-file (EOF). The "s" tells ex to perform a substitution. The "/a new/an old/" says to search for "a new" and change it to "an old." The g tells ex to change all occurrences on a line. The default is to change the first occurrence on each line. Press Return.
4.  To escape to the shell type :sh and press Return. Notice a shell prompt appears on your terminal; you have been placed in a new subshell. You may execute any normal UNIX command. To return to vi/ex press Ctrl-D and then press Return.
5.  To execute a UNIX command without leaving the editor type :!cat % and press Return. The % is expanded by vi into the current buffer name (file1). The command cat file1 is executed and control is returned to vi.
6.  To save the file to disk and exit vi type ZZ (capital letters).

Previous Table of Contents Next