Previous Table of Contents Next

The .exrc File

The set options can be placed in the .exrc file so that each time vi is invoked the options are read and set automatically. The .exrc file may be placed in the user's HOME directory or the current working directory. The options set in .exrc will be overridden by the settings in the EXINIT variable if it exists.

An example .exrc file:

     cj> cat .exrc
     set ai | map v :w
     set window=5
     set wm=8
     map Ctrl-Z :shCtrl-M
     ab rf Robert Felps

These commands activate auto-indent (in the .exrc file | is equal to a new-line), map v to :w. Set the window size to 5 lines. The report line will be updated if 1 or more lines are modified. Set the right margin to 72; new-lines are inserted automatically. Map control-Z to an escape to a subshell key sequence. You must use Ctrl-V then Ctrl-M when creating the map. Abbreviate rf for Robert Felps.

You may save abreviations, maps, and set commands in any file. For ex/vi to read them during initialization the file must be named .exrc and reside in the current working directory. If the commands reside in another file, you can have ex/vi read the file at any time by using the following command.

     so file

where file is the name of the file containing the commands.

The EXINIT Variable

The shell variable (EXINIT) can be assigned vi options. When vi is invoked the options are read and set inside vi. The EXINIT variable should be put in your .profile file so it is set when you log in. The EXINIT variable will override the .exrc file settings.

The EXINIT variable can be set as follows:

     cj> EXINIT='set ai window=5 report=1 wm=8'

This command sets auto-indent, the window size to 5 lines, causes updates to the report line if 1 or more lines are modified, and sets the page width to 72 columns with auto-wrap margin.


There are a few bugs or implementations of features that are worth noting.

The undo command (u) causes all marks to be lost on lines that were involved in the change.

The undo command never clears the modified buffer status.

No warning is given of text stored in named registers when you exit the editor.

The z command prints a logical number of lines, not the physical. Thus if you have long lines that wrap around the screen, the top lines of the screen may scroll off the visual terminal screen.

The NUL (octal 000) is discarded on input and thus cannot be entered in the buffer.

The new-line (012), Ctrl-J, character cannot be placed in the text as visual characters.


Refer to the nawk, ed, sed, and vi commands described in modules 6, 39, 117, and 151.


The following list of files are used by ex/vi.

/usr/lib/ex?.?strings The error messages used by ex/vi
/usr/lib/ex?.?recover The recover command
/usr/lib/ex?.?preserve The preserve command
/usr/share/lib/terminfo/?/* Terminfo database for terminal descriptions
$HOME/.exrc The startup file used to store the ex/vi environment
./.exrc The startup file used to store the ex/vi environment
/tmp/EXnnnnn Temporary buffer copy of the file
/tmp/Rxnnnnn Temporary named register storage files
/var/preserve/login Directory where preserved files are kept


The ex command is useful in performing editing functions that do not require full-screen viewing of the data. For example, global substitutions, file manipulation within the editor, macros, abbreviations, tags, and various other features are beneficial to the everyday requirements of editing.


In this activity you use the ex command to create and edit multiple files. Begin at the shell prompt.

1.  Type ex mypasswd mygroup and press Return. This starts ex with a buffer named mypasswd. Since no file by that name exists, the buffer is empty.
  cj> ex mypasswd mygroup
2.  Now read in the /etc/passwd file by typing 0r /etc/passwd and pressing Return.
  :0r /etc/passwd
3.  To view the entire contents of the file type %p and press Return. Use Ctrl-S and Ctrl-Q to start and stop the display as desired.
4.  Create a backup copy of the buffer by writing the contents to a file on disk. Type w %.bak and press Return.
  :w %.bak
5.  Type /mylogin and press Return to locate the line containing your login. Replace mylogin with your real login name.
6.  Make a copy of this line by typing ya to yank the line to the undo register. Then type pu to put the line back in the session buffer.
7.  Display the new line by typing p and pressing Return.
8.  Change all the colons on the current line to tabs. Type s/:/ /g and press Return. The line should be displayed after the substitution is performed.
  :s/:/   /g
9.  Type u and press Return to undo the substitute command.
10.  Insert a line at the beginning of the buffer by typing 1i and pressing Return.
11.  Insert the following line of text. To exit insert mode type a . in the first position of a line and press Return.
  NEWUSER::5000:50:New User:/u1/new/user:/bin/ksh
12.  Save the file to disk by typing :w and pressing Return.
13.  Save the first 5 lines of the buffer to register a. Type "1,5 ya a and press Return.
14.  List all the files being editing by typing arg and pressing Return.
  [mypasswd] mygroup
15.  Type n! to edit the next file mygroup without having ex check for unsaved modifications of the current file.
16.  Type 0r /etc/group and press Return to read the /etc/group file into the buffer.
17.  Type %s/:/ /g and press Return to substitute all : (colons) to tabs.
18.  Type %nu and press Return to display the contents of the buffer with preceding line numbers.
19.  Type pu a and press Return to put the 5 lines you saved from the passwd file into buffer a back into the current buffer.
20.  Type %nu and press Return to display the contents of the buffer again. Notice the 5 new lines at the bottom of the buffer.
21.  Type q! and press Return to quit the editor without saving the changes.
22.  Type rm mypasswd* and press Return to remove the files you just created.
23.  Turn to Module 151 to continue the learning sequence.

Previous Table of Contents Next