I use quite a number of plugins now and the init.vim script for nvim has become quite unwieldy. So I started separating out the individual configuration files for each individual plugin.
And now I realized that sometimes I want to switch some plugins off for testing, or to try out an alternative, etc, without having to comment out the sourcing of the plugin configuration in my init file or renaming or deleting the configuration file.
So far I use a structure like this
if filereadable(expand("~/.vim/coc.vim")) source ~/.vim/coc.vim endif
I would instead like to use a function that as a string the name of a plugin and returns true or false if the plugin is loaded or not.
I know that I can use
scriptnames to list all sourced files. So my idea was to run over that list and match each line to the provided string.
I am however very inexperienced with vimscript and don't know how to do that.
Here is what I have so far
function! IsPluginLoaded(pluginName) redir =>scripts silent scriptnames redir END for script in split(scripts, "\n") if match(script, "^\d+: ~/\.vim/plugged/" . a:pluginName . "/") >= 0 return v:true endif endfor return v:false endfunction
But it gives me a weird error of
E874: (NFA) Could not pop the stack!. It also doesn't work, as in it always returns false.
How do I accomplish the task of checking whether a plugin is loaded or not and preferably in a readable format that doesn't consist of vimscript shorthands that look worse than regular expressions.
I would use lua, but the script should also work for standard vim.
I have found out that the source of the error is that "~" has a special meaning. I have updated the function to be more readable by putting the regular expression in a separate variable and now the error is gone, however it is still not returning true, even though it should match the regex.
function! IsPluginLoaded(pluginName) redir =>scripts silent scriptnames redir END let re = "^ *\\d+: \\~/\\.vim/plugged/" . a:pluginName . "/" " echo "re: \"" . re . "\"\n" for script in split(scripts, "\n") " echo "script: \"" . script . "\"\n" if match(script, re) >= 0 return v:true endif endfor return v:false endfunction