In a .vimrc
, is it possible to load a color scheme only if it exists?
Vim color schemes are stored in vim directory named /usr/share/vim/vim80/colors/ but vim80 can be different according to vim version.
You can change color schemes at anytime in vi by typing colorscheme followed by a space and the name of the color scheme. For more color schemes, you can browse this library on the vim website. You can enable or disable colors by simply typing "syntax on" or "syntax off" in vi.
vimrc (or . Here, you can set the default color scheme by modifying the colorscheme line and setting the theme you prefer. When manually configuring syntax highlighting, you can add individual instructions with the highlight command.
In Ubuntu make a file with the name . vimrc in your Home directory if it doesn't exist, add colorscheme pablo in it and save it. Now restart GVim. For Windows put this file with the name _gvimrc in C:/Documents and Settings/<your-username> .
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With