Configuring Vim right

Published November 6th, 2008

I’ve spent a lot of time nerding into a Vim window, and ergo, a lot of time fooling around with different configurations. These are the best non-standard options I’ve found or stolen from others over the years. Listed below in order of descending usefulness — though I think everything in this article is worth skimming — is an omnium-gatherum of tips that should have value to any developer, no matter how they like to run Vim. That is, minimal editorializing.

Note: no plugins are covered here, just vanilla Vim.

Essential .vimrc configuration items

For whatever reason, the following options aren’t set by default, but they should be.

  1. Turn on hidden

    Don’t worry about the name. What this does is allow Vim to manage multiple buffers effectively.

    • The current buffer can be put to the background without writing to disk;
    • When a background buffer becomes current again, marks and undo-history are remembered.

    Turn this on.

    set hidden

  2. Remap ` to '

    These are very similar keys. Typing 'a will jump to the line in the current file marked with ma. However, `a will jump to the line and column marked with ma.

    It’s more useful in any case I can imagine, but it’s located way off in the corner of the keyboard. The best way to handle this is just to swap them:

    nnoremap ' `
    nnoremap ` '

  3. Map leader to ,

    The leader character is your own personal modifier key, as g is Vim’s modifier key (when compared to vi). The default leader is \, but this isn’t located standardly on all keyboards and requires a pinky stretch in any case.

    let mapleader = ","

    <SPACE> is also a good choice. Note: you can of course have several “personal modifier keys” simply by mapping a sequence, but Vim handles the leader key more formally.

  4. Keep a longer history

    By default, Vim only remembers the last 20 commands and search patterns entered. It’s nice to boost this up:

    set history=1000

  5. Enable extended % matching

    The % key will switch between opening and closing brackets. By sourcing matchit.vim — a standard file in Vim installations for years — the key can also switch among e.g. if/elsif/else/end, between opening and closing XML tags, and more.

    runtime macros/matchit.vim

    Note: runtime is the same as source except that the path is relative to the Vim installation directory.

  6. Make file/command completion useful

    By default, pressing <TAB> in command mode will choose the first possible completion with no indication of how many others there might be. The following configuration lets you see what your other options are:

    set wildmenu

    To have the completion behave similarly to a shell, i.e. complete only up to the point of ambiguity (while still showing you what your options are), also add the following:

    set wildmode=list:longest

Recommended .vimrc configuration items

Most people like these.

  1. Use case-smart searching

    These two options, when set together, will make /-style searches case-sensitive only if there is a capital letter in the search expression. *-style searches continue to be consistently case-sensitive.

    set ignorecase 
    set smartcase

    This is usually the most useful combination.

  2. Set the terminal title

    A running gvim will always have a window title, but when vim runs within an xterm, by default it inherits the terminal’s current title.

    set title

    This gives e.g. | page.html (~) - VIM |.

  3. Maintain more context around the cursor

    When the cursor moves outside the viewport of the current window, the buffer scrolls a single line to keep the cursor in view. Setting the option below will start the scrolling three lines before the border, keeping more context around where you’re working.

    set scrolloff=3

    Typing zz is also handy; it centers the window on the cursor without moving the cursor. (But beware ZZ, which does something entirely different!)

  4. Store temporary files in a central spot

    Swap files and backups are annoying but can save you a lot of trouble. Rather than spread them all around your filesystem, isolate them to a single directory:

    $ mkdir ~/.vim-tmp  # or whatever

    And in .vimrc:

    set backupdir=~/.vim-tmp,~/.tmp,~/tmp,/var/tmp,/tmp
    set directory=~/.vim-tmp,~/.tmp,~/tmp,/var/tmp,/tmp

    This is especially valuable after an unexpected reboot — you don’t have to track down all the leftover temp files. However: if you are editing files on a shared file system, it’ll be easier to clobber concurrent modifications, as other users’ Vim processes won’t see your swaps.

  5. Scroll the viewport faster

    <C-e> and <C-y> scroll the viewport a single line. I like to ratchet this up:

    nnoremap <C-e> 3<C-e>
    nnoremap <C-y> 3<C-y>

  6. Enable limited line numbering

    It’s often useful to know where you are in a buffer, but full line numbering is distracting. Setting the option below is a good compromise:

    set ruler

    Now in the bottom right corner of the status line there will be something like: 529, 35 68%, representing line 529, column 35, about 68% of the way to the end of the buffer.

  7. A bunch of stuff your OS should already do

    If you are running Windows or OS X or a sloppy Linux distribution, you may not be using these:

    " Intuitive backspacing in insert mode
    set backspace=indent,eol,start
    " File-type highlighting and configuration.
    " Run :filetype (without args) to see what you may have
    " to turn on yourself, or just set them all to be sure.
    syntax on
    filetype on
    filetype plugin on
    filetype indent on
    " Highlight search terms...
    set hlsearch
    set incsearch " ...dynamically as they are typed.

    The filetype lines enable type-specific configuration, such as knowledge of syntax and indentation. E.g. foo.c will be opened with Vim’s pre-configured C settings, and will be opened with Python settings.

    If the search term highlighting gets annoying, set a key to switch it off temporarily:

    nmap <silent> <leader>n :silent :nohlsearch<CR>

  8. Catch trailing whitespace

    The following will make tabs and trailing spaces visible when requested:

    set listchars=tab:>-,trail:ยท,eol:$
    nmap <silent> <leader>s :set nolist!<CR>

    By default whitespace will be hidden, but now you can toggle with ,s.

  9. Stifle many interruptive prompts

    The “Press ENTER or type command to continue” prompt is jarring and usually unnecessary. You can shorten command-line text and other info tokens with, e.g.:

    set shortmess=atI

    See :help shortmess for the breakdown of what this changes. You can also pare things down further if you like.

  10. Stop distracting your co-workers

    Vim is a sullen ally, beeping aspersions at every minor slip. You can either find a way to turn off the bell completely, or more usefully, make the bell visual:

    set visualbell

    Instead of hearing that obnoxious beep, you’ll see a brief window flash. This is similar to screen‘s interpretation of the bell in its default configuration.

Here is my own .vimrc, which includes all these settings (and some more which are less generally useful). A fairly good source for other configuration tips is the Vim Tips Wiki.

Thanks to Adam Katz and Chris Gaal for their comments and suggestions.

Further discussion on the programming reddit

51 Responses to “Configuring Vim right”

  1. Vim Options Says:

    That’s a nice intro into setting up a vim config file for a beginner like me. If you are lazy to type it in all by yourself consider using an app like Vim Options.

Leave a Reply

XHTML: You can use these tags: <a href=""> <b> <i> <code> <pre lang="">
Languages for <pre> include: commonlisp, emacslisp, vim, ruby, clojure, bash