Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gVim Dynamically Change Font Based on Window Size

Tags:

vim

I'm running gVim 7.3 on a GNU/Linux machine. I'm wondering if there is a way to change the font automatically based on the size of the window. I'm relatively new to Vim, so I'm not terrible familiar with it.

My situation is this: I'm using DejaVu Sans Mono 12 for my default font, but sometimes I like to switch down to Terminus 9 when I want to shrink the Vim window down to look at something on my screen while I type. (You know, for those of us with tiny laptop screens that don't want to keep switching between workspaces...!)

So I'm wondering if this behaviour can happen automatically with a command in the .vimrc file if I shrink the default window size down small enough so that Vim will automatically adjust the font.

Any ideas?

like image 538
qmoog Avatar asked Oct 26 '12 19:10

qmoog


1 Answers

Vim fires the VimResized event when its window size is changed. You can write an autocmd that adapts the font ('guifont') then. Here's an example that only considers 'columns' (not 'lines'), and has a hard-coded font name:

function! FontChangeOnResize()
    if &columns > 80
        set guifont=Lucida_Console:h14
    elseif &columns > 60
        set guifont=Lucida_Console:h12
    elseif &columns > 40
        set guifont=Lucida_Console:h10
    elseif &columns > 20
        set guifont=Lucida_Console:h8
    else
        set guifont=Lucida_Console:h6
    endif
endfunction
autocmd VimResized * call FontChangeOnResize()
like image 56
Ingo Karkat Avatar answered Oct 22 '22 21:10

Ingo Karkat