I have a function that I would like to run once and do nothing in subsequent calls to it. It handles lazy-loading of plugins.

function! LoadExpensivePlugins()
  call StuffToRunOnceOnly
  " what do i do here?
  • Trying to define a new function inside of it results in E127: Cannot redefine function LoadExpensivePlugins: It is in use – Steven Lu Feb 16 '17 at 19:06
  • 1
    I would simply delete it: :delfunction (this won't work from within the function however (as you noted)) – Christian Brabandt Feb 16 '17 at 19:25
  • couldn't find :delfunction. Thank you.... – Steven Lu Feb 16 '17 at 21:40
  • Do you have a specific reason for trying to postpone the loading of plugins? I would say that the features of a plugin that you do not need all the time should not be loaded until they are needed. Vim provides ways of doing just that with autoload and ftplugin etc. – Octaviour Feb 17 '17 at 16:19
  • YouCompleteMe is the main one that is big and expensive. I use Vim-Plug to manage the plugins. My understanding of the situation is that vim-plug and vim's own auto/on-demand loading do not work properly on this plugin so I'm using this technique, but since it's getting more unwieldy, I'm breaking it out to its own function. – Steven Lu Feb 17 '17 at 19:43

The simplest solution is simply to set a variable:

let s:LoadExpensivePluginsHasBeenRun = 0
function! LoadExpensivePlugins()
   if !(s:LoadExpensivePluginsHasBeenRun)
      call StuffToRunOnceOnly
      let s:LoadExpensivePluginsHasBeenRun = 1
| improve this answer | |
  • 1
    Also would accept :delfunction as an answer, it seems more directly answering my original question. – Steven Lu Feb 16 '17 at 21:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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