Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

In a .vimrc, is it possible to load a color scheme only if it exists?

like image 611
ClosureCowboy Avatar asked Apr 18 '11 03:04

ClosureCowboy


People also ask

Where does vim look for color schemes?

Vim color schemes are stored in vim directory named /usr/share/vim/vim80/colors/ but vim80 can be different according to vim version.

How do I enable color scheme in vim?

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.

How do I change the color scheme in Vimrc?

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.

How do I change my Gvim color scheme permanently?

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> .


1 Answers

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.

like image 199
Luc Hermitte Avatar answered Sep 26 '22 04:09

Luc Hermitte