48

I'm going through my vim dotfiles to tidy them up. I've noticed that through time I've added various filetype specific settings in various inconsistent ways. Let's suppose I'm customizing for Python:

  • au BufRead,BufNewFile *.py (do something). I don't like this because some Python files might not have the .py termination.

  • au FileType python (do something). This seems a better option because it doesn't depend on the file having the .py termination. The drawback is that Vim doesn't know about some filetypes. I can make Vim recognize additional filetypes, but I also have various inconsistent ways of doing it: a .vim/filetype.vim file, another in .vim/after/filetype.vim and various set filetype commands in .vimrc.

  • Add a .vim/ftplugin/python.vim file with filetype specific settings. I understand the $VIMRUNTIME/ftplugin/python.vim can override whatever settings I make here. One problem is that I'm not sure how this interacts with .vim/filetype.vim and .vim/after/filetype.vim.

  • Add a .vim/after/ftplugin/python.vim. I understand that this is loaded after $VIMRUNTIME/ftplugin/python.vim so it can overwrite settings from there. As in the previous method I'm not sure how it interacts with the filetype.vim files.

So I have at least four ways of doing this, not mentioning syntax files and filetype-specific plugins. It seems to me the best way to do this is to put my filetype specific settings in after/ftplugin so they don't get overwritten, and filetypes.vim in after for the same reason.

However, before I proceed I'd like to ask if anyone has suggestions about the best way to deal with filetype specific settings.

Koraktor
  • 41,357
  • 10
  • 69
  • 99
dimatura
  • 1,230
  • 1
  • 11
  • 14

1 Answers1

70

I store my Python-specific settings is in $HOME/.vim/ftplugin/python.vim, since I do nothing to conflict with $VIMRUNTIME/ftplugin/python.vim.

Keeping these settings in an ftplugin file keeps my .vimrc nice and generic and easier to maintain (it's still pretty big after over ten years of Vim usage).

If you want to overrule what the ftplugins with your Vim distribution set up, then $HOME/.vim/after/ftplugin/python.vim is what you want, as it is read after these.

Similarly $HOME/.vim/filetype.vim will be sourced before $VIMRUNTIME/filetype.vim and that in turn will be sourced before $HOME/.vim/after/filetype.vim.

Invoking :scriptnames will list all sourced script names, in the order they were first sourced.

:help filetype provides pretty comprehensive information on all of this.

johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • 21
    Just want to add a note here that not all your settings must be in a single file for a filetype, they can be in a directory named after the filetype. I have my python settings (and plugins specific to python) in .vim/ftplugin/python/{pydoc.vim, python.vim, etc..} – Randy Morris May 23 '10 at 01:55
  • 2
    +1: Great point if you have a lot of different settings for a given filetype this is a really neat way to organise them. – johnsyweb May 23 '10 at 02:11
  • If you keep your dotfiles under source control, this makes it easier to keep keep all of your dotfiles up-to-date, too. – johnsyweb May 23 '10 at 02:12
  • ooh, `:scriptnames` is nice. In fact it gives me a pretty good idea of what the hell vim is doing with all my setting files. @Randy, that's a good suggestion, I have way too much stuff for some filetypes. Oh and of course I've got everything under git :) – dimatura May 23 '10 at 03:05
  • So what I'm doing seems reasonable. `scriptnames` is a pretty good way to debug this if I run into trouble. – dimatura May 23 '10 at 03:07
  • If you like `scriptnames`, you will *love* `:verbose`. Try something like `:verbose set makeprg`. – johnsyweb May 23 '10 at 03:09
  • 1
    Huh, that's cool. Better not crank it up to 11 because `verbose = 10` is way too much information. – dimatura May 23 '10 at 03:16
  • 4
    Randy mentioned you can use .vim/ftplugin/python/*, but you can also make your scripts start with the filetype: .vim/ftplugin/python_*.vim Good for splitting out your python settings from python-vim code. – idbrii Jul 23 '10 at 05:37
  • 1
    `:help filename` returns help on the `:filename` command for me. Did you mean `:help filetype`? – Rich Feb 17 '11 at 16:55