Wednesday, 18 March 2009

Per project .vimrc

My natural C indentation style is basically kernel-like and my ~/.vimrc reflects that. Unfortunately I have to hack on GNUish-style projects and I really don't want to edit my ~/.vimrc every single time I switch between different indentation styles.

Modelines are evil.

To solve that terrible issue, vim can use per directory configuration files. To enable that neat feature only two little lines are needed in your ~/.vimrc:
set exrc   " enable per-directory .vimrc files
set secure   " disable unsafe commands in local .vimrc files
Then it's just a matter of writing a per project .vimrc like this one:
set tabstop=8
set softtabstop=2
set shiftwidth=2
set expandtab
set cinoptions=>4,n-2,{2,^-2,:0,=2,g0,h2,t0,+2,(0,u0,w1,m1
You can find help with the wonderful cinoptions variable in the Vim documentation. As sane persons open files from the project's root directory, this works like a charm. As for the Makefiles, they are special anyway, you really should add an autocmd in your ~/.vimrc.
" add list lcs=tab:>-,trail:x for tab/trailing space visuals
autocmd BufEnter ?akefile* set noet ts=8 sw=8 nocindent


  1. Mumm...It's what I need!
    Thx Damien! :D

  2. Hi, 'secure' doesn't seem to be that secure. eg "echo system('cat .vmirc')" is executed !? As alternative you can give a try which also walks up the directory tree. It keeps a (simple) hash of the local rc file, if that changes you must confirm sourcing it. Anyway do your own tests.

  3. I was wrong - it only does so if the file is owned by you. If its owned by other users you can't run system or ! commands

  4. opening a file from a random tarball containing a .vimrc file can execute arbitrary shell commands.

  5. EXACTLY what I was looking for! Thank you! This is going to be invaluable for per-project `wildignore` values for ignoring dirs when using command-t

  6. @unsecure

    On the other hand, if you run stuff like './', './configure' and 'make', you are facing the same problems.