We’re giving away 1,500 more DJI Tello drones. Enter to win ›
William von Hagen | Published January 18, 2011
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 section in the right for links to some great vi and Vim tutorials. That section also lists other popular vi clones and where to get them.
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.
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.
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 section in the right for links to more information about such conditionals.
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.
(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
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.
The set command enables users to set internal vi and Vim variables. Variables are set in different ways depending upon their type:
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 $:
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.
The map command enables users to bind existing or new commands to specific keys or key combinations using the map key-sequence commandmap key-sequence command syntax for vi’s command mode and map! key-sequence commandmap! key-sequence command for vi’s insert mode.
map key-sequence command
map! key-sequence command
The following are some sample map commands for Vim:
map <C‑T> hhxpmap <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 hhxpmap ^@ !}fmt ^M
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 theab 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 resources section in the right for links on much more information.
March 14, 2019
This article provides a way to implement a kernel module on Linux, compile it, and explore ways in which a…
May 8, 2019
Artificial intelligenceData science+
Back to top