Customizing the vi and Vim editors

Extending standard editors with custom commands


The vi text editor (or a workalike clone) has been included as part of every UNIX and UNIX-like system since 1978. Due to AT&T licensing restrictions, UNIX-like open source systems had to write their own vi clones from scratch, which led to vi workalikes such as Elvis, nvi, and Vim (the latter of which is discussed in this article along with the original vi). This ubiquity makes it one of the truly indispensable tools for a system administrator of any UNIX, Linux, Berkeley Software Distribution (BSD), or other UNIX-like computer system.

Fans of the Emacs text editor on UNIX and UNIX-like systems, myself included, delight in its flexibility, expandability, and configurability. This article highlights those same capabilities in vi and its most popular clone, Vim. The vi and Vim editors provide essentially the same customization and configuration possibilities as Emacs, and vi's support for abbreviations is unmatched in Emacs. Knowing vi makes it possible for any system administrator to be instantly productive on any UNIX-like system, long before you figure out how to install Emacs.

This article explains how to customize vi and Vim, not how to use them. See Related topics for links to some great vi and Vim tutorials. That section also lists other popular vi clones and where to get them.

Types of vi and Vim customizations

The vi and Vim editors store customizations in text files or environment variables (which are discussed in the next section). Each of the vi or Vim configuration files can contain any number of the types of configuration and customization commands shown in Table 1.

Table 1. Configuration and customization commands
abDefine abbreviations that are expanded into a specific sequence of characters when they are encountered while you are typing text in Insert mode. These abbreviations can function like the AutoCorrect feature in Microsoft® Word.
mapAssign existing vi or Vim commands to a custom key or define your own custom commands. See "Customizing key bindings and creating commands" for more information.
setSet a vi or Vim variable. Depending on the type of variable and how it is used, variables are set either by simply naming the specific variable or by specifying and assigning an explicit value. See "Setting vi and Vim configuration options" for more information.

