Indenting source code - Vim Tips Wiki

Different settings for different file typesEdit Different settings for different file types sectionEdit

You may want indentation for html files to use tabs with 2-columns per indent, while Python files use spaces with 4-columns per indent. To apply suitable settings automatically, first enable file type detection with the following in your vimrc:

filetype plugin indent on

Create file html.vim with contents:

setlocal shiftwidth=2
setlocal tabstop=2

and file python.vim with contents:

setlocal expandtab
setlocal shiftwidth=4
setlocal softtabstop=4

The html.vim and python.vim files should be in this directory (which you may need to create):

  • ~/.vim/after/ftplugin on Unix-based systems; or
  • $HOME/vimfiles/after/ftplugin on Windows systems

The standard plugins probably do not change settings such as shiftwidth, and in that case the directory ~/.vim/ftplugin (or $HOME/vimfiles/ftplugin) would work as an alternative. However the "after" directory should be used because you intend to override settings from other plugins.

Using the "after" directory as above is recommended, but it is possible to put commands such as the following in your vimrc as an alternative:

autocmd FileType html setlocal shiftwidth=2 tabstop=2
autocmd FileType python setlocal expandtab shiftwidth=4 softtabstop=4

Useful PHP tip