2

What is the plugin system ?

I could not find answers how the plugins are actually loaded/executed in Vim. I know that Vim looks for them in specific locations. I also found this question about what an plugin is and this one about the load sequence of plugins.

So if someone could point me towards some reference on how it works under the hood or what the relevant code is that I can read, it would be highly appreciated!

An overview would be good as well.

Rich
  • 31,891
  • 3
  • 72
  • 139
noobman
  • 23
  • 3

1 Answers1

2

About plugin a good introduction is Learn Vimscript the Hard Way.

In particular the chapter 42: Plugin Layout in the Dark Ages will give you the role of the different folders and files.

Here is my understanding:

The first script to be loaded is .vimrcif it exists:

  1. .vimrc

When Vim starts it executes the following scripts (from: vimfiles, your installed plugins, Vim distribution plugins):

  1. plugin/*.vim
  2. ftdetect/*.vim

Then in executes the following scripts (from: vimfiles, your installed plugins, Vim distribution plugins):

  1. after/plugin/*.vim

When Vim loads a file of type: mytype it executes the following scripts (from: vimfiles, your installed plugins, Vim distribution plugins):

  1. ftplugin/mytype.vim
  2. after/ftplugin/mytype.vim
  3. indent/mytype.vim
  4. after/indent/mytype.vim
  5. syntax/mytype.vim
  6. after/syntax/mytype.vim

When Vim calls a function myscript#myfunction it executes the following scripts searching for the myscript#myfunction (from: vimfiles, your installed plugins, Vim distribution plugins):

  1. autoload/myscript.vim

There are two other folders with special meaning.

The script colors/mycolor.vim is executed when the command :colorscheme mycolor is executed.

When the command :compiler mycompiler is executed Vim executes the following scripts (from: Vim standard, your installed plugins, Vim distribution plugins):

  1. compiler/mycompiler.vim
  2. after/compiler/mycompiler.vim

Remark: You can learn more about in which order the scripts are executed using the :scriptname command.

Vivian De Smedt
  • 16,336
  • 3
  • 18
  • 37
  • Thanks. But I think it is more about what vim is looking for and less about vim loads and runs it. I am interested in the workings of vim once it finds such plugins – noobman May 06 '22 at 14:02
  • The chapter 14 title is somehow misleading. If you read it you will find information about in which orders the scripts are loaded (run) and it gave to me a good understanding about vim handle the plugin (sorry for pushing you to give it another try :-|) – Vivian De Smedt May 06 '22 at 14:17
  • Ah yes. I gave it a proper read this time. It does contain information along the lines of how things are and what the entire plugin model might look like. Certainly a start. I will wait if something more specialized is referred else I will accept this answer.

    Thanks!

    – noobman May 06 '22 at 14:29
  • The :scriptname command may also help you in your understanding. It list scripts that have been executed in the order they have been executed. – Vivian De Smedt May 06 '22 at 15:02
  • I am more inclined towards knowing what is going on internally when it detects a plugin, say myscript for example,after it is done figuring out which file is where. What does the call hierarchy look like. How is the plugin code parsed. I am familiar with what the folders do and what the plugin location look queue is. To put it simply, what happens when it parses "myscript#myfunction" ? This specific information is not covered in the book. The book says in what order vim looks for files (and a bit of info on what the script is supposed to look like), not what it does in the code itself. – noobman May 06 '22 at 19:00
  • I believe what the code does is fully dependent of the code. What I mean is that the reason there are multiple folders is merely conventional. Moving code from plugin to ftdetect do not change the behavior but by convention in ftdetect you only set the file type of the buffer with autocommand. In particular vim does nothing that is not coded in the plugin. In the hope it help :-/ – Vivian De Smedt May 06 '22 at 19:27
  • 1
    @noobman it seems to me you are more interested in the implementation details (i.e., C code) that parses and executes vimscript? There are a couple of special vimscript files that actually implement the filetype mechanism, among other things (see the files in :Explore $VIMRUNTIME, for example filetype.vim and ftplugin.vim). For the C code, perhaps you could edit the question to clarify that's the kind of implementation you are interested in? – D. Ben Knoble May 06 '22 at 20:11
  • 2
    @noobman I think it's important to realize there is nothing "special" about plugins: everything you can do in a plugin you can do in your vimrc file. From Vim's perspective, there is essentially no difference between "plugin code" and "normal VimScript code". Using :source ~/.vim/myplugin/myplugin.vim has the same effect as "using a plugin". Vim's "plugin system" is little more than a set of filepaths to make organizing and distributing bits of VimScript easier. – Martin Tournoij May 07 '22 at 01:32