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 Resources 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
|Define 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.|
|Assign 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.|
|Set 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
|Automatically execute specific commands based
on the type of file you are editing. See Downloads for a sample.vimrc file that includes an
|Load and activate various plugins that provide extra capabilities when you are editing files of specific types.|
|Define a custom function which you can then map to a key command.|
|Activate 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 Resources 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
|1||Specific file invoked by |
(Vim only): If invoked with the
You can prevent standard vi
from loading any configuration files by setting the
EXINIT=":set noexrc" vi file1.txt
|2||Specific 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.|
|3||(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 Downloads for the sample.vimrc file.|
|5||An environment variable that consists of a sequence of one or more configuration commands.|
|6||$HOME/.exrc||A 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 |
|7||.exrc||A 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 |
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:
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
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 variablecommand and unset using the
Variables that take values are set by the
For example, the
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
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
command to represent end of line, trailing whitespace, and
unexpanded tabs by setting the value of the
variable. The following command displays unexpanded
>>, trailing whitespace as
!, and end of line
To see the current value of all of the options that you
can set in vi or Vim, use the
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,
Customizing key bindings and creating commands
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
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
would look like the following:
map ^T hhxp
map ^@ !}fmt ^M
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
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 Resources for links on much more information.
|Sample .vimrc file||sample.vimrc||3KB|
|Sample .exrc file||sample.exrc||1KB|
- "Interview with Bill Joy" (Unix Review, August 1984) provides interesting information from the original author of vi regarding its history and early development.
- See Linux vi and Vim editor: Tutorial and advanced features and Mastering the VI editor for great introductions to vi and Vim, including tutorials and advanced information.
- The Vi Lover's Home Page is another great source of information that provides links to many other vi-related sites.
- Specific sections of the Vim documentation provide complete information about setting and customizing abbreviations, functions, key mappings, and options.
- See developerWorks' "Scripting the Vim editor" series for excellent articles about creating Vim scripts.
- The AIX and UNIX developerWorks zone provides a wealth of information relating to all aspects of AIX systems administration and expanding your UNIX skills.
Get products and technologies
- See the Vim home page or the Vim page at SourceForge for complete information and download links for Vim.
- Elvis and nvi are other popular open source vi clones.
- Get involved in the developerWorks community to connect with other developerWorks users while exploring the developer-driven blogs, forums, groups, and wikis.
- Follow developerWorks on Twitter.
- Get involved in the developerWorks community.
- Participate in the AIX and UNIX® forums:
Dig deeper into AIX and Unix on developerWorks
Get samples, articles, product docs, and community resources to help build, deploy, and manage your cloud apps.
Keep up with the best and latest technical info to help you tackle your development challenges.
Software development in the cloud. Register today to create a project.
Evaluate IBM software and solutions, and transform challenges into opportunities.