67

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?

1

8 Answers 8

56

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.

8
  • 11
    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... Apr 27, 2015 at 22:14
  • :hXX works on the Mac as well. Good point about GTK though.
    – user72
    Apr 27, 2015 at 22:31
  • set guifont=? Not set gfn=? Mar 1, 2018 at 23:13
  • 3
    @PeterMortensen Both work; I prefer the longer forms because I find them more readable, and especially tend to use the longer ones in answers.
    – user72
    Mar 2, 2018 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.
    – AturSams
    Jun 15, 2019 at 7:04
53

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.

1
  • 2
    On Linux this line should look like: guifont=Hasklig\ Light\ 16
    – piotao
    Oct 27, 2020 at 21:39
15

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
2
  • 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'). Apr 28, 2015 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, 2018 at 2:00
10

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

  • Determine the current font settings in an open gvim instance
: set guifont?
‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
 guifont=DejaVu Sans Mono 12
  • Use this information to write the settings (modifying the size as desired) in the gVim configuration file, escaping the spaces with backslashes, and not using : before the size.
~/.gvimrc
‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
set guifont=DejaVu\ Sans\ Mono\ 18`
  • After saving, new instances of gvim should use the new desired size.
References

How to save font choice in gVim? - Ask Ubuntu

vim - List of fonts available in gvim - Stack Overflow

How can I change the font size in gVim? - Vi and Vim Stack Exchange (comments)

5

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

You can try this plugin:vim-fontzoom.

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

Cycling over bitmap fonts

The other solutions work for GTK, but if the GUI is Athena, Motif or neXt, then XLFD is used to specify the bitmap font, for example

-*-fixed-medium-r-*-*-15-*-*-*-*-*-*-*

The first numeric value is the pixel size, and differently from other types of fonts, only a discrete set of them is available, which you have to determine with Xfontsel or Xlsfonts and put in the fSizes list below.

To cycle over those values with F1 and F2:

function FontSizeCycle(arg)
    let fSizes=['9', '10', '13', '15', '18', '20']
    let idx=(index(fSizes, matchstr(&guifont, '\d\+'))+a:arg)%len(fSizes)
    exe 'set guifont=*-fixed-medium-r-*-*-'.fSizes[idx].'-*-*-*-*-*-*-*'
    set lines=999 columns=999
endfunction

noremap <F1> :call FontSizeCycle(-1)<CR>
noremap <F2> :call FontSizeCycle(+1)<CR>

set lines=999 columns=999 forces Gvim to recalculate the screen size.

0

Drew's answer helped me fix a flaw in my code so i decided to share. You can change the binds at the bottom, but the way i use it is:

<F11>           Decrease font size.
<F12>           Increase font size.
<Shift><F11>    Set font size to 10.
<Shift><F12>    Display the font selector if compiled with 'GUI'.

Slightly more complex regular expressions in this code allow for parsing and displaying font name and size as you zoom in and out. This code is tested on Windows and Linux.

function! AdjustFontSize(amount)
    if !has("gui_running")
        return
    endif

    let l:min_font_size = 5
    let l:max_font_size = 23

    let l:font_info = GetFontInfo()
    if l:font_info.name == '' || l:font_info.size == ''
        return
    endif

    let l:font_name = l:font_info.name
    let l:font_size = l:font_info.size

    " Decrease font size.
    if a:amount == '-'
        let l:font_size = l:font_size - 1

    " Increase font size.
    elseif a:amount == '+'
        let l:font_size = l:font_size + 1

    " Use a specific font size.
    elseif str2nr(a:amount)
        let l:font_size = str2nr(a:amount)
    endif

    " Clamp font size.
    let l:font_size = max([l:min_font_size, min([l:max_font_size, l:font_size])])

    if matchstr(&guifont, ':') == '' " Linux guifont style.
        " \v           Very magical.
        " (\d+$)       Capture group:       Match [0-9] one-or-more times, at the end of the string.
        let l:font_size_pattern = '\v(\d+$)'
    else " Windows and macOS guifont style.
        " \v           Very magical.
        " (:h)@<=      Positive lookbehind: Match ':h'.
        " (\d+)        Capture group:       Match [0-9] one-or-more times.
        let l:font_size_pattern = '\v(:h)@<=(\d+)'
    endif

    " Update vim font size.
    let &guifont = substitute(&guifont, l:font_size_pattern, l:font_size, '')

    call DisplayFontInfo()
endfunction

function! DisplayFontSelector()
    if !has("gui_running")
        return
    endif

    " Display font selector.
    " NOTE: This only changes &guifont to '*' in terminal vim.
    set guifont=*

    " Display font info after font selector closes.
    call DisplayFontInfo()
endfunction

function! DisplayFontInfo()
    let l:font_info = GetFontInfo()
    if l:font_info.name == '' || l:font_info.size == ''
        return
    endif

    " Display font name and size.
    redraw | echomsg l:font_info.name . ' ' . l:font_info.size . '%'
endfunction

function! GetFontInfo()
    " Windows and macOS &guifont: Hack NF:h11:cANSI
    "                             3270Medium_NF:h10:W500:cANSI:qDRAFT
    " Linux &guifont: Hack Nerd Font Mono Regular 10

    if matchstr(&guifont, ':') == '' " Linux guifont style.
        " \v           Very magical.
        " (^.{-1,})    Capture group:       Anchored at the start of the string, match any character one-or-more times non-greedy.
        " ( \d+$)@=    Positive lookahead:  Match ' ' followed by [0-9] one-or-more times, at the end of the string.
        let l:font_name_pattern = '\v(^.{-1,})( \d+$)@='

        " \v           Very magical.
        " (\d+$)       Capture group:       Match [0-9] one-or-more times, at the end of the string.
        let l:font_size_pattern = '\v(\d+$)'
    else " Windows and macOS guifont style.
        " \v           Very magical.
        " (^.{-1,})    Capture group:       Anchored at the start of the string, match any character one-or-more times non-greedy.
        " (:)@=        Positive lookahead:  Match ':'.
        let l:font_name_pattern = '\v(^.{-1,})(:)@='

        " \v           Very magical.
        " (:h)@<=      Positive lookbehind: Match ':h'.
        " (\d+)        Capture group:       Match [0-9] one-or-more times.
        let l:font_size_pattern = '\v(:h)@<=(\d+)'
    endif

    let l:font_name = matchstr(&guifont, l:font_name_pattern)
    let l:font_size = matchstr(&guifont, l:font_size_pattern)

    return { 'name' : l:font_name, 'size' : l:font_size }
endfunction

" Bind Control + Mouse-wheel to zoom text.
" NOTE: This event only works in Linux and macOS. SEE: :h scroll-mouse-wheel
map <silent> <C-ScrollWheelDown> :call AdjustFontSize('-')<CR>
map <silent> <C-ScrollWheelUp> :call AdjustFontSize('+')<CR>

" Decrease font size.
nnoremap <silent> <F11> :call AdjustFontSize('-')<CR>
inoremap <silent> <F11> <Esc>:call AdjustFontSize('-')<CR>
vnoremap <silent> <F11> <Esc>:call AdjustFontSize('-')<CR>
cnoremap <silent> <F11> <Esc>:call AdjustFontSize('-')<CR>
onoremap <silent> <F11> <Esc>:call AdjustFontSize('-')<CR>

" Increase font size.
nnoremap <silent> <F12> :call AdjustFontSize('+')<CR>
inoremap <silent> <F12> <Esc>:call AdjustFontSize('+')<CR>
vnoremap <silent> <F12> <Esc>:call AdjustFontSize('+')<CR>
cnoremap <silent> <F12> <Esc>:call AdjustFontSize('+')<CR>
onoremap <silent> <F12> <Esc>:call AdjustFontSize('+')<CR>

" Set font size to my preferred size.
nnoremap <silent> <S-F11> :call AdjustFontSize(10)<CR>
inoremap <silent> <S-F11> <Esc>:call AdjustFontSize(10)<CR>
vnoremap <silent> <S-F11> <Esc>:call AdjustFontSize(10)<CR>
cnoremap <silent> <S-F11> <Esc>:call AdjustFontSize(10)<CR>
onoremap <silent> <S-F11> <Esc>:call AdjustFontSize(10)<CR>

" Display font selector.
nnoremap <silent> <S-F12> :call DisplayFontSelector()<CR>
inoremap <silent> <S-F12> <Esc>:call DisplayFontSelector()<CR>
vnoremap <silent> <S-F12> <Esc>:call DisplayFontSelector()<CR>
cnoremap <silent> <S-F12> <Esc>:call DisplayFontSelector()<CR>
onoremap <silent> <S-F12> <Esc>:call DisplayFontSelector()<CR>
2
  • This is a lot of code, you might want to add a few explanations about how it work and which are the important/tricky part. This way people reading your answer can not just copy/paste your code but also learn from your experience.
    – statox
    Mar 21 at 10:35
  • @statox Oops I totally forgot to copy the bind lines. Hopefully that clears it up. Mar 21 at 23:14

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.