math2001's blog

Vim file type specific settings

28 September 2018

If you want to run some vim script for a specific file type, don't make an auto command, they get messy really quickly. Instead, you should use ftplugin.

Just edit ~/.vim/ftplugin/<filetype>.vim.

ftplugin stands for file type plugin

To get the file type of the current file, just do:

:set filetype?

It'll be automatically sourced when you open a file of that specific type.

Edit matching file

Most of the time, you'll find yourself wanting to edit the ftplugin file of the current filetype. So, here's a quick command to do this:

command! -nargs=? Ftedit execute "tabe ~/.vim/ftplugin/" . ("<args>" == "" ? &filetype : "<args>") . ".vim"

If you just type :Ftedit, it'll open the ftplugin of the current file. But it you give it an argument, it'll open the one with the given name.

:set filetype?
" opens ~/.vim/ftplugin/python.vim
:Ftedit vim
" opens ~/.vim/ftplugin/vim.vim

Enjoy 😄