1

Should user vimfiles/after/ftplugin scripts loaded after any system script?

1 Answer 1

3

Let's start with a blank vimrc:

" Hello, world!

Vim doesn't do any filetype-specific stuff by default: no ftplugins, no syntax highlighting, nothing.

We can tell Vim to start doing filetype-specific stuff, namely syntax highlighting, by adding the following line (or syntax enable, but that's a debate for another day):

syntax on

which tells Vim to try sourcing syntax scripts in the following order:

~/.vim/syntax/javascript.vim
$VIMRUNTIME/syntax/javascript.vim
~/.vim/after/syntax/javascript.vim

when you edit a *.js file.

From there, you can enable filetype plugins by adding the following line:

filetype plugin on

which tells Vim to try sourcing filetype plugins in the following order:

~/.vim/ftplugin/javascript.vim
$VIMRUNTIME/ftplugin/javascript.vim
~/.vim/after/ftplugin/javascript.vim

Now, as mentioned by @Matt, in the comments, order matters. If you have this:

syntax on
filetype plugin on

then Vim tries to source the syntax scripts before the filetype plugins:

~/.vim/syntax/javascript.vim
$VIMRUNTIME/syntax/javascript.vim
~/.vim/after/syntax/javascript.vim
~/.vim/ftplugin/javascript.vim
$VIMRUNTIME/ftplugin/javascript.vim
~/.vim/after/ftplugin/javascript.vim

but, if you have this:

filetype plugin on
syntax on

then the filetype plugins are tried before the syntax scripts:

~/.vim/ftplugin/javascript.vim
$VIMRUNTIME/ftplugin/javascript.vim
~/.vim/after/ftplugin/javascript.vim
~/.vim/syntax/javascript.vim
$VIMRUNTIME/syntax/javascript.vim
~/.vim/after/syntax/javascript.vim

which I think is extremely silly.

Now, there are two ways to enable indent scripts. One is to add a line like this:

filetype indent on

and the other is to combine it with an existing filetype plugin on line:

filetype plugin indent on
filetype indent plugin on

Again, order matters. If you have this:

filetype plugin on
filetype indent on

then the filetype plugins are tried before the indent scripts. But, if you have this:

filetype indent on
filetype plugin on

then the indent scripts are tried before the filetype plugins.

Funnily enough, the order of indent and plugin in:

filetype plugin indent on

has no effect on the order in which the scripts are sourced: it is always filetype plugins first and indent scripts second.

In my own config, I have the following:

filetype plugin indent on
syntax on

which tells Vim to source the filetype plugins first, then the indent scripts, and then the syntax scripts. YMMV.

So… the only order that is more or less guaranteed (assuming a relatively normal setup) is the one within a "family" of scripts:

~/.vim/ftplugin/javascript.vim
$VIMRUNTIME/ftplugin/javascript.vim
~/.vim/after/ftplugin/javascript.vim

You can definitely expect any runtime script to be sourced after ~/.vim/after/ftplugin/javascript.vim, so there is no reason to expect that ~/.vim/after/ftplugin/javascript.vim will be sourced after everything else.

If you are looking for a way to do some setup after everything else has been set up, then after/ftplugin/ is not it.

1
  • @Matt, I tried to make my answer more comprehensive.
    – romainl
    Nov 10, 2023 at 10:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.