4

How can I detect whether a color scheme installed via a vim-plug plug-in is available?

For context, I'm trying to automate my Vim setup on a new machine, pulling the vimrc from a git repository. The vimrc has :Plug directives to load all the plugins, but on the first run they're not installed yet. Later on I have a :colorscheme setting, but that fails if the plug-ins are not installed yet.

I can automate installing plug-ins on first run with:

vim -c PlugInstall -c qa

But that trips on the :colorscheme setting:

Error detected while processing ~/.vimrc:
line  123:
E185: Cannot find color scheme 'blablabla'
Press ENTER or type command to continue

After pressing Enter, plug-ins are installed correctly and next time everything works as expected... But it's still inconvenient to have this on installation (which is supposed to be as automatic as possible.)

I found some solutions for the problem, but none of them seems ideal... Using :silent! colorscheme makes it fail silently, but that's not great since other than during setup I would actually want to know when this fails.

I looked at vim-plug documentation to see if there's a function telling whether plug-in X is installed but couldn't find it...

There's also a possibility of loading the colorscheme using :runtime colors/blablabla.vim but that's not really great since later on typing :colo will not report which one is in use.

Vim knows about the color schemes, since when typing :colorscheme Ctrl+D it will autocomplete all those which are available. But I couldn't really find a good way to get that list from VimL.

Is there a good way to programatically tell which color schemes are installed or available?

Would you suggest I take a different approach in this case? (I'm open to alternatives that involve radically different ways to fixing the core problem, which is reliable first-time plug-in installation...)

1
  • @Mass Yes that was very useful! (not the top answer, but:) if index(getcompletion('', 'color'), 'blablabla') >= 0 worked great. Thanks!
    – filbranden
    Commented Feb 15, 2019 at 19:20

1 Answer 1

5

You could search the colorscheme file via the runtime path:

let colorscheme="desert"
if findfile("colors/" .colorscheme .".vim", &rtp) != ""
    execute "colorscheme " . colorscheme
endif

Another way is to work with try-catch. Thanks to D. Ben Knoble for the hint. Here is a example from his vimrc:

try
  colorscheme dracula
catch /^Vim\%((\a\+)\)\=:E185/
  colorscheme default
  set background=dark
endtry

If the colorschema "dracula" is not available, Vim would throw the exception Vim(colorscheme):E185: Cannot find color scheme 'dracula'. This is then catched and a default processing is possible.

1

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