开发者

In my .vimrc, how can I check for the existence of a color scheme?

In a .vimrc, is it possib开发者_JS百科le to load a color scheme only if it exists?


Using :colorscheme in a try-catch as Randy has done may be enough if you just want to load it if it exists and do something else otherwise. If you are not interested in the else part, a simple :silent! colorscheme is enough.

Otherwise, globpath() is the way to go. You may, then, check each path returned with filereadable() if you really wish to.

" {rtp}/autoload/has.vim
function! has#colorscheme(name) abort
    let pat = 'colors/'.a:name.'.vim'
    return !empty(globpath(&rtp, pat))
endfunction

" .vimrc
if has#colorscheme('desert')
     ...

EDIT: filereadable($HOME.'/.vim/colors/'.name.'.vim') may seem simple and it's definitively attractive, but this is not enough if the colorscheme we're looking for is elsewhere. Typically if it has been installed in another directory thanks to a plugin manager. In that case the only reliable way is to check in the vim 'runtimepath' (a.k.a. 'rtp'). Hence globpath(). Note that :colorscheme name command searches in {rtp}/colors/{name}.vim.


An alternative to @eckes answer would be to try to load the colorscheme and deal with the error if it doesn't exist:

try
    colorscheme mayormaynotexist
catch /^Vim\%((\a\+)\)\=:E185/
    " deal with it
endtry


You could use the filereadable function to check if a color scheme (e.g. schemename) exists: check once under ~/vimfiles/colors (Win32, for Unix use ~/.vim/colors/) and once under $VIMRUNTIME/colors/:

if filereadable("/path/to/schemename.vim")
  colo schemename
endif


My method is similar,

if filereadable( expand("$HOME/.vim/colors/railscast.vim") )
    colorscheme railscast
endif

This is a little more robust than hardcoding the entire path.


Normally I use a favorite colorscheme with a fallback if my favorite is not available. A nested try will make this work:

try 
  colorscheme solarized
  catch
  try 
    colorscheme peachpuff
    catch
  endtry
endtry

If neither colorscheme is available, the default one is loaded (whatever that happens to be on your system). No errors are shown if one or both of the colorschemes are not available. Put your preferred colorscheme first.

Also, catch with no arguments catches any error. This is handy if you are dealing with different locales that give different error messages.


This is wat I have in my .vimrc file.

if filereadable( expand("$HOME/.vim/colors/sublimemonokai.vim") )
    colorscheme sublimemonokai "https://github.com/ErichDonGubler/vim-sublime-monokai

    " vim-sublime-monokai only support 256 colours in terminal. If you are using a terminal which support truecolor like iterm2, enable the GUI color
    set termguicolors

    " Otherwise, use below setting to activate the 256 color in terminal
    set t_Co=256
else
    echom "The sublimemonokai.vim were not found to be used as colorscheme. elflord will be set for the timebeing..."
    colorscheme elflord
endif

basically it does check to see if the color scheme I like exists on the machine or not. If it does, it will select it and apply all the setting necessary for it. Otherwise it choose a suitable color scheme that is shipped with vim.

By looking at other answers, my answer shares bit part with @user427390 answer and it has an additional else condition.

The following link has helped me a lot in scripting my own .vimrc and vim related files: http://learnvimscriptthehardway.stevelosh.com/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