82

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?

glS
  • 3,869
  • 4
  • 18
  • 16

11 Answers11

65

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.

  • 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... – Martin Tournoij Apr 27 '15 at 22:14
  • :hXX works on the Mac as well. Good point about GTK though. –  Apr 27 '15 at 22:31
  • set guifont=? Not set gfn=? – Peter Mortensen Mar 01 '18 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. –  Mar 02 '18 at 03: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 '19 at 07:04
  • This answer is very helpful to me. When I set the guifont option in .vimrc, it did not work. But when I set the option in .gvimrc, it worked. – plhn Jul 27 '20 at 12:28
  • I was getting this error: E518: Unknown option: Code in linux. it worked when i escaped the space character with a backslash like this: set guifont=Fira\ Code\ 12 in my .gvimrc – Manik Aug 04 '21 at 14:48
  • @user72 I use long names in my vimrc and short names at the : prompt – Braden Best Jan 15 '22 at 01:25
62

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.

pkout
  • 721
  • 5
  • 3
22

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
Drew
  • 585
  • 2
  • 9
  • 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 02:00
  • 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: https://github.com/eggbean/resize-font.gvim – paradroid Nov 26 '23 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)

bli
  • 201
  • 3
  • 6
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>
gauteh
  • 173
  • 1
  • 5
  • What is cot? I have found something else in vim's help: 'completeopt' – Horror Vacui Jun 28 '22 at 17:22
  • It's a keybinding defined in my answer, it calls a function that changes font. – gauteh Jun 29 '22 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. – Horror Vacui Jun 30 '22 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)
tracyone
  • 345
  • 1
  • 6
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

paradroid
  • 367
  • 2
  • 10
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.

Quasímodo
  • 2,466
  • 9
  • 22
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

&quot; Decrease font size.
if a:amount == '-'
    let l:font_size = l:font_size - 1

&quot; Increase font size.
elseif a:amount == '+'
    let l:font_size = l:font_size + 1

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

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

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

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

call DisplayFontInfo()

endfunction

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

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

&quot; 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

&quot; 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(&amp;guifont, ':') == '' &quot; Linux guifont style.
    &quot; \v           Very magical.
    &quot; (^.{-1,})    Capture group:       Anchored at the start of the string, match any character one-or-more times non-greedy.
    &quot; ( \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+$)@='

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

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

let l:font_name = matchstr(&amp;guifont, l:font_name_pattern)
let l:font_size = matchstr(&amp;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>

FocusedWolf
  • 101
  • 3
  • 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 '22 at 10:35
  • @statox Oops I totally forgot to copy the bind lines. Hopefully that clears it up. – FocusedWolf Mar 21 '22 at 23:14
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.

markling
  • 359
  • 1
  • 10
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>