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

8 thoughts on “Per project .vimrc

  1. Marc Weber

    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.

  2. Marc Weber

    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

  3. unsecure

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

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>