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: */

7 comments:

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

    ReplyDelete
  2. I found this solution useful:

    http://git.sv.gnu.org/gitweb/?p=coreutils.git;a=commitdiff;h=6ab90b53f

    ReplyDelete
  3. 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.

    ReplyDelete
  4. 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

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

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

    ReplyDelete
  7. 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) https://github.com/MarcWeber/vim-addon-local-vimrc

    Works just fine.

    ReplyDelete

Comments are moderated thanks to spammers