12

While fonts can be manually selected in GVim from the menu, I have a few preferred fonts I would like to switch between depending on the task at hand (small bitmap, larger OTF... etc).

Is there a way I can setup key-binding to cycle over a list of fonts predefined in my vimrc?

Martin Tournoij
  • 62,054
  • 25
  • 192
  • 271
ideasman42
  • 3,733
  • 3
  • 27
  • 34

2 Answers2

13

The basic idea could be something like:

" Define a list of the fonts you want to use, and the index in the 
" list of the default font. See :help Lists
let g:fc_list = [
\   "DejaVu Sans Mono 9",
\   "Source Code Pro 12",
\   "GohuFont 11"
\   ]
let g:fc_current = 0

" Set default font
let &guifont = g:fc_list[g:fc_current]

function! FontCycle()
  " Increment circular list. See :help expr-%
  let g:fc_current = (g:fc_current + 1) % len(g:fc_list)
  let &guifont = g:fc_list[g:fc_current]
endfunction

noremap <leader>fc :call FontCycle()<cr>
toro2k
  • 4,882
  • 1
  • 31
  • 35
7

I have the following defined in my .vimrc file.

set guifont=DejaVu\ Sans\ Mono\ for\ Powerline\ 10

So you can set that up as a mapping like this...

nmap <Leader>f :set guifont=DejaVu\ Sans\ Mono\ for\ Powerline\ 10<CR>

Add additional mappings for other fonts.

Quincy Bowers
  • 1,516
  • 10
  • 10