4

Following the answers in I have tried many varieties of setting the font size and face in ~/.vimrc . None of them have any effect

set guifont=Menlo:h12
set guifont=Monaco:h12
set guifont=Menlo\ Regular:h12

Note that executing

:set gfn=*

does bring up a font chooser dialog - and I am able to choose either Monaco or Menlo or many others correctly. But I don't want to have to choose every time - thus the ~/.vimrc . What might be wrong how to fix this?

5
  • The font names are system-dependent. Make sure you write it in a right way. First, type :set gfn=* to choose one. Then see an exact name with :set gfn?
    – Matt
    Dec 22, 2019 at 5:42
  • Thx Matt Monaco and Menlo do show up in the dialog - as alluded in the actual question above. Dec 22, 2019 at 5:49
  • I mean what :set gfn? exactly shows after you've selected one of them? Could be, for example, "Menlo 12" instead of "Menlo:h12" etc.
    – Matt
    Dec 22, 2019 at 5:52
  • guifont=Menlo Regular:h14 is what shows up without actually manually setting one of the fonts. If I select one of the fonts from the chooser that does set it properly. But the question above is about avoiding that for each file Dec 22, 2019 at 6:02
  • set guifont=Menlo\ Regular:h12 works fine for me in my .vimrc. Does it work for you if you run it manually from the command-line after start up? I presume other settings in your vimrc are taking effect? What's the output of :verbose set guifont? after start up?
    – Rich
    Jan 21, 2020 at 16:45

6 Answers 6

4

Since font is specific to the GUI versions of vim (i.e. gvim and macvim), you need to should put the set guifont=... line in your gvimrc.

A good place for that is ~/.vim/gvimrc, but you can read help gvimrc for locations.

According to setting-guifont:

                                                        *setting-guifont*
When you use the same vimrc file on various systems, you can use something
like this to set options specifically for each type of GUI:

if has("gui_running")
  if has("gui_gtk2")
    :set guifont=Luxi\ Mono\ 12
  elseif has("x11")
    " Also for GTK 1
    :set guifont=*-lucidatypewriter-medium-r-normal-*-*-180-*-*-m-*-*
  elseif has("gui_win32")
    :set guifont=Luxi_Mono:h12:cANSI
  elseif has("gui_macvim")
    :set guifont=DejaVu\ Sans\ Mono:h13
  endif
endif

So this would indicate guifont should be set only when has('gui_running') is true—i.e., when the gui is started or about to start.

I can't find anything that requires this in help macvim, help gui, or help gui-init.

3
  • 1
    Sounds plausible... Unfortunately I didn't find anything in the docs to confirm this is actually the case... Do you happen to have any corroborating sources? Or some other indication that this (or similar GUI settings) won't work when set from vimrc, but will from gvimrc?
    – filbranden
    Dec 22, 2019 at 8:08
  • 2
    AFAIK, you can't directly use set gfn=... and such in vimrc only if GUI load is delayed, like in Neovim, for example.
    – Matt
    Dec 22, 2019 at 8:12
  • 1
    @filbranden I can't find anything that requires it, but it is definitely preferred. It works for me like that, at least.
    – D. Ben Knoble
    Dec 22, 2019 at 15:57
5

I had the same problem as the OP after upgrading from Ubuntu 16.04 to 18.04. Turns out the condition if has("gui_gtk2") in _vimc is no longer true, changed it to "gui_gtk3" as a wild guess and bingo! my fonts settings are working again.

5

Here is a trick what I usually do in a graphical Vim to select a nice looking font. First I interactively set the font using:

:set guifont=*

This brings up a nice dialog with all the known fonts known by gvim. So just select a nice looking font to be used and press Okay to get out of the dialog.

Now, you should have set this as your font setup by gvim and you can try it out a bit to see how satisfied you are with the font.

Once you are satisfied one needs to store that particular font in either the vimrc or gvimrc init file.

So to make this setting permanent, I edit the initialization file:

:e $MYVIMRC

and then I put the guifont setting there by typing this:

ilet &guifont="CTRLR=&guifontEnter"

This makes use of the expression register (see :h i_CTRL-R_=) to recall the guifont option setting and place it into the current position in your document.

So this will basically add the guifont setting exactly the way it is currently used by gvim and should make sure to set it correctly next time you start gvim.

Now optionally, you can add all the fancy autocommand and if statements you have seen elsewhere here around that guifont option setting and if you use different gvim builds on different systems, you need to repeat this step to get the correct value.

Here is an example of mine that I use on various systems:

if has("autocmd")
  function! SetGuiFont()
    if has("gui_gtk") && !(has("win32") || has("win64"))
      let &guifont="Ubuntu Mono derivative Powerline 12"
    elseif has("win32") || has("win64")
      try
        let &guifont="DejaVu_Sans_Mono_for_Powerline:h12:cANSI:qDRAFT"
      catch
        " font does not exists, fall back to default
        let &guifont="Courier_New:h10:cANSI"
      endtry
    " optionally: add statements for other guis
    endif
  endfunction

  if !exists('#VimStartup#GuiEnter')
    augroup VimStartup
      au!
      autocmd GuiEnter * :call SetGuiFont()
    augroup end
  endif
endif

You can make this of course a lot more complex, depending on your needs.

3

Create/modify a file called.gvimrc in your home directory and add set guifont=DejaVu\ Sans\ Mono\ Book\ 12. Restart gvim to take effect.

2

You may find useful this plugin I made to simplify setting guifont in a portable way: https://github.com/awvalenti/vim-simple-guifont. Your vimrc goes like this and it handles all the OS specific stuff:

silent! call simple_guifont#Set(
  ['Cascadia Code PL', 'JetBrains Mono', 'Hack'], 'Consolas', 14)
2

The reason it didn't worked for me is because I was only using "Family" to set guifont. Adding "Style" made it worked. This is for CentOS 7

NOT working examples:

set guifont=Monospace regular:h10 

set guifont=clean:h10

Working examples:

set guifont=DejaVu\ Sans\ Mono\ Book\ 10

set guifont=Clean\ Regular\ 10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.