Vim Tips Wiki

Change font size quickly

1,625pages on
this wiki
Add New Page
Talk0 Share

Duplicate tip

This tip is very similar to the following:

These tips need to be merged – see the merge guidelines.

Tip 760 Printable Monobook Previous Next

created 2004 · complexity intermediate · author Wouter Bolsterlee · version 6.0

If you regularly switch to a larger or smaller font, for example because someone looking at your code thinks the letters are too small, or because you want to lay back in your chair while reading, this tip is for you.

The following script defines two commands, :LargerFont and :SmallerFont, to allow quick adjustments to the font size used in the gtk2 gui. Change minfontsize and maxfontsize to suit your needs. See below for alternative solutions.

To use this script, put the following code into ~/.vim/plugin/gtk2fontsize.vim or in your vimrc.

let s:pattern = '^\(.* \)\([1-9][0-9]*\)$'
let s:minfontsize = 6
let s:maxfontsize = 16
function! AdjustFontSize(amount)
  if has("gui_gtk2") && has("gui_running")
    let fontname = substitute(&guifont, s:pattern, '\1', '')
    let cursize = substitute(&guifont, s:pattern, '\2', '')
    let newsize = cursize + a:amount
    if (newsize >= s:minfontsize) && (newsize <= s:maxfontsize)
      let newfont = fontname . newsize
      let &guifont = newfont
    echoerr "You need to run the GTK2 version of Vim to use this function."

function! LargerFont()
  call AdjustFontSize(1)
command! LargerFont call LargerFont()

function! SmallerFont()
  call AdjustFontSize(-1)
command! SmallerFont call SmallerFont()

Related pluginsEdit

As an alternative, you could use one of these plugins which work with fonts on Unix and Windows systems:

  • quickfonts.vim : quickly switch between a list of favorite fonts, manage list of favorite fonts
  • guifont++.vim : Vim plugin for quickly changing GUI font size
  • fontsize : Vim plugin for adjusting GUI font size via "font size" mode single-key shortcuts

Mapping solutionEdit

Another alternative is to use the following mappings which use a clever substitute (no functions are required). This example works using gvim under Windows, where the command :set guifont? would show the font name and :h12 if the font size is 12 points, for example.

nnoremap <C-Up> :silent! let &guifont = substitute(
 \ &guifont,
 \ ':h\zs\d\+',
 \ '\=eval(submatch(0)+1)',
 \ '')<CR>
nnoremap <C-Down> :silent! let &guifont = substitute(
 \ &guifont,
 \ ':h\zs\d\+',
 \ '\=eval(submatch(0)-1)',
 \ '')<CR>

For example, the command :echo &guifont might show Bitstream_Vera_Sans_Mono:h12:b:cANSI.

The substitute pattern :h\zs\d\+ looks for :h followed by a number (one or more digits). The \zs marks the start of what is found (so it skips the :h).

The replacement string \=eval(submatch(0)+1) uses:

Following is an expression to be evaluated.
The text that matched (this example finds 12 with \d\+).


Evaluate the following string (12+1).

If you have multiple fonts listed, such as

:echo &guifont

you can add 'g' to the last argument of the substitute command above to 'globally' replace all the font sizes.

nnoremap <C-Up> :silent! let &guifont = substitute(
 \ &guifont,
 \ ':h\zs\d\+',
 \ '\=eval(submatch(0)+1)',
 \ 'g')<CR>
nnoremap <C-Down> :silent! let &guifont = substitute(
 \ &guifont,
 \ ':h\zs\d\+',
 \ '\=eval(submatch(0)-1)',
 \ 'g')<CR>



Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.