3

My &guifont is set using let &guifont="SauceCodePro NF 11,Source code pro 11".

Calling set guifont? returns the full option, guifont=SauceCodePro NF 11,Source code pro 11.

Is there a way to check which of the two fonts gvim eventually used, if any?

Reading :h guifont didn't give me any ideas.

2

If there really isn't a simple query to find the information then we'll have to brute-force it...

Two key behaviors are described in :h 'guifonts':

[If a list of fonts is given] the first valid font is used.

and

[If a single font is given and] the font cannot be found you will get an error message.

That suggests something like this function:

func! ReportFirstValidGuiFont()
    " preserve existing value
    let l:fonts = &guifont

    for font in split(l:fonts, ",") 
        try
            exe "set guifont=" . font
            echo "Applied GUI font: " . font
            return
        catch /E596/
            " ie: Vim(set):E596: Invalid font(s): guifont=<font-name>
            echo v:exception
        finally
            " always restore original value
            let &guifont = l:fonts
        endtry
    endfor
endfunc

Call with :call ReportFirstValidGuiFont() and it will iterate over the comma separated font list, setting each font as the sole guifonts value. It will print a message naming the first one that doesn't cause an error and exit. The printed name should be the font that is currently applied.

|improve this answer|||||
  • Is the return a bug? – D. Ben Knoble Nov 24 '19 at 14:34
  • @D.BenKnoble How so? All we care about is the first font that doesn't cause an error. So if we get past the set guifont line we print and bail. – B Layer Nov 25 '19 at 4:43
  • Yeah misread my bad – D. Ben Knoble Nov 25 '19 at 12:51
  • I finally got around to trying this, and discovered a few things. First, it took me ages to figure out I needed to remove the finally block. The other thing is that this only works after the GUI has connected, so I had to put it into au GUIEnter * call .... Sadly, that means it can't be used for anything during startup. – BlueDrink9 Feb 1 at 10:29
  • 1
    Sorry, my question didn't quite outline why I wanted to know this. I wanted to check the eventual font while starting up, so I could configure whether or not to use nerd font symbols. I removed the finally to actually set guifont to the first valid font, rather than keeping the fallback list for the whole session. I now see why you put it in there! This answer did answer the question though, so I'll give you the answer tag. I don't think I'll find anything to suit my requirements – BlueDrink9 Feb 1 at 10:48

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.