Dave Vogt
Dave Vogt

Reputation: 19502

GVim: find out if guifont is available

I'm sharing my vim settings across a number of different machines, which don't neccessarily have exactly the same configuration.

Now if my favourite font is only available on one system but not another, this leads to the problem that gvim uses a fallback which may not be the best choice.

So: Is there a way to do multiple tries of set guifont=... and somehow check whether it was successful? Or is there a way to provide a list of fonts to try?

Upvotes: 9

Views: 3482

Answers (3)

sampi
sampi

Reputation: 872

Following the accepted answer is likely the right method in most cases, but this is a work around. Platform specific to unix like gtk2 systems only. First, create a function checking for the font using a shell command:

function! Font_exists(font)
  exec system("fc-list -q '" . a:font ."'")
  return v:shell_error == 0
endfunction

Then use it with whatever logic is desired. E.g.:

if Font_exists('Iosevka Term')
  set guifont=Iosevka\ Term\ Light\ 9
elseif Font_exists('Inconsolata')
  set guifont=Inconsolata\ 9
elseif Font_exists('Terminus (TTF)')
  set guifont=Terminus\ (TTF)\ 9
endif

Upvotes: 1

romainl
romainl

Reputation: 196556

You can give Vim a list of fonts:

set guifont=Monaco:h24,Inconsolata-gz:10

Vim will try the first then the second…

:h guifont doesn't tell if there's a limit to the number of choices.

Upvotes: 21

Ingo Karkat
Ingo Karkat

Reputation: 172580

Detection / fallbacks may work in this instance, but things get hairy when you also want different font sizes (due to different display resolutions), window sizes, local commands, etc.

A more extensible system than switching on $HOSTNAME or similar schemes is checking for a "local" .[g]vimrc and sourcing that in:

" Put this in ~/.gvimrc:
" Source system-specific .gvimrc first.
if filereadable(expand('~/local/.gvimrc'))
    source ~/local/.gvimrc
endif

This way, all special settings are localized and do not complicate your shared config.

Upvotes: 3

Related Questions