You can intersperse configuration commands and comments within the command in a vi or Vim configuration file. In Vim configuration files, any characters following a double quotation mark (") that appear anywhere on a line are viewed as a comment, while a comment in standard vi configuration files must have the double quotation marks as the first character of the line. Standard vi configuration files cannot contain any blank lines, whereas they are fine in a Vim configuration file.

The Vim editor supports additional keywords that enable you to take advantage of capabilities that are not present in the original vi editor, including those shown in Table 2.

Table 2. Additional keywords supported by Vim
autocmdAutomatically execute specific commands based on the type of file you are editing. See Downloadable resources for a sample.vimrc file that includes an autocmd example.
filetypeLoad and activate various plugins that provide extra capabilities when you are editing files of specific types.
functionDefine a custom function which you can then map to a key command.
syntaxActivate or deactivate syntax highlighting.

The configuration files used by Vim also support a rich set of internal functions that you can use in conditional expressions. These enable you to write your own functions and execute them in different contexts. See Related topics for links to more information about such conditionals.

Startup files for vi and Vim

You can define the vi and Vim configuration options discussed in the previous section in environment variables, configuration files, or both. Both vi and Vim use a sequence of environment variables and configuration files to load customizations and share much of the same configuration sequence, though Vim uses a more extensive set.

By default, the vi and Vim editors check the locations in Table 3, in the order listed, for configuration information.

Table 3. Locations vi and Vim check for configuration information
1Specific file invoked by -u option

(Vim only): If invoked with the -u option followed by the name of a configuration file, Vim ignores the rest of its configuration sequence and loads only settings from the specified file. To skip all configuration files, you can specify NONE as the argument to the -u option.

You can prevent standard vi from loading any configuration files by setting the EXINIT environment variable to the ":set noexrc" string and then executing vi. The following example does this from the command line if you are using the Bash shell:

EXINIT=":set noexrc" vi file1.txt
2Specific configuration file identified when Vim is compiled(Vim only): A specific configuration file identified when Vim is compiled (usually /etc/vim/vimrc or /usr/share/vim/vimrc) that contains system-wide configuration settings for Vim. This file typically sets some configuration options and loads other Vim configuration files that are specific to the operating system and operating system distribution that you are running.
3VIMINIT environment variable(Vim only): An environment variable that consists of a sequence of one or more configuration commands. If this environment variable is set, Vim does not look for any subsequent configuration files or examine other environment variables.
4 $HOME/.vimrc(Vim only): A file named .vimrc, located in the user's home directory, that contains configuration options. If this file is found, Vim does not look for any subsequent configuration files or examine other environment variables. See Downloadable resources for the sample.vimrc file.
5EXINIT environment variableAn environment variable that consists of a sequence of one or more configuration commands.
6$HOME/.exrcA file named .exrc, located in the user's home directory, that contains configuration options. If this file is found, vi and Vim do not look for any subsequent configuration files unless the exrc option is set in this configuration file. See Downloadable resources for the sample.exrc file.
7.exrcA file named .exrc, located in the current working directory, that contains configuration options. (If you are using Vim on a Microsoft Windows® system, the name of this file is _exrc.) An .exrc file in the current directory is read only if a previous configuration instruction or file set the exrc option (or if you are starting vi or Vim from your home directory and you have an .exrc file there, which therefore matches the following step in the search sequence).

This impressive series of alternatives makes it especially easy to configure system-wide defaults for Vim, while also enabling some clever interaction between Vim and standard vi configuration files. For example, whenever my home directory is not located in a networked file system, I copy all my configuration files for shells and applications to each of the systems where I have an account. Some of these systems use vi, while Vim is provided (as vi) on others. Thus, I put all my vi-specific configuration options in an .exrc file, put all the Vim-specific configuration commands in a .vimrc file, and put the following line at the beginning of my .vimrc file:

so .exrc

This causes Vim to read all the configuration commands stored in my .exrc file before loading those in the remainder of my .vimrc file. On systems where only vi is present, my .exrc file is read normally and the .vimrc file is ignored. Loading configuration commands at the beginning of my .vimrc file enables key mappings that are present in both files to be overridden by fancier Vim versions when I'm using Vim.

Setting vi and Vim configuration options

The set command enables users to set internal vi and Vim variables. Variables are set in different ways depending upon their type:

  • Variables, set to on or off, are set by the set variable command and unset using the set novariable command.
  • Variables that take values are set by the set variable=value command.

For example, the set list command sets the list option, which tells vi or Vim to display characters that represent end of line ($) and unexpanded tab (^I) characters. (In Vim, this option also displays trailing whitespace.) The list option does not require a value—simply setting it is sufficient to activate the feature. You can unset the list option by executing the set nolist command.

Other variables take explicit values or types of values, which you assign using the equal sign (=) followed by a value or comma-separated list of values. For example, in the Vim editor, you can configure the characters used by the list command to represent end of line, trailing whitespace, and unexpanded tabs by setting the value of the listchars variable. The following command displays unexpanded tabs as >>, trailing whitespace as !, and end of line as $:

set listchars=tab:>>,trail:!,eol:$

To see the current value of all of the options that you can set in vi or Vim, use the :set all command within vi or Vim. To display the current value of a specific option, use the f:set option? command. To show all options you have modified from their default values, use the :set command.

Customizing key bindings and creating commands

The map command enables users to bind existing or new commands to specific keys or key combinations using the map key-sequence command syntax for vi's command mode and map! key-sequence command for vi's insert mode.

The following are some sample map commands for Vim:

map <C-T> hhxp
map <F5> !}fmt <CR>

The first of these maps the key sequence control-t to the vi command sequence hhxp, which reverses the order of the two characters in front of the current cursor position (as does control-t in Emacs). The second of these maps the F5 key to a key sequence that invokes the fmt command to reformat the current paragraph.

One important difference between map commands in configuration files for standard vi and Vim is that, as shown in the previous examples, Vim supports key identifiers within angle brackets to identify keys that would otherwise be non-printable characters. vi requires that you actually insert the control sequence (which you do by pressing control-v followed by the key sequence). The vi versions of the sample map commands would look like the following:

map ^T hhxp
map ^@ !}fmt ^M

Defining abbreviations

The ab command enables you to identify abbreviations for long but commonly-used terms, bits of code, spelling corrections, and so on. Abbreviations that you define are expanded as you enter text in insert mode, whenever you type a whitespace or punctuation character. For example, some of my favorite abbreviations are the following:

ab het the
ab teh the

Abbreviations work only in insert mode in standard vi, but they also work in replace mode and command-line mode in Vim. The Vim editor provides the cab (command-line-abbreviation), iab (insert-mode abbreviation), and rab (replace-mode abbreviation) commands to enable you to restrict the expansion of specific abbreviations to specific modes in Vim.


Knowing how to use the vi and Vim editors is a fundamental skill for any administrator of any UNIX, Linux, or other UNIX-like system. Though vi is a classic UNIX tool, it is extremely configurable and is used by many developers for day-to-day work as well as on systems where it is the only editor available.

This article introduced the basic vi and Vim configuration commands and provided an overview of how vi and Vim locate and load customized configuration settings. There is much more to learn about these topics than can fit into a single article. See Related topics for links on much more information.

Downloadable resources

Related topics


Sign in or register to add and subscribe to comments.

Zone=AIX and UNIX, Open source
ArticleTitle=Customizing the vi and Vim editors