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

7 comments:

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

    ReplyDelete
  2. Hi, 'secure' doesn't seem to be that secure. eg "echo system('cat .vmirc')" is executed !? As alternative you can give github.com/MarcWeber/vim-addon-local-vimrc 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.

    ReplyDelete
  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

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

    ReplyDelete
  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

    ReplyDelete
  6. @unsecure

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

    ReplyDelete