Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I achieve project specific indentation in vim?

Tags:

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?

like image 396
VoY Avatar asked May 30 '11 08:05

VoY


People also ask

How do I change the indentation in Vim?

To indent the current line, or a visual block: ctrl-t, ctrl-d - indent current line forward, backwards (insert mode) visual > or < - indent block by sw (repeat with . ) then try hitting the F5 key while in insert mode (or just :set paste ).

How do I indent a whole file in Vim?

Start in the top of a file (to get there, press gg anywhere in the file.). Then press =G , and Vim will fix the indentation in the whole file. If you don't start in the beginning of the file, it will fix indentation from current line to the bottom of file.


1 Answers

  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 
like image 139
Jan Hudec Avatar answered Oct 01 '22 19:10

Jan Hudec