Almost all the proposed solutions try to execute or not execute the code if the plugin is loaded or not. But I think that what you want is to execute some action when the plugin gets loaded. So it is not a matter of not executing the code, but executing it at the right moment.
According to Plug documentation, when the plugin is loaded an User
event with the name of the plugin is triggered, then you can use that event in autocmd commands.
For example, in my case I want to execute the register function of the plugin called vim-wich-key
. My Vimrc is split into several files, but I will put everything togheter in this example:
Plug 'liuchengxu/vim-which-key', { 'on': ['WhichKey', 'WhichKey!'] }
autocmd! User vim-which-key call which_key#register('<Space>', "g:which_key_map")
As you can see, the plugin is loaded on the first call to one of it's functions and my register call only happens when the plugin gets loaded. I verified that this works perfectly.
let g:loaded_myplugin = 1
) and then check if this variable exists (if exists("g:loaded_myplugin")
). This is usually a best practice to use this kind of variable to avoid loading your plugin several times (see:h write-plugin
especially the "NOT LOADING" part)