Changing settings / options based on filetype in Vim

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!

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

8 comments on this post.
  1. d.m:

    If you want several files to be sourced for one filetype, you can
    * create folders like ftplugin/perl or ftplugin/html and put your *.vim files in there
    * or alternatively you can create files like ftplugin/perl_indent.vim, ftplugin/perl_outdent.vim, ftplugin/perl_rocks_da_party.vim etc.

  2. Greg:

    I think you should use setlocal commands in the ftplugin-files as well. Otherwise the settings will apply to all (new) buffers even if they are not of that filetype.

    Keep up the good tips – I’m looking forward to your next video :)

  3. Derek Wyatt:

    I believe that you get that for free. Filetype files make buffer local modifications.

  4. Greg:

    Yes, that’s what it’s meant to be. But that is only true if you use the proper ‘buffer only’ commands. See :h ftplugin-special
    I’m not sure if I understand that right but that’s how I interpret that :)

  5. Derek Wyatt:

    Interesting. Now that I actually read the docs (I guess I tend to foolishly “skim” with that air of ‘I know what’s in this doc already’ :)) I see that setlocal all over the place. I did try it out though and the standard “set” played as a buffer local setting. I tried it with a Java filetype and set complete+=i and was only set for that buffer.

    But regardless of that I believe you’re correct and I should use setlocal. Thanks for the enlightenment.

  6. Greg:

    I think the ftplugin settings done with ‘set’ are applied to all *new* opened buffers. I tried the following:
    I did a ‘set spell’ in my /ftplugin/tex.vim and opened a new tex file. All buffers (regardless which filetype) opened after that had activated spellchecking (which is usually deactivated in my setup).

  7. Anand Sharma:

    Here’s a message I posted on one of your VoDs on Vimeo, but since I am not sure if you get any notification from that I am reposting this here:

    Derek:

    A huge fan of your work. Excellent job and keep it coming. However, I do have a question. And yes, I am a new convert to the idea of using Vim as *the* editor for my hacking needs. Don’t ask me what I have been using all this time. You’ll be decidedly unimpressed.

    Anyways, so all jokes aside, how are you getting the rather spiffy looking effect when you tab through the options. I have the xoria256 colorscheme turned on, I also have wildmenu set, but when I say
    :set wild
    i get a rather unimpressive looking new line with all the options as opposed to what you have which is basically overwriting the status line and highlighting the first entry with a yellow background.

    Thx for all your help and your inputs are much appreciated

  8. Derek Wyatt:

    Hey Anand,

    I think you’ve probably got ‘wildmode’ set to ‘list’ and you might want to set it to ‘full’ or something like that. Check the help on ‘wildmode’ for more info on what you can set it to. I have it set to ‘full’ myself, but nor have I really experimented with it either :)

    Cheers!

Leave a comment