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.
if exists(*submode#enter_with)
.if exists('*submode#enter_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 yourvimrc
end).try
andcatch
myself; from my part is perfect if you post your solved problem (or relevant lines) as answer :)