163

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?

nelstrom
  • 18,802
  • 13
  • 54
  • 70
  • 1
    possible duplicate of [Changing Vim indentation behavior by file type](http://stackoverflow.com/questions/158968/changing-vim-indentation-behavior-by-file-type) – acgtyrant Jul 12 '15 at 12:54
  • All these answers just made me more confused. The problem is that the options are up to preference. The accepted answer has a nice syntax though. This was an excellent guide: http://vimcasts.org/transcripts/2/en/ – Lindlof Feb 02 '16 at 07:03

5 Answers5

258

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
tbitai
  • 473
  • 5
  • 5
Peter
  • 127,331
  • 53
  • 180
  • 211
  • 31
    ts = 'number of spaces that in file uses' sts = 'number of spaces that uses while editing' sw = 'number of spaces to use for (auto)indent step' for details see: http://vimdoc.sourceforge.net/htmldoc/quickref.html#option-list – zdsbs Jan 03 '14 at 04:18
  • I needed to append expandtab at least for ruby, see answer by 'too much php' here – Michael Durrant May 21 '19 at 13:56
91

Peter's answer is straightforward enough, but unfortunately the options aren't right. You need to use the following options instead:

autocmd Filetype html setlocal ts=2 sw=2 expandtab
autocmd Filetype ruby setlocal ts=2 sw=2 expandtab
autocmd Filetype javascript setlocal ts=4 sw=4 sts=0 noexpandtab

Also note:

  • You can make vim show tab characters by using :set list.
  • Once you have the tab/space options set correctly, you can make vim repair the file (replace spaces with tabs or vice versa) using the :retab! command.
too much php
  • 88,666
  • 34
  • 128
  • 138
  • 2
    What's the benefit of `expandtab` over `sts=2` – James McMahon Oct 14 '13 at 02:58
  • 12
    @JamesMcMahon expandtab expands all tabs to spaces. sts (softtabstop) inserts spaces and tabs for indents: as many tabs as will fit in the indent based on the size of tabstop, and then spaces after that. Of course, if expandtab is on, all the tabs that get inserted are converted to spaces. http://stackoverflow.com/questions/1562336/tab-vs-space-preferences-in-vim might help further. Without expand tab, Peter's answer would insert tabs that are 2 chars wide, not spaces. – ajmccluskey Apr 28 '14 at 11:02
38

+1 to Peter's answer, but Vim provides another solution as well. If you want to do something more complicated than a single setlocal, like setting up a whole bunch of options, commands, and mappings at once, then vim's filetype plugin feature comes to the rescue.

You need to have filetype plugin on or filetype plugin indent on in your .vimrc, and then to create a plugin for e.g. ruby you can create ~/.vim/ftplugin/ruby.vim. Technically you can use any commands you like in here, to be run when a Ruby file is loaded, but the recommended ones include setlocal, map <buffer>, command -buffer, and defining functions. Lots more information is in the User Guide; if you're pretty familiar with scripting vim then jump to :help 41.11, otherwise read :help usr_40 and :help usr_41.

hobbs
  • 223,387
  • 19
  • 210
  • 288
11

There's also a nice vim script: DetectIndent which tries to detect the indentation of a file that you open. It's very handy if you work with many files with different coding style.

I use an autocommand in my .vimrc:

:autocmd BufReadPost * :DetectIndent 
Benedikt Waldvogel
  • 12,406
  • 8
  • 49
  • 61
1

To insert space characters whenever the tab key is pressed, set the 'expandtab' option:

:set expandtab

Next step is to control the number of space characters that will be inserted when the tab key is pressed, set the 'tabstop' option. For example, to insert 2 space for a tab, use:

:set tabstop=2

ref: http://vim.wikia.com/wiki/Converting_tabs_to_spaces

bartoindahouse
  • 411
  • 2
  • 5