Configuring vi

<> Configuring vi

When we first started talking about vi, I mentioned that there were a lot things we could do to configure it. There are mappings and abbreviations and settings that we can control. The problem is that once we leave vi, everything we added is lost. Fortunately, there is hope. Like many programs, vi has its own configuration file: .exrc (note the dot at the front).

Typically, vi just uses its standard settings and does not create this file on its own. However, if this file resides in our home directory, it will be valid every time we start vi unless we have an .exrcfile in our current directory, which will then take precedence. Having multiple .exrc files is useful when doing programming as well as when editing text. When writing text, I don’t need line numbers or autoindent like I do when programming.

The content and syntax of the lines is exactly the same as in vi; however, we don’t have the leading colon. Part of the .exrc file in my text editing directory looks like this:

map! ^X :wq
map x :wq
map! ^W :w
map w :w
set showmode
set wm=3
abbr Unix UNIX
abbr btwn between
abbr teh the
abbr refered referred
abbr waht what
abbr Para {P}
abbr inot into

By default, vi will look in the current directory for an .exrc. This is useful for having different version for different tasks, for example, one for programming and one for normal editing. If there is no .exrc in the current directory, vi will look for one in your home directory.

One important caveat is that the, for the root only the .exrc in root’s home directory will be used. This is for security reasons. If an .exrc file was used in the current directory, a nefarious user could add something that might do something unexpected.

If you are using the improved version of vi, vim, there is also the /etc/vimrc, which applies to all users.