197

I am using gVim 7.2 on Windows 7. I can set the gui font as Consolas 10 (font size) from the menu. I am trying to set this in .vimrc file like below:

set guifont=Consolas\ 10

But it doesn't work. Does anyone know how to set this?

1
  • Vim 8.2 on windows 10 is better of with this settings put in ~/_gvimrc, otherwise, plugins stop working if file ~/_vimrc is edited instead. For, example, syntax highlighting stopped working.
    – yozniak
    Commented Feb 23, 2022 at 1:25

10 Answers 10

336

I use the following (Uses Consolas size 11 on Windows, Menlo Regular size 14 on Mac OS X and Inconsolata size 12 everywhere else):

if has("gui_running")
  if has("gui_gtk2")
    set guifont=Inconsolata\ 12
  elseif has("gui_macvim")
    set guifont=Menlo\ Regular:h14
  elseif has("gui_win32")
    set guifont=Consolas:h11:cANSI
  endif
endif

Edit: And while you're at it, you could take a look at Coding Horror's Programming Fonts blog post.

Edit²: Added MacVim.

7
  • 18
    So basically for Windows all you need is to add set guifont=Consolas:h11:cANSI to the ~/.vimrc file Commented Aug 14, 2013 at 15:21
  • 10
    It's even better to do this from .gvimrc. Commented Aug 21, 2013 at 6:52
  • 1
    Here's an updated Codding Horror Programming Fonts Blog Post
    – fratrik
    Commented Jan 27, 2016 at 22:00
  • 2
    With Vim 8 using GTK 3, you also have to check for "gui_gtk3".
    – Ruud
    Commented Oct 1, 2016 at 8:39
  • 4
    Could you add an explanation as to why cANSI used and if it's even needed?
    – anishpatel
    Commented May 3, 2017 at 4:48
93

Try setting your font from the menu and then typing

:set guifont?

This should display to you the string that Vim has set this option to. You'll need to escape any spaces.

46

I am trying to set this in .vimrc file like below

For GUI specific settings use the .gvimrc instead of .vimrc, which on Windows is either $HOME\_gvimrc or $VIM\_gvimrc.

Check the :help .gvimrc for details. In essence, on start-up VIM reads the .vimrc. After that, if GUI is activated, it also reads the .gvimrc. IOW, all VIM general settings should be kept in .vimrc, all GUI specific things in .gvimrc. (But if you do no use console VIM then you can simply forget about the .vimrc.)

set guifont=Consolas\ 10

The syntax is wrong. After :set guifont=* you can always check the proper syntax for the font using :set guifont?. VIM Windows syntax is :set guifont=Consolas:h10. I do not see precise specification for that, though it is mentioned in the :help win32-faq.

46
  1. Start a graphical vim session.
  2. Do :e $MYGVIMRC Enter
  3. Use the graphical font selection dialog to select a font.
  4. Type :set guifont= Tab Enter.
  5. Type G o to start a new line at the end of the file.
  6. Type Ctrl+R followed by :.

The command in step 6 will insert the contents of the : special register which contains the last ex-mode command used. Here that will be the command from step 4, which has the properly formatted font name thanks to the tab completion of the value previously set using the GUI dialog.

6
  • I guess that's because using the graphical font selection dialog results in running a command, like most (all?) similar GUI and menu items in gvim. Sometimes you can see their echoes. Commented Oct 1, 2013 at 7:44
  • 1
    I'm sure that the menu does run a command, but that's irrelevant here; the only effect of the graphical interface being used here is that it sets the guifont option, not that it runs any particular command.
    – qqx
    Commented Oct 1, 2013 at 11:53
  • I had to do ":p for step 6 to get the last ex-mode command. See this answer.
    – erik
    Commented Jun 24, 2015 at 23:16
  • @erik That's a normal mode command, and shouldn't have worked there since step 5 would place you in insert mode. It also doesn't ensure that the pasted command is on a separate line.
    – qqx
    Commented Jun 25, 2015 at 12:19
  • @qqx: Ok, my fault. I exited somehow the insert mode. You are right. Excellent solution!
    – erik
    Commented Jun 25, 2015 at 12:27
