5

Say I have vim-plug installed

But before calling call plug#begin(), I want to check if this function exists

How to perform this check?

I tried the following:

echom empty(g:plug)
echom empty(plug)
echom empty(plug#begin)

But all of the above led to an "Undefined variable" error, although the function was defined and calling it was successful.

3 Answers 3

5

For a function specifically, you can write

if exists('*funcname')
  …
endif

(that would be exists('*plug#begin') here). More generally, see the documentation for :help exists().

1
  • This seems to work! Thank you Sep 13, 2022 at 12:57
6

This is an autoloaded function, so you can't (because it just did not exist until it has been loaded successfully).

If you really believe you need to check that then do it by try and catch.

3

As this is a function from an autoload plugin, you need first to be sure the plugin has been loaded before checking for the function. This means the typical sequence (if you're not sure the plugin is installed!) would be

runtime autoload/plug.vim  " because the function is plug#something()
if exists('*plug#begin')
   whatever you want to do with it
endif
3
  • what does the runtime keyword do here? Sep 13, 2022 at 12:58
  • :h :runtime makes sure autoload/plug.vim has been loaded before checking whether its functions are known (BTW, I'll fix the path in my answer) (See also stackoverflow.com/a/7322267/15934) Sep 13, 2022 at 13:54
  • In autoload function cases, it makes little sense to check whether a function exists() without making sure the related autoload plugin has been loaded. This is definitively not the expected use case. Sep 13, 2022 at 13:57

Your Answer

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

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