How to automatically set indentation of files in vim

Tyler Tyler (291)
Total time: 1 minute 

This guide will teach you how to set the indentation of files in Vim automatically.

Posted in these interests:
h/vim23 guides

Since you're trying to configure vim I'll assume you want to edit the file in vim. The .vimrc file is usually located in your home directory.

vim ~/.vimrc
autocmd FileType python setl tabstop=4|setl shiftwidth=4|setl softtabstop=4
autocmd FileType html setl tabstop=2|setl shiftwidth=2|setl softtabstop=2
autocmd FileType javascript setl tabstop=2|setl shiftwidth=2|setl softtabstop=2
autocmd FileType css setl tabstop=2|setl shiftwidth=2|setl softtabstop=2
autocmd Filetype ruby setlocal tabstop=2|setl shiftwidth=2|setl softtabstop=2

Feel free to adjust the values based on your preferences and coding conventions you use.