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.
The global or system-wide vim configuration file is generally located under the /etc/vim/vimrc . This configuration file is applied to all users and when Vim is started this configuration file is read and Vim is configured according to this file contents.
vim which is used to determine the "type" of a file. For example, while editing example.py the command :set ft? should display filetype=python if :filetype indent plugin on has been used. The file type determines whether any plugins for scripts, indenting rules, or syntax highlighting are loaded.
Using file name completion, you could type :e $M then press Tab until you see the desired variable. If you only want to see the path, type :echo $M then press Tab to see the variable, and press Enter. In gvim, the Edit menu includes “Startup Settings” which will use $MYVIMRC to edit your vimrc file.
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 ftplugin
s 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With