Changing settings / options based on filetype in Vim

Posted by Derek Wyatt on November 1, 2009
How to change the operational feel of Vim based on the type of file you're editing.

If you want to change settings / options based on the filetype there are a couple of ways you can do it.

Using an Autocommand

You can do this with an autocmd in your vimrc. Let’s assume you want to change some of the indenting rules for perl and html.

augroup indent_settings
    au BufEnter *.pl setl autoindent smartindent
    au BufEnter *.html setl noautoindent nosmartindent
augroup END

The above will do a setlocal when entering a perl or html file. It will turn autoindent to ‘on’ and smartindent to ‘on’ when you enter a buffer containing a perl file, and will turn them off when entering a buffer containing an html file.

Dropping the commands in a filetype file

You can also choose to organize things into separate ftplugin files in your runtime directory. If we want to continue with the perl and html examples above, you would do the following: In ftplugin/perl.vim:

setl autoindent
setl smartindent

In ftplugin/html.vim:

setl noautoindent
setl nosmartindent