John Robinette

Customizing Vim

(Near) Endless Possibilities

One of the best features of Vim, in my opinion, is the great amount of customizability you can squeeze from it. A plain, out-of-the-box Vim installation, while useful, really isn't too special and the initial adjustment to the different modes could turn some users off. That probably would have happened to me if I hadn't discovered some very useful features. We're going to take a look at my configuration to give you an idea of what's possible.

Before we dive into all of the possible customizations, I'll point out some useful files and directories, plus a short explanation of what they're used for.

~/.vimrc (~/_vimrc on Windows)
Configuration script loaded whenever an instance of Vim is started.
~/.gvimrc (~/_gvimrc on Windows)
Configuration script loaded after .vimrc when the GUI version of Vim is started.
~/.vim/ (~/vimfiles/ on Windows)
Directory for storing custom Vim config files.

There are many other directors under ~/.vim. Note that these parallel the runtime directories in your Vim installation directory, so for any folder in that directory (e.g. autoload, doc, plugin), you can also have a personal version in ~/.vim.

A Sample .vimrc

set nocompatible
set nowrap
set hlsearch
" fix backspace
set backspace=indent,eol,start
set nobackup
syntax on
" show ruler and commands
set ruler
set showcmd
" use UNIX style newlines
set ff=unix
set ffs=unix,dos
" use spaces instead of tabs
set sw=4 sts=4 ts=4 et
" Only do this part when compiled with support for autocommands.
if has("autocmd")
  " Enable file type detection.
  " Use the default filetype settings, so that mail gets 'tw' set to 72,
  " 'cindent' is on in C files, etc.
  " Also load indent files, to automatically do language-dependent indenting.
  filetype plugin indent on
  " Put these in an autocmd group, so that we can delete them easily.
  augroup vimrcEx
  " When editing a file, always jump to the last known cursor position.
  " Don't do it when the position is invalid or when inside an event handler
  " (happens when dropping a file on gvim).
  autocmd BufReadPost *
    \ if line("'\"") > 0 && line("'\"") <= line("$") |
    \   exe "normal g`\"" |
    \ endif
  augroup END
else
  set autoindent        " always set autoindenting on
endif " has("autocmd")

Explanation

You can look up most of these with :help <command> from inside Vim, but I'll touch on some key parts.

There are some other things interspersed through there, but I'm sure you can determine which options do the preceding things. Almost any command you could type in Vim command mode could be placed in a .vimrc file to be loaded on startup. You may notice that I didn't put any rules for specific filetypes in here. We'll cover why next.

~/.vim/

I have a directory called ~/.vim/ftplugin/ that files associated with different filetypes. For example, here are some of the files in my ftplugin folder:

java.vim
tex.vim
python.vim
ruby.vim

Any time a file of the corresponding type is opened, the appropriate file will be called to initialize Vim. Here's the contents of ruby.vim:

setlocal ai sts=2 sw=2 ts=2 et
iab <buffer> fe each do
iab <buffer> fewi each_with_index do

It's a very simple file, but it shows a new commands. The first line redefines the tab stops to 2 spaces instead of the 4 I used in my .vimrc. But note that here I use "setlocal" instead of "set"; this will cause this change to only affect the current file, so my non-Ruby files keep the default indentation. The next two lines define abbreviations that only work in insert mode and are only applied to the current file. So if I'm editing a Ruby file:

['one','two','three'].fe

Will be expanded to:

['one','two','three'].each do

This is really a trivial use of an abbreviation, but I'm sure you can see how you could use the feature to save a large number of keystrokes.

As I stated earlier, you can put just about any folder present in your Vim installation directory under ~/.vim/ for customized options. For example, ~/.vim/colors/ can hold color schemes you create or download from the internet, ~/.vim/plugin/ can hold new plugins, ~/.vim/doc/ can hold documentation for your plugins, &tc. You can heavily customize your personal Vim files without affecting any other users on the system.

Final Notes

In the interest of keeping this short, I'll only show one more thing: my .gvimrc file

color koehler
let use_xhtml = 1
let html_use_css = 1
let html_number_lines = 1

These settings are only applied when the GUI version of Vim loads. The first sets a the color scheme to one of the color files included in the Vim installaion. The next lines define a few variables that are used by the TOhtml command. That command will format an HTML file that displays the contents of your current window with the same markup that you see inside Vim. I used this command to create the syntax highlighted versions of my config files for this HowTo. You can look up these commands and more using the vim help files. Plus check out the Vim Scripts and Vim Tips sections on Vim.org; they contain thousands of enhancements and useful tricks.