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?
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.
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.
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.
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
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
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):
: set guifont?
‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
guifont=DejaVu Sans Mono 12
:
before the size.~/.gvimrc
‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾
set guifont=DejaVu\ Sans\ Mono\ 18
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)
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>
You can try this plugin:vim-fontzoom.
map <m-o> :Fontzoom!<cr>
map <m--> <Plug>(fontzoom-smaller)
map <m-=> <Plug>(fontzoom-larger)
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.
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>
guifont
is system-dependent.
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.
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.
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>
:help 'guifont'
should have the information you need. Also see How do I navigate to topics in Vim's documentation?, Quickly switch between fonts at runtime, How to display Font Awesome in Vim?.