In one of our earlier articles, we discussed the use of .exrc file. In this article, we will discuss about the EXINIT variable and how it is related to .exrc file.
EXINIT is an environment variable which is used by the vi command. When the vi command opens a file, it first reads the EXINIT variable, if present, and applies the settings accordingly.
For example, if I want the line numbering to always be enabled for files, do the below setting at the prompt:
export EXINIT="set number"Or simply:
export EXINIT="se nu"Now, If you open any file, you will see the lines are already numbered as shown below:
Similarly, if you want to set more than one option, say you also want to do "set list", the EXINIT can be set as shown below:
export EXINIT="se nu li"
Now, if you open any file, you can see both the line numbering and the listing features enabled. Similarly, we can set any number of options with the EXINIT variable. However, in order to make the EXINIT settings permanent, the setting of EXINIT has to be done in the appropriate profile file depending on your shell.
EXINIT vs .exrc:
When we discussed .exrc, we saw the same features can also be enabled using the .exrc file. Usually, when you have only a couple of settings to be done, one can go with EXINIT variable. However, if there are large number of options to be enabled, it is always the .exrc which will be preferred one of the reasons being easier to maintain a file.
However, if both .exrc file is present and also the EXINIT variable is set, its always the EXINIT variable settings will be considered. Its because in case of a clash between EXINIT and .exrc, its always the EXINIT which will override .exrc.
So, whenever any settings in .exrc variable is not taking effect, the first thing the user should check for is the existence of the EXINIT variable. If defined, unset the EXINIT variable as shown below in order for the settings in .exrc to take effect: