I have done some googling and I'm surprised that I haven't found this. COuld someone explain to me simply the easiest way to change the indentation behavior of vim based on the file type? For instance if I open a python file it would indent with 2 spaces, but if I open powershell it would use 4 spaces.

+4  A: 

Put autocmd commands based on the file suffix in your ~/.vimrc

autocmd BufRead,BufNewFile   *.c,*.h,*.java set noic cin noexpandtab
autocmd BufRead,BufNewFile   *.pl syntax on

The commands you're looking for are probably ts= and sw=

Paul Tomblin
+3  A: 

I usually work with expandtab set, but that's bad for makefiles. I recently added:

:autocmd FileType make set noexpandtab

to the end of my .vimrc file and it recognizes Makefile, makefile, and *.mk as makefiles and does not expand tabs. Presumably, you can extend this.

Jonathan Leffler
The better option is to enable :filetype plugins. The default one for Vim includes :setl noet, so you don't even need that aucmd in your vimrc.
OK. Can you explain the benefits of that, and what is involved in doing it? Why are filetype plugins better than autocmd? When should autocmd be used? Not used?
Jonathan Leffler
The filetype plugins that come with Vim will do helpful things like "setlocal noexpandtab" for makefiles, for example. Autocommands vs ftplugins for personal things like shiftwidth don't matter--it's just how you choose to structure your vim config.
+5  A: 

Use ftplugins or autocommands to set options. (:h ftplugin for more information)

In ~/.vim/ftplugin/python.vim:

setlocal sw=2 sts=2 et

And don't forget to turn them on in ~/.vimrc

filetype plugin indent on

Or in ~/.vimrc

au FileType python setl sw=2 sts=2 et

I would also suggest learning the difference between 'ts' and 'sts'. A lot of people don't know about 'sts'.

+15  A: 

You can add .vim files to be executed whenever vim switches to a particular filetype.

For example, I have a file ~/.vim/after/ftplugin/html.vim with this contents:

setlocal shiftwidth=2
setlocal tabstop=2

Which causes vim to use tabs with a width of 2 characters for indenting (the noexpandtab option is set globally elsewhere in my configuration).

This is described here:, scroll down to the section on filetype plugins.

You should put that in `~/.vim/after/ftplugin/html.vim` instead. But as others have pointed out below, it’s much nicer to just add `autocmd FileType html setlocal shiftwidth=2 tabstop=2` to your `.vimrc`.
Aristotle Pagaltzis
Whoops, actually, that /is/ where I have that file. I'll fix the answer. I disagree though, I think separating out commands for different filetypes into separate files makes everything much easier, especially if you have requirements for many filetypes, or lots of options for some filetypes.
Actually, there's not much reason to use the after directory for ftplugins. Vim will load all of them it finds in your runtimepath, not just the first like for syntax files.
+1  A: 

While you can configure Vim's indentation just fine using the indent plugin or manually using the settings, I recommend using a python script called Vindect that automatically sets the relevant settings for you when you open a python file. Use this tip to make using Vindect even more effective. When I first started editing python files created by others with various indentation styles (tab vs space and number of spaces), it was incredibly frustrating. But Vindect along with this indent file

Also recommend: