My .vimrc chooses my preferred color scheme (solarized) which is installed via plugin management system. Yet on some systems which don't have that plugin the :colorscheme solarized command runs but the result is whacky.

how do I conditionally test if solarized is indeed installed and use it if it is but use a different (built in) color scheme if not?


Lets fail with Style!

silent! colorscheme evening
silent! colorscheme solarized

The :silent! will ignore the errors at start up. Just put color schemes in reverse order of what you really want.

For more help see:

:h :silent

If you want to accomplish this programmatically you will need to check the list of available colorschemes.

To do this in :Ex mode is fairly trivial (type :colorscheme and then tab complete through a list of available colors). While this is a great way to check available colorschemes in general it will not solve your specific problem..

In order to verify that a colorscheme like solarized (not installed by default) is installed you will need to check the file is installed in the ~/.vim/colors/yourscheme.vim

To do this you will need to add the following conditional check to your vimrc (may vary depending on your install):

if !empty(glob("~/.vim/colors/solarized.vim"))
  colorscheme solarized

glob will expand the directory and return a string with the full pathname if the file is present. When no file is present it will return an empty string. Hopefully this approach is enough to take care of those pesky machines that don't have solarized installed!

Edit If you prefer to use filereadable (will simply return true/false) you will need to use expand in order to get the global directory.

  • 2
    A better test: if globpath(&runtimepath, 'colors/solarized.vim', 1) !=# ''.
    – lcd047
    May 26 '15 at 17:15
  • hmm that is a cool way to do it too.. not sure why its a better test though.. If you could elaborate on why that might be a better way to check for the file that would be awesome! (figure its just the fact that &runtimepath will dynamically use the appropriate path no matter what your vim install is..) Either way the you just want to check that the file is present before attempting to load the colorscheme.. May 26 '15 at 17:21
  • 1
    It's better (1) because of runtimepath, (2) because empty() is a list function, not a string one (even though empty(string) works), and (3) because your glob() pays attention to suffixes and wildignore.
    – lcd047
    May 26 '15 at 17:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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