Wednesday, May 9, 2012

vimdir for project-specific vim settings

Sometimes it feels that each project I work on has different indentation settings. Not quite true but still annoying. I don't know of a way to tell vim to auto-detect the indentation settings based on the current file (which, for X.Org projects wouldn't work anyway) but what has been incredibly useful is the vimdir script. It simply scans the directory tree upwards to find a .vimdir file and loads the settings from there. So I keep files like this around:
setlocal noexpandtab shiftwidth=8 tabstop=8
The alternative is to add a snippet to the file itself but not every maintainer is happy with that.
/* vim: set noexpandtab tabstop=8 shiftwidth=8: */


Ciaran McCreesh said...

It's worth noting that that plugin will execute arbitrary code, so you may wish to avoid it if you ever checkout code written by naughty people.

Pádraig Brady said...

I found this solution useful:;a=commitdiff;h=6ab90b53f

Ray said...

I have this in my ~/.vimrc

autocmd BufRead * silent %g/→ /set noexpandtab
autocmd BufRead * silent %g/^ \+{$→ /set cinoptions={.5s,:.5s,+.5s,t0,g0,^-2,e-2,n-2,p2s,(0,=.5s
autocmd BufRead * silent %g/.*if .*) {$→/set cinoptions=:0,l1,t0,g0,{s

(where → is a tab character)

It's not perfect but it helps quite a bit.

Leif said...

I often include a .vimrc in the root of my projects. This vimdir script is pretty close to exactly what I wanted. Sadly it expects a .vimdir file and not a .vimrc

Unknown said...

au! BufRead,BufNewFile *linux*/*.[ch] source ~/.vim/after/ftplugin/c-linux.vim

PiPo said...

@Ciaran: that's what |:sandbox| should prevent. It allows just limited set of options.

Anonymous said...

I think the best solution is to have in your ~/.vim/bundle git repo submodule (you have your ~/.vim/bundle in git repo, right? You should)

Works just fine.