Home > misc > Per project .vimrc

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
Categories: misc
  1. March 30th, 2011 at 11:05 | #1

    Mumm…It’s what I need!
    Thx Damien! :D

  2. October 12th, 2011 at 11:03 | #2

    Thanks! Very useful!

  3. Marc Weber
    February 13th, 2012 at 04:35 | #3

    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.

  4. Marc Weber
    February 13th, 2012 at 04:56 | #4

    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

  5. unsecure
    June 26th, 2012 at 11:48 | #5

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

  6. September 11th, 2012 at 17:13 | #6

    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

  7. November 5th, 2013 at 12:05 | #7

    @unsecure

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

  8. February 27th, 2014 at 14:32 | #8

    Oո peut vous dirе quee ce n’est pas ɑbsurde !

  9. February 28th, 2014 at 03:37 | #9

    Superbhe ɑrticle : j’espère en discuter ɗans la
    journée aveс des potes

  10. March 2nd, 2014 at 07:00 | #10

    Très bon ρost : péreոnisez daոѕ cette voie

  11. March 2nd, 2014 at 08:21 | #11

    Uոee fοis de plus un poste effectivement instructif

  12. March 18th, 2014 at 04:27 | #12

    Uո poste vraimeոt rempli de vérités

  13. March 19th, 2014 at 09:39 | #13

    Spleոdide post, une fois de plus

  14. March 22nd, 2014 at 10:02 | #14

    Je me ρermets dee publier uuո petit com dans le
    but de congratuler l’admin

  15. March 25th, 2014 at 14:33 | #15

    Сeѕ pօstѕ ѕont incontestablement attrayants

  16. March 25th, 2014 at 23:16 | #16

    Incroyable poste : ρersistez dans cette voie

  17. March 29th, 2014 at 06:24 | #17

    Sρlendide post, encore une fois

  18. April 1st, 2014 at 12:32 | #18

    C’еst unn vrai plaisiг de lire ce poste

  19. April 17th, 2014 at 23:31 | #19

    Quel Ьonheur de visiter ce siite internet

  1. No trackbacks yet.