12

For Windows do the following:

  1. Note down the font name and font size from the "Edit-Select Font..." menu of "gvim.exec".
  2. Then do :e $MYGVIMRC
  3. Search for "guifont" string and change it to set guifont=<font name as noted>:h<font size>
  4. Save the file and quit.
  5. Next time when you execute gvim.exec, you will see the effect.
10

Although this is an old thread I thought that I would add a comment as I have come across it whilst trying to solve a similar issue; this might help anyone else who may find themselves here:

The backslash character is used to ignore the next character; once added to the font name in my gvimrc it worked; I am on a GNU/Linux machine which does not like spaces. I suspect that the initial post was an error due to the back slash being used on a windows machine.

In example:

:set guifont?  ## From gvim command, would give the following:

set guifont=DejaVu Sans Mono for Powerline 11

Where as I needed to add this line to the gvimrc file for it to be read:

set guifont=DejaVu\ Sans\ Mono\ for\ Powerline\ 11
2

When I try:

set guifont=Consolas:h16

I get: Warning: Font "Consolas" reports bad fixed pitch metrics

and the following is work, and don't show the waring.

autocmd vimenter * GuiFont! Consolas:h16

by the way, if you want to use the mouse wheel to control the font-size, then you can add:

function! AdjustFontSize(amount)
    let s:font_size = s:font_size + a:amount
    :execute "GuiFont! Consolas:h" . s:font_size
endfunction

noremap <C-ScrollWheelUp> :call AdjustFontSize(1)<CR>
noremap <C-ScrollWheelDown> :call AdjustFontSize(-1)<CR>

and if you want to pick the font, you can set

set guifont=*

will bring up a font requester, where you can pick the font you want.

enter image description here

1

I had to end up doing

:set guifont=Courier:h10:cANSI
0

Ubuntu 14.04 LTS

:/$ cd etc/vim/
:/etc/vim$ sudo gvim gvimrc

After if - endif block, type

set guifont=Neep\ 10

save the file (:wq!). Here "Neep" (your choice) is the font style and "10" is respect size of the font. Then build the font - cache again.

:/etc/vim$ fc-cache -f -v

Your desired font will set to gvim.

0

Windows 11 Users

Find this set guifont= XXX:xx:xx as showen in above examples

go to C:\Program Files (x86)\Vim and edit _vimrc file using ADMIN right set that guifont code in that file

ex

" Vim with all enhancements
source $VIMRUNTIME/vimrc_example.vim

" Use the internal diff if available.
" Otherwise use the special 'diffexpr' for Windows.


set guifont=Cascadia_Mono:h14:cANSI:qDRAFT


if &diffopt !~# 'internal'
  set diffexpr=MyDiff()
endif
function MyDiff()
  let opt = '-a --binary '
  if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif
  if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif
  let arg1 = v:fname_in
  if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif
  let arg1 = substitute(arg1, '!', '\!', 'g')
  let arg2 = v:fname_new
  if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif
  let arg2 = substitute(arg2, '!', '\!', 'g')
  let arg3 = v:fname_out
  if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif
  let arg3 = substitute(arg3, '!', '\!', 'g')
  if $VIMRUNTIME =~ ' '
    if &sh =~ '\<cmd'
      if empty(&shellxquote)
        let l:shxq_sav = ''
        set shellxquote&
      endif
      let cmd = '"' . $VIMRUNTIME . '\diff"'
    else
      let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"'
    endif
  else
    let cmd = $VIMRUNTIME . '\diff'
  endif
  let cmd = substitute(cmd, '!', '\!', 'g')
  silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3
  if exists('l:shxq_sav')
    let &shellxquote=l:shxq_sav
  endif
endfunction

save and restart

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