How can I achieve project specific indentation in vim?
- Look at the
cinoptions
option andsofttabstop
option (andexpandtab
, but you know that). 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
Yes, there is: If you're using the Project Plugin, you could specify a file whose content is being evaluated every time you open a file of the project (this file is called in.vim
). The opposite of in.vim
is out.vim
: this one's executed every time you leave the project.
I use the plugin localvimrc, which does exactly what you are asking for:
Sometimes, when you work on different projects, you have the problem, that they use different indentation, tab expansion and so on. You need vimrc for each project that overrides your prefered settings from ~/.vimrc
EditorConfig and it's Vim plugin:
What is EditorConfig?
EditorConfig helps developers define and maintain consistent coding styles between different editors and IDEs. The EditorConfig project consists of a file format for defining coding styles and a collection of text editor plugins that enable editors to read the file format and adhere to defined styles. EditorConfig files are easily readable and they work nicely with version control systems.
Among few other things it allows you to set indentation, which will be applied only for this project. It's very simple and most importantly standardized way supported by many different editors and IDE-s, so that it will set the proper indentation not only for you but likely all people working on a project.
You just need to create a .editorconfig
file in the project root and Vim automatically finds it (assuming you have the plugin installed), setting the proper values. Creating .editorconfig
files for each project achieves what you need - project specific indentation.
Example config file:
root = true
[*]
charset = utf-8
end_of_line = lf
indent_style = space
indent_size = 4
insert_final_newline = true
trim_trailing_whitespace = true
[*.{js,html}]
indent_size = 2
[*.css]
indent_size = 4