84

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

11 Answers 11

66

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
  • 12
    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... Commented Apr 27, 2015 at 22:14
  • :hXX works on the Mac as well. Good point about GTK though.
    – user72
    Commented Apr 27, 2015 at 22:31
  • set guifont=? Not set gfn=? Commented 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
    Commented 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
    Commented Jun 15, 2019 at 7:04
63

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
    Commented Oct 27, 2020 at 21:39
24

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
3
  • 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'). Commented 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
    Commented Nov 27, 2018 at 2:00
  • 1
    I started with your script and added Windows capability, which turned out to be a lot more difficult than I first realised. I thought I may as well make it into a a plugin: github.com/eggbean/resize-font.gvim
    – paradroid
    Commented Nov 26, 2023 at 12:58
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
  • What is cot? I have found something else in vim's help: 'completeopt' Commented Jun 28, 2022 at 17:22
  • It's a keybinding defined in my answer, it calls a function that changes font.
    – gauteh
    Commented Jun 29, 2022 at 18:29
  • sorry if I miss here something trivial, but I do not find the definition of 'cot'. A key-combination is supposed to be at that position. I have no 'cot' key in my keyboard, and I was curious what could it mean. Commented Jun 30, 2022 at 13:28
3

You can try this plugin:vim-fontzoom.

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

I started with Drew's answer and added Windows support, which turned out to be a lot more difficult than I first thought. I spent quite a few hours on it, so I thought I may as well make it into a plugin.

Works on Linux and Windows.

resizing gvim

https://github.com/eggbean/resize-font.gvim

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

Type the following in command mode to autocomplete the command you need with your current font setting:

:set guifont=<tab>

That will give you something like this:

:set guifont=Monospace\ Regular\ 8

Then backspace the 8 or whatever your fontsize is, add desired number and <enter> the new setting.

0

Following allow you to change font size by Control+scroll in gvim by adding to your .vimrc file

"set global variables for font and size
let g:font='Monospace'
let g:font_size=14
"set initial settings
let &guifont = g:font . "\ " . g:font_size

function! AdjustFontSize(amount)
    let g:font_size=g:font_size+a:amount
    let &guifont = g:font . "\ " . g:font_size
endfunction
noremap <C-ScrollWheelUp> :call AdjustFontSize(1)<CR>
noremap <C-ScrollWheelDown> :call AdjustFontSize(-1)<CR>
1
  • Note that the format of the value of guifont is system-dependent.
    – D. Ben Knoble
    Commented Apr 10, 2023 at 13:35
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 = 64

    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: Starting in version 9.0 this works in Windows. Previously it only worked 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 (10).
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
    Commented Mar 21, 2022 at 10:35
  • @statox Oops I totally forgot to copy the bind lines. Hopefully that clears it up. Commented Mar 21, 2022 at 23:14

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