How do I set up different tab settings for different languages in Vim?

These other answers seem way too complex. Instead of messing around with yet more directories and files in your ~/.vim tree, just add the following to your ~/.vimrc.

autocmd Filetype python setlocal expandtab tabstop=4 shiftwidth=4 softtabstop=4

(you can be l33t and abbreviate parameters to et ts=4 sw=4 sts=4). I found this in Setting Vim whitespace preferences by filetype


Just put the settings into the filetype plugin file ~/.vim/ftplugin/LANGUAGE.vim . My ~/.vim/ftplugin/perl.vim contains the lines:

"
" ---------- tabulator / shiftwidth --------------------
"  Set tabulator and shift width to 4 (Perl Style Guide)
"
setlocal  tabstop=4
setlocal  shiftwidth=4
"

These settings will automatically be in effect for each file with file type 'perl' (new or existing).


My answer is based on this tip on the VIM Wiki. This answer uses the "after" directory so you won't have to muck with the supplied plugin files for different filetypes.

For example, to specify custom settings for Python files, create a file called python.vim to hold your Python settings:

setlocal expandtab
setlocal shiftwidth=4
setlocal softtabstop=4

Place this file in either

  • ~/.vim/after/ftplugin (Linux)
  • $HOME/vimfiles/after/ftplugin (Windows)

And finally, you must have this in your .vimrc (Linux) or _vimrc (Windows):

filetype plugin indent on