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 bar.py 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


50 Responses to “Configuring Vim right”

  1. Pádraig Brady Says:

    Oh great stuff.
    vim is a great editor but the defaults stink.
    Here are my settings:
    http://www.pixelbeat.org/settings/.vimrc

  2. Keith Says:

    Very nice – thanks!

  3. Stephen Bach Says:

    Pádraig, very nice. I didn’t know about wildignore or nojoinspaces.

  4. Maxim Says:

    I don’t often take the time to leave a note in an article, but this time I had to. This is a good, useful post: some handy stuff about VIM I didn’t already know! Thanks!

  5. Daniel Says:
    " you can also toggle through the matches by tab
    set wildmode=list:longest,full
    
    " use confirm instead of aborting an action
    set confirm
    
    " current directory is always matching the
    " content of the active window
    set autochdir
    
    " remember some stuff after quiting vim:
    " marks, registers, searches, buffer list
    set viminfo='20,<50,s10,h,%
    
    " use console dialogs instead of popup
    " dialogs for simple choices
    set guioptions+=c
    
  6. Dev Blog AF83 » Blog Archive » Veille technologique : Merb 1.0, Microsoft, HTML5, etc. Says:

    […] http://items.sjbach.com/319/configuring-vim-right : quelques astuces pour Vim. […]

  7. Daniel Says:

    Plugins I can’t live without:

    toggle comments
    http://www.vim.org/scripts/script.php?script_id=665

    auto completion by tab
    http://www.vim.org/scripts/script.php?script_id=182

    svn/cvs/git integration
    http://www.vim.org/scripts/script.php?script_id=90

    fast switching between most recently used files
    http://www.vim.org/scripts/script.php?script_id=42

    fast switching between h/cpp
    http://www.vim.org/scripts/script.php?script_id=31

    Nice to have plugins:

    emacs style quick copy and paste
    http://www.vim.org/scripts/script.php?script_id=2064

    auto completion in command mode from current file
    http://www.vim.org/scripts/script.php?script_id=2222

    see the classes, methods, functions,
    macros defined in the file
    http://www.vim.org/scripts/script.php?script_id=273

  8. Stephen Bach Says:

    Daniel, I didn’t know about confirm or autochdir — those could certainly be useful.

  9. Anton Olsen.com » Blog Archive » Bookmarks for November 10th Says:

    […] items.sjbach.com » Configuring Vim right – These are the best non-standard options I have 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 – are tips which should have value to anyone, no matter how they like to run Vim. That is, there is minimal editorializing. […]

  10. Cale Gibbard Says:

    I also rather like set expandtab and set smarttab, being someone who absolutely hates accidentally placing hard tab characters in source code.

  11. Cristian Says:

    A big thank you for this!

  12. Alex Chapman Says:

    Perhaps number 5 would be better written as:

    nnoremap 3
    nnoremap 3

  13. Alex Chapman Says:

    Damn, I knew that wouldn’t format properly. How about this:

    nnoremap <C-e> 3<C-e>
    nnoremap <C-y> 3<C-y>
    
  14. Stephen Bach Says:

    Cale, me too. I like to handle this using autocmd Filetype so that it’s set at a finer granularity.

    Alex, yes! I’ve modified the article. Thanks.

  15. spispopd Says:

    “The default leader is \, but this is not located standardly on all keyboards and requires a pinky stretch in any case. ”

    On UK/Ireland keyboards it’s just between left shift and Z, doesn’t even need shift. It’s positioned pleasingly roughly symmetrically with / on the right.

  16. Scott Says:

    Hi

    Good list, here’s a few I add to that list.

    http://blog.learnr.org/post/59098925/configuring-vim-some-more

    Doesn’t include plugins too, which can be quite useful. I like yankring and autotag.

  17. Bob Says:

    Nice! Thanks. I’ve added a few to my .vimrc file.

    A small point, using <SPACE> as a leader is a bit problematic if you use it to move to the next char.

  18. Razee Marikar Says:

    Hi,

    The file $VIMRUNTIME/macros/matchit.txt says that matchit.vim is a plugin, and that putting it in ~/.vim/plugin will autoload it (no need to edit config file), and it also says how to install the associated help file.

  19. Stephen Bach Says:

    Razee, that’s fine. matchit.vim has been distributed with Vim for over eight years, so it’s unlikely you’ll see an installation where it’s missing — but it doesn’t hurt to be sure.

  20. vim configuration tips. Three I didn’t … » jon.smajda.com/blog Says:

    […] vim configuration tips. Three I didn’t know and now use: set title "set terminal title to filename set wildmode=list:longest " for bash-like tab-completion set scrolloff=3 "keep 3 lines context when scrolling […]

  21. Spencer Alexander Says:

    Great post! I’ve been playing around with all of the defaults you spoke about in your article, but I definitely missed a few important ones! It’s also quite nice to hear the opinion of a seasoned vim expert.

  22. Stephen Bach Says:

    Spencer, glad the article was helpful!

  23. E. Colon Says:

    Thanks for the tips. I really needed something like this to completely make the switch from a gui text editor to vim. I feel a lot more comfortable now. Hats off!

  24. vim mind share soaring: roundup of 10 vim articles, recent and older gems — be present now Says:

    […] Configuring Vim Right […]

  25. 20081127 網摘 - 中文網誌年會 - 網絡暴民 Jacky’s Blog Says:

    […] Configuring Vim right […]

  26. vimrc updated - /dev/null Says:

    […] that the vimrc I provide via bzr (or via direct link) has been updated.  Tom recently found a site or two that had some nifty settings I hadn’t stumbled across yet.  Changes […]

  27. Making Vim your default | stackq Says:

    […] Configuring Vim Right […]

  28. Jim Says:

    Nice writeup. I would recommend against your suggestion #2, however. Apostrophe is a line movement command, and Backtick is a text movement command. Since vim is both line editor and a text editor, it makes a big difference which you use. Personally, I use line commands way more often than text commands, as they are quicker to fire off. “ma j j j d'a” will cut 4 lines of text, no matter what column your cursor is in. Likewise, you can paste these w/o regard for the cursor column and vim does the right thing (@" is marked as a line register from the cut). I guess this thing would depend on your usage pattern of course.

  29. Stephen Bach Says:

    Hi Jim, you have a point. I rely heavily on visual-mode, so I would more likely do this as “V j j j d” — I tend to use marks only for navigating text, rather than for manipulating it. But this isn’t true of everyone and it’s good to get a different view.

  30. Steve Francia Says:

    Vim is a good editor, made great with a custom .vimrc file.
    My .vimrc file adds syntax highlighting, autocomplete with tab, folding and highlights the line you are on.
    Feel free to use or modify it for your needs.

    Here is my .vimrc file


    And my guide to the best 15 vim plugins

  31. craschworks » Blog Archive » links for 2009-06-25 Says:

    […] Configuring Vim right – items.sjbach.com have spent a lot of time looking at a Vim window, and correspondingly, a lot of time testing different configurations. These are the best non-standard options I have 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 â are tips which should have value to anyone, no matter how they like to run Vim. That is, there is minimal editorializing. (tags: programming/vim vim, vimrc) […]

  32. Adam Katz Says:

    Refined the gvimrc portion you inherited from me to better handle sizing (especially useful for gvimdiff), fixed (upgraded?) buggy matchit line:

    " who needs .gvimrc?  (people in irc://irc.freenode.net#vim say this is limited)
    if has('gui_running')
      set guifont=Monospace 7.5   " this is not limited to integers
      if &diff
        autocmd GuiEnter * set columns=165 lines=50
      else
        autocmd GuiEnter * set columns=80 lines=40
      end
    end
    set diffopt+=iwhite             " ignore whitespace in diff mode
     
    " from sbach, http://items.sjbach.com/319/configuring-vim-right
    " and http://www.vim.org/scripts/script.php?script_id=39
    "runtime macros/matchit.vim
    filetype plugin on
    source ~/.vim/plugin/matchit.vim
  33. Making vi more colourful on the mac « fredsherbet.com Says:

    […] This site has lots of useful configuration options to consider to make vim more useful. […]

  34. Ben Thomasson Says:

    Thanks, I’ve been using vim for years and I’ve learned quite a few things from this post.

  35. Luca G. Says:

    Thanks! This post is incredibly useful!

  36. David Rivers Says:

    I just added several of these to my .vimrc. Thanks a bunch!

  37. Frazer Says:

    thanks for this post. just what I was looking for.

    what does this part of your .vimrc do?

    ” “Very magic” regexes in searches
    “nnoremap / /\v
    “nnoremap ? ?\v

  38. Stephen Bach Says:

    Those modify the /- and ?-style searches to use “very magic” regular expressions; see :help magic. Basically, you don’t need to escape some regex special characters like you usually do. It’s not perfect, though, so I keep it disabled these days.

  39. Dieter_be Says:

    Thanks, some useful tips like the title and scrolloff 🙂

  40. My notes on vim « linux-well Says:

    […] .vimrc file that is stored in your home directory. There is a fair amount of things that can be configured. And with right configuration vim is godlike text […]

  41. Anonymous Says:

    Thank you!
    my eyes can finally stop hurting 🙂

  42. Learning the Vi Editor | Eclecti.ca Says:

    […] Configuring Vim Right […]

  43. Fede Says:

    Thank you, this was really useful

  44. Kris Says:

    If your a Rubyist I put the recommendations here in to my Vimfile: https://github.com/krisleech/vimfiles

  45. frinty Says:

    You wrote clearly and accurately. That made the ideas easier to understand. Thanks for this very helpful tutorial.

    Frinty

  46. kevinpaulconnor.com Project Blog › How to configure vim Says:

    […] Everyone has their own opinion about this, but I found this post to be useful. This was written by admin. Posted on Tuesday, December 27, 2011, at 6:41 pm. Filed under vim. Bookmark the permalink. Follow comments here with the RSS feed. Post a comment or leave a trackback. […]

  47. Quora Says:

    What are some good resources for learning Vim?…

    Okay, this iis going to be a complete hodgepodge of blag posts, command sheets, and Vim 101 posts, and editorials. Look at a whole variety of you’re just starting out, so you can be exposed to a huge variety of views. First thing: Learn about the sema…

  48. Jason Says:

    thx for your article, it helps me a lot 🙂

  49. Can I use SPACE as mapleader in VIM? | Ask Programming & Technology Says:

    […] http://items.sjbach.com/319/configuring-vim-right I got that you were supposed to be able to use SPACE as the mapleader in vim. I’ve tried but […]

  50. 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.