Search code examples
vimpluginsindentation

How can I achieve project specific indentation in vim?


I'm working on several projects, each of which uses different indentation style (for various filetypes). For example 1 tab per indentation level, 2 or 4 spaces etc. How can I automate switching between these different styles? I normaly prefer indenting with tabs, but I'm tired of having to type :set expandtabs all the time when working with space-indented code. Possible solutions would include loading a piece of vim configuration based on file path or some configuration in the project root. Is there a plugin to solve this for me in an elegant way?


Solution

    1. Look at the cinoptions option and softtabstop option (and expandtab, but you know that).
    2. In your '~/.vimrc', define buffer entry auto-commands for each directory where you keep sources of some project like:

      augroup ProjectSetup
      au BufRead,BufEnter /path/to/project1/* set et sts=2 cindent cinoptions=...
      au BufRead,BufEnter /path/to/project2/* set noet sts=4 cindent cinoptions=...
      augroup END
      

      If the project has mixture of languages and needs different settings for then, you can also add extensions like:

      au BufRead,BufEnter /path/to/project1/*.{c,h} set noet sts=0 cindent cinoptions=...
      au BufRead,BufEnter /path/to/project1/*.py set et sts=4