Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to easily test for guifont in vimrc?

Tags:

vim

I have many different machines that I am logging into and none of them have decent fonts in common. So, I would like to be able to have my first preferance for a font but if it does not exist on the machine use another one and so on. Has anyone done this before?

...

So after re-reading the help on guifont it seems that I should be able to have multiple fonts defined seperated by commas. The funny thing is though I have the following line in my .vimrc:

set guifont=-dt-interface\ user-medium-r-normal-*-*-100-*-*-m-*-iso8859-1,Liberation\ Mono\ 8

And on one machine (gvim 7.1) I do a

:set gfn?
guifont=-dt-interface user-medium-r-normal-*-*-100-*-*-m-*-iso8859-1

And on another machine (gvim 7.2) I get:

:set gfn?
guifont=-dt-interface user-medium-r-normal-*-*-100-*-*-m-*-iso8859-1,Liberation Mono 8

I will try to run 7.1 on the incorrect machine and see if it fixes the problem and if so doesn't it seem like a bug in 7.2?


SOLUTION

Here is the code I cam up with to fix the problem. It seems very brittle but it at least allows me to make some progress with my real work for now:

" SETTINGS FOR GUI ONLY MODE
: Trying to emulate how it should work but on some machines it will not select the available font
" set guifont=-dt-interface\ user-medium-r-normal-*-*-100-*-*-m-*-iso8859-1,Liberation\ Mono\ 8
let g:MyFontPre = ''
let g:MyFontPost = ''
let g:MyFontSize = '8'
if has("gui_running")
    if ( match(hostname(), 'server5-1..') >= 0 )
      let g:MyFontSize = '10'
      let g:MyFontPre = '-dt-interface\ user-medium-r-normal-*-*-'
      let g:MyFontPost = '0-*-*-m-*-iso8859-1'
    elseif ( match(hostname(), 'server5-3..') >= 0 )
      let g:MyFontPre = 'Liberation\ Mono\ '
      let g:MyFontPost = ''
    else
      " Leave it the default for now
    endif
    execute "set guifont=".g:MyFontPre."".g:MyFontSize."".g:MyFontPost
endif


" FONT SIZE SHORTCUTS
function! ToggleMyFontSize()
  if ( g:MyFontSize == 12 )
    let g:MyFontSize = 8
    execute "set guifont=".g:MyFontPre."".g:MyFontSize."".g:MyFontPost
  elseif ( g:MyFontSize == 8 )
    let g:MyFontSize = 10
    execute "set guifont=".g:MyFontPre."".g:MyFontSize."".g:MyFontPost
  elseif ( g:MyFontSize == 10 )
    let g:MyFontSize = 12
    execute "set guifont=".g:MyFontPre."".g:MyFontSize."".g:MyFontPost
  endif
endfunction
nnoremap  <silent>  <F12> :call ToggleMyFontSize()<CR>
like image 249
stephenmm Avatar asked Jun 17 '09 20:06

stephenmm


2 Answers

If using commas isn't working for you, how about this to allow graceful font degradation? This trick can be used for other 'set' options as well.

silent! set guifont=Font\ One:h8
if &guifont != 'Font One:h8'
    set guifont=Font\ Two:h8
endif
like image 78
Unk Avatar answered Nov 04 '22 15:11

Unk


Not a direct solution to your problem, but you could use hostname() and a conditional statement to select the font you would like:

  if hostname() == 'home-pc'
      set guifont=...
  elseif hostname() == 'work-pc'
      set guifont=...
  else
      set guifont=...
  endif
like image 5
too much php Avatar answered Nov 04 '22 13:11

too much php