9

My use case is the following, but I think a general solution, working for as many plugins as possible would be preferable:

I use the submodes plugin to create a submode handling windows. In my .vimrc I have several lines containing calls to functions defined by this plugin (like that:)

" The submode is entered whith <Leader>k and exited with <Leader>
call submode#enter_with('WindowsMode', 'n', '', '<Leader>k', ':echo "windows mode"<C
call submode#leave_with('WindowsMode', 'n', '', '<Leader>')
" Change of windows with hjkl
call submode#map('WindowsMode', 'n', '', 'j', '<C-w>j')

When I am on a machine where I use my .vimrc but I haven't installed the plugin yet, I get errors on Vim starting: E117: Unknown function :submode#enter_with.

This is normal that this happen since the plugin isn't installed, Vim can't know the function. But the message on my shell necessitating a key press to disappear is pretty annoying.


To avoid that I've tried several things

  • Some (good) plugins declare a variable g:loaded_pluginname which is accessible at the time of the .vimrc sourcing and can be used to test if the plugin exists. Unfortunately submode (and other plugins) doesn't provide such a variable (nor any variable available at .vimrc sourcing time).
  • Of course, I could use :silent! in front of all the lines causing the message to mute them. The problem is that it doesn't feels really clean to proceed this way: I'd like keeping the potential error messages when the plugin is installed.
  • The last solution I've found is to check if the file defining the plugin exists and put the call to the function in this if:

    if (filereadable($HOME . "/.vim/plugged/vim-submode/autoload/submode.vim"))   
    

    The fact is that it doesn't seem efficient nor reliable to me.

Thus I'm looking for the most generic way to programmatically test from my .vimrc if a plugin is already installed.

11
  • 2
    Maybe you could test the existence of the functions before calling them. For example something like: if exists(*submode#enter_with). May 1, 2016 at 12:19
  • Sorry I forgot the single quotes: if exists('*submode#enter_with') May 1, 2016 at 12:25
  • 1
    Maybe that's because your plugin uses autoloading: learnvimscriptthehardway.stevelosh.com/chapters/53.html If that's the case, the function is only defined when you call it for the first time, and my answer was not really useful for your particular case, sorry. May 1, 2016 at 12:44
  • 2
    I think checking if plugin's path exists is your best bet. This seems to be the case also with vim-plug plugin manager - see for example github.com/junegunn/vim-plug/blob/master/plug.vim#L2003. (BTW using it could simplify things for you, I think you just have to add a 'PlugInstall' or similar command to your vimrc end).
    – VanLaser
    May 1, 2016 at 15:06
  • 1
    @statox - cool then! I don't use try and catch myself; from my part is perfect if you post your solved problem (or relevant lines) as answer :)
    – VanLaser
    May 2, 2016 at 12:16

2 Answers 2

5

Normally exists('*submode#enter_with') can be used, however it was returning false in my case.

So @VanLaser suggested a pretty simple solution using the try and catch vimscript instruction to execute the lines which echoed errors when the plugin wasn't installed.

For now I use this solution, it doesn't allow to determine if a plugin is installed or not but at least my instructions are run when it's possible and I got rid of the error messages when the instructions can't be executed:

try
    " Create a submode to handle windows
    " The submode is entered whith <Leader>k and exited with <Leader>
    call submode#enter_with('WindowsMode', 'n', '', '<Leader>k', ':echo "windows mode"<CR>')
    call submode#leave_with('WindowsMode', 'n', '', '<Leader>')
    " Change of windows with hjkl
    call submode#map('WindowsMode', 'n', '', 'j', '<C-w>j')
catch
    "echo "The functions can't be executed"
endtry

I think I could even put the following in the catch block to automatically try to install the missing plugin.

catch
    echo "vim-submode is not installed, trying to install it"
    PlugInstall vim-submode
endtry
1

For autoload plugins i proceed in two steps.

First i test whether the function exists. If not i execute :runtime autoload/path/pluginname.vim and i test again whether the function exists.

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.