3

I'm confused about the order in which Vim loads plugin files, and seem to be finding mixed answers online. Some answers seem to suggest that vimrc is loaded before plugins, while others suggest that they are loaded during the sourcing of vimrc, at the line filetype plugin indent on. Can someone please clarify the order in which vimrc, plugins, and plugins in the after/ directory are loaded, what causes each to load, and when each could be reloaded during a vim session (e.g. what happens when sourcing vimrc again, what happens when setting the filetype, etc.)?

Kvass
  • 8,294
  • 12
  • 65
  • 108

3 Answers3

10

Some answers seem to suggest that vimrc is loaded before plugins, while others suggest that they are loaded during the sourcing of vimrc, at the line filetype plugin indent on.

All plugins are sourced (the correct term) after your vimrc unless you source them manually. The filetype plugin indent on line doesn't change anything to that order.

Can someone please clarify the order in which vimrc, plugins, and plugins in the after/ directory are loaded,

Assuming you have filetype plugin indent on in your vimrc:

  1. The system vimrc if there is one.
  2. Your vimrc.
  3. Built-in plugins.
  4. Your plugins.
  5. Built-in filetype-specific plugins.
  6. Stuff in the after/ directory.

The whole thing is explained in :help startup and can be seen very clearly with :scriptnames.

what causes each to load,

The value of &runtimepath in general and the :filetype command for filetype-specific stuff.

and when each could be reloaded during a vim session (e.g. what happens when sourcing vimrc again, what happens when setting the filetype, etc.)?

  • :source $MYVIMRC re-executes each command in your vimrc.
  • Most plugins are written in a way that prevent them from being sourced twice. Read their documentation/code if you want to reset them.
  • :help :filetype.
romainl
  • 186,200
  • 21
  • 280
  • 313
  • The help describes `filetype plugin on` and `filetype indent on` as loading files. What is the distinction between loading them and sourcing them? – Kvass Mar 16 '17 at 09:56
  • 1
    You just spotted a discrepancy in the doc. Sadly that's not the only one. – romainl Mar 16 '17 at 10:21
4

.vimrc is executed before loading plugins:

At startup, Vim checks environment variables and files and sets values accordingly. Vim proceeds in this order:

(...)

  1. Execute Ex commands, from environment variables and/or files An environment variable is read as one Ex command line, where multiple commands must be separated with '|' or "". vimrc exrc A file that contains initialization commands is called a "vimrc" file. Each line in a vimrc file is executed as an Ex command line.

(...)

  1. Load the plugin scripts.
  • Can you explain then what the effect of that filetype line is? The answer I linked in my question says that it loads ftplugin and indent files. Is that wrong? Please elaborate on this answer to more fully answer the question before I accept. – Kvass Mar 16 '17 at 07:18
  • 1
    Your question is broad in that case. Why not reading `:help filetype` for more about the command. – Meninx - メネンックス Mar 16 '17 at 08:05
  • I did read the help, as well as other stack overflow answers and blog posts on the topic. I asked because after that I'm still confused :/ If my question seems too broad maybe I can clarify where my confusion is? – Kvass Mar 16 '17 at 08:11
0

Just use :scriptnames to see all the sources files and their order in which they are loaded during startup.

gregory
  • 10,969
  • 2
  • 30
  • 42