38

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?

30

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 '18 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 '18 at 3:52
  • Note that the file name in Windows (a common OS choice) is actually: for MS-DOS and Win32: $VIM\_gvimrc or in my case that file didn't exist and I needed to create it that way. – wolfdawn Jun 15 at 7:04
28

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. You can then ask Vim to print the line you would need to add to your ~/.gvimrc to make the change permanent:

:set guifont?

Produces something like the following in the status line:

guifont=Hasklig-Light:h16

Add that with the prefix set to your ~/.gvimrc to persist.

13

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
  • 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
  • This works on Xubuntu (Xfce) where :set guifont? returns text like "Monospace Bold 11", without the colon separators and the "h" prefix that the selected answer relies on. – NeilG Nov 27 '18 at 2:00
5

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:

4

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>
2

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 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.