Is there a way to change the size of the font in gVim, using native commands?

And if not, what script can be used to do it quickly?

up vote 23 down vote accepted

Set the guifont option in your .gvimrc. See :help guifont and :help setting-guifont.

Specifically, you can do something like: set guifont=Consolas:h11 to use 11-point Consolas in gvim on Windows. There are a few other options you can use (each separated with a :), such as :b for bold, although some of these are only available on certain platforms.

The :h option to set the font size as demonstrated is probably the most useful, and the one specifically related to changing the size of the font, as you requested.

  • 6
    Note that the name:size syntax only works on Windows gVim, for gtk gVim you need to use name size (a space between the font name & size) ... Other platforms may have other formats... – Martin Tournoij Apr 27 '15 at 22:14
  • :hXX works on the Mac as well. Good point about GTK though. – Josh Apr 27 '15 at 22:31
  • set guifont=? Not set gfn=? – Peter Mortensen Mar 1 at 23:13
  • 1
    @PeterMortensen Both work; I prefer the longer forms because I find them more readable, and especially tend to use the longer ones in answers. – Josh Mar 2 at 3:52

If you want to simply change the font size in a running GVim instance, type:

:set guifont=*

A window should pop up letting you set font size as well as pick a new font if desired.

I have the following in my .vimrc to change font size quickly without changing the font. Works on Windows and GTK. I haven't tested other GUIs. I'm sure I originally copied it from somebody else, but it's been in my rc file so long I don't remember where it came from.

if has("unix")
    function! FontSizePlus ()
      let l:gf_size_whole = matchstr(&guifont, '\( \)\@<=\d\+$')
      let l:gf_size_whole = l:gf_size_whole + 1
      let l:new_font_size = ' '.l:gf_size_whole
      let &guifont = substitute(&guifont, ' \d\+$', l:new_font_size, '')
    endfunction

    function! FontSizeMinus ()
      let l:gf_size_whole = matchstr(&guifont, '\( \)\@<=\d\+$')
      let l:gf_size_whole = l:gf_size_whole - 1
      let l:new_font_size = ' '.l:gf_size_whole
      let &guifont = substitute(&guifont, ' \d\+$', l:new_font_size, '')
    endfunction
else
    function! FontSizePlus ()
      let l:gf_size_whole = matchstr(&guifont, '\(:h\)\@<=\d\+$')
      let l:gf_size_whole = l:gf_size_whole + 1
      let l:new_font_size = ':h'.l:gf_size_whole
      let &guifont = substitute(&guifont, ':h\d\+$', l:new_font_size, '')
    endfunction

    function! FontSizeMinus ()
      let l:gf_size_whole = matchstr(&guifont, '\(:h\)\@<=\d\+$')
      let l:gf_size_whole = l:gf_size_whole - 1
      let l:new_font_size = ':h'.l:gf_size_whole
      let &guifont = substitute(&guifont, ':h\d\+$', l:new_font_size, '')
    endfunction
endif


if has("gui_running")
    nmap <S-F12> :call FontSizeMinus()<CR>
    nmap <F12> :call FontSizePlus()<CR>
endif
  • That's useful, thank you for sharing! – glS Apr 28 '15 at 17:30
  • 4
    According to this answer, has('unix') is often true on OSX systems, and according to John here, OSX requires the same format as Windows... So using has('gui_gtk2') is probably better than using has('unix'). – Martin Tournoij Apr 28 '15 at 17:30

Based on Drew's answer this setup worked better for me.

It increases or decreases font-size using \+ or \- (assuming \ is your leader), and cycles through the predefined fonts using cot. I often swap between these fonts depending on whether I am working with source code or text.

let g:fnt_types = ['SourceCode\ Pro', 'monofur' ]
let g:fnt_sizes = [ 10, 13 ]


let g:fnt_index = 0
let g:fnt_size  = g:fnt_sizes[g:fnt_index]

function! CycleFont()
  let g:fnt_index = (g:fnt_index + 1) % len(g:fnt_types)
  let g:fnt_size  = g:fnt_sizes[g:fnt_index]
  call ResetFont()
endfunction

function! ResetFont ()
  if has('gui_running')
    exe ':set guifont=' . g:fnt_types[g:fnt_index] . '\ ' . string(g:fnt_size)
  endif
endfunction

call ResetFont()

function! FontSizePlus ()
  let g:fnt_size = g:fnt_size + 0.5
  call ResetFont()
endfunction

function! FontSizeMinus ()
  let g:fnt_size = g:fnt_size - 0.5
  call ResetFont()
endfunction

nnoremap <leader>+ :call FontSizePlus()<cr>
nnoremap <leader>- :call FontSizeMinus()<cr>
nnoremap cot :call CycleFont()<cr>

Apparently, the way to write the font settings are quite platform-dependent. What worked for me (gvim in MX Linux 17, (a Debian-based Linux distribution with an XFCE desktop)):

  • Determine the current font settings in an open gvim instance using : set guifont?

Example: guifont=DejaVu Sans Mono 12

  • Use this information to write the settings (modifying the size as desired) in the ~/.gvimrc file, escaping the spaces with backslashes, and not using : before the size.

Example: set guifont=DejaVu\ Sans\ Mono\ 18

  • After saving the ~/.gvimrc file, new instances of gvim should use the new desired size.

These two answers helped me:

As well as the comments of this one:

You can try this plugin:vim-fontzoom.

map <m-o> :Fontzoom!<cr>
map <m--> <Plug>(fontzoom-smaller)
map <m-=> <Plug>(fontzoom-larger)

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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