Monday, June 14, 2010

What is .exrc file for?



   vi is one of the most commonly used editors in UNIX. When we open a file using vi, we do some settings depending on our needs say, to set line number, to set indentation, to set the tab space etc in the file. However, these settings or customizations last only till the file is open. Some of these settings any user would always like to retain on opening any file.  These settings can be grouped in a file called .exrc file, and hence the settings become permanent.

  Whenever a file is opened using vi, vi looks for the .exrc file in the home directory of the user. If present, it reads the file and applies the settings in the file being opened. And hence any customization we would like to have in the file should be put in this. In fact, all the commands which we execute in the escape mode or the last line execution mode can be put in the .exrc file.

Settings commonly maintained in the .exrc file are:

1.  Set commands :- Set commands used in vi like set list, set number, etc.
2. Abbreviations:-  Frequently used words can be abbreviated. For example,say, a user uses the word 'include' lot many times.  The user can create an abbreviation 'inc' for the word include.
3. Mapping keys:- Some key combinations can be mapped or made into a hot-key combination. We saw in in one of our earlier articles, how to map function keys.

A typical $HOME/.exrc file will look like as shown below:
set number
set autoindent
set nowrapscan
ab inc include
map Q :q!
1. The first three entries are part of the set commands, which includes setting line number, auto-indentation and search wrapping. With these settings, whenever a file is opened, automatically the line numbers will be set, and so is the indentation and search wrap.


2. The abbreviation 'ab inc include':- whenever you type 'inc' followed by a space in a file, it automatically gets converted to 'include'. This is a very helpful feature at times.

3. map Q :q! - This command maps the Q key with the quit operation. So, whenever the user wants to quit the file, instead of typing ':q!', the user can simply type 'Q' from the escape mode.

   The .exrc file should be present in the home directory itself and there is no limitation on the number of customizations being done in it.

No comments:

Post a Comment