Using Vim: Configuration

In This Section

You may have noticed that the title of this page has gone from Vi/Vim to just Vim. That's because we're going to talk about some of the features that are unique to Vim--the ones that make it a superior text editor.

Command Source Files

One nice feature of Vim (and Vi, but not used as often there) is that if you have a list of Ex commands that you like to run, say, every time you start up your editor, you can put them all in a file and "source" the file by using the command:

:so [filename]
and Vim will run all the commands in the file for you, in sequence. In these source files, usually named with the extension .vim, you don't have to put the colons in front of commands.

Even better: if you name the file .vimrc and put it in your home directory, the commands will automatically be run upon starting up Vim. For Windows, the file is named _vimrc and is placed in the Vim program directory.

Right now, this might not sound all that useful, given the few Ex commands that were given on the previous page. But you might be able to think of some creative uses. Say, for instance, you had a reference file named ref.txt in your home directory, and you wanted to open it up every time you started Vim. Then you could create a .vimrc file containing the line:

sp ~/ref.txt
and the file would open in a split window automatically. Now, let's introduce some useful commands that will really let you configure Vim.

Vim Options

Vim has a multitude of options for configuring just about everything in it. These options are changes using the :set command. There are two types of options, boolean options and valued options. Boolean options are set by calling the command:

:set option-name
and turned off by executing:
:set nooption-name
For example, the wrap option determines whether Vim will wrap long lines or let them invisibly trail off past the right margin (the page will scroll when you move past the margin). To turn it on, use :set wrap; to turn it off, use :set nowrap.

Value options take a value like a number or word; they have the format:

:set option-name=value
A good example of this is the textwidth option, which specifies the maximum line width (if you type a line longer than this width, Vim will automatically wrap it on a word break). It takes a number indicating the number of characters allowed on a line, or zero indicating that lines are never to be automatically wrapped. So, to have Vim wrap lines at the standard ANSI terminal width of eighty columns, run:
:set textwidth=80
The word "textwidth" may be abbreviated to just "tw"; many Vim options and commands have short and long forms. And remember, since these are all Ex commands, you can put them all into your .vimrc file.

Table of Options

Here is a table of some useful boolean options.

Option Short Meaning
autoindent ai Automatically indents lines the same number of spaces as the above line. See cindent for another autoindenting system.
backup bk Writes a backup of a file whenever you update it.
cindent cin Indents lines according to the conventions of the C programming language.
compatible cp Change options to be more "compatible" with Vi. Don't set this; but if you ever find Vim acting funny, try unsetting it.
hlsearch hls Highlights the word last searched for with a slash or question-mark search.
ignorecase ic Ignore case when performing searches.
incsearch is When you type in a slash- or question-mark search, start searching immediately as the search string is being typed (rather than after you press Enter). I'm not a big fan of this one, but a lot of people like it.
number nu Shows line numbers.
ruler ru Shows the current cursor position (row and column) at the bottom of the screen.
showmatch sm When you type a closing parenthesis, bracket, or brace, the cursor will jump to the matching one for a brief moment.
showmode smd Shows the mode status (e.g. Insert mode) on the bottom line of the screen.
visualbell vb Rather than using the system bell, the screen will flash.
wrap wrap Wraps long lines to the width of the screen.

The following are common valued options.

Option Short Value Meaning
fileformat ff dos, unix or mac Specifies the file type, which determines how Vim will store newlines.
shiftwidth sw Number When cindent is on, this specifies how many spaces to indent by on each level.
syntax syn String Sets the file type, which determines the syntax highlighting scheme to be used. Usually Vim can guess what kind of file you're editing, but if it guesses incorrectly, you may use this to fix the problem. See the next page for more on syntax highlighting.
tabstop ts Number Number of columns between tab stops; the default is 8.
textwidth tw Number Maximum line length, above which Vim will attempt to force a new line by breaking at whitespace; a value of zero will suppress this.

All of the options available to Vim are available in the Options help file (:h options). Clearly there are more than you would want to commit to memory, but you can pick the ones you like and put them in your .vimrc file, and never think about them again.

On our next page, we will talk about the most useful, the most debated, and unarguably the most eye-opening feature of all: syntax highlighting.

Next Page

404 Not Found

404 Not Found