Setting Vim whitespace preferences by filetype

nelstrom picture nelstrom · Oct 13, 2009 · Viewed 43.4k times · Source

At my work, I am required to follow the house style for indentation, which goes as follows:

  • 2 spaces when coding html and ruby
  • tabs when coding javascript, with tabwidth=4 recommended

What is the best way to specify different whitespace preferences per filetype?

Answer

Peter picture Peter · Oct 13, 2009

there are many ways, but here's a simple, easy to understand way. add these lines to your ~/.vimrc:

autocmd FileType html setlocal ts=2 sts=2 sw=2
autocmd FileType ruby setlocal ts=2 sts=2 sw=2
autocmd FileType javascript setlocal ts=4 sts=4 sw=4