Detect platform

2 views
Skip to first unread message

703designs

unread,
Nov 12, 2008, 8:55:25 PM11/12/08
to vim_use
How can I detect my current platform in Vim? I share my configuration
across platforms, and I need to set the font selectively:

I use courier on Windows, and Dejavu Sans on all other platforms.

Thanks,
Thomas

Tony Mechelynck

unread,
Nov 12, 2008, 9:41:37 PM11/12/08
to vim...@googlegroups.com

Well, the key here is the has() function.

For a quick and dirty test, use

if has('unix')
" unix-like platform (including Cygwin)
else
" probably Windows
endif

For the 'guifont' option, however, you need to take care of the five
different incompatible formats used by various versions of Vim:

if has('gui')
" we use has('gui') rather than has('gui_running') here
" so it will work even if we start Console Vim first
" then run :gui manually (which is only possible on Unix)
if has('gui_gtk2')
set gfn=DejaVu\ Sans\ Mono\ 11
elseif has('gui_photon')
set gfn=DejaVu\ Sans\ Mono:s11
elseif has('gui_kde')
" the obsolete kvim
" just make sure it works correctly if it hits our vimrc
set gfn=DejaVu\ Sans\ Mono/11/-1/5/50/0/0/0/1/0
elseif has('x11')
" I'm guessing the following (other-X11 including GTK1)
" please check, and correct if necessary.
" On GTK1 (and maybe some others) you can use :set gfn=*
" Replace by asterisks like here
" to make it a little more general:
set gfn=-*-dejavu-medium-r-normal-*-*-110-*-*-m-*-*
" add another elseif here
" if you want DejaVu on mac-without-x11
else
" not x11 (probably Windows)
set gfn=Courier_New:h11:cDEFAULT
endif
endif

In all cases the first or only number is the size (use, of course, a
larger number for larger type, a smaller number for smaller type).

See http://vim.wikia.org/wiki/Setting_the_font_in_the_GUI


Best regards,
Tony.
--
Hackers do it with fewer instructions.

703designs

unread,
Nov 12, 2008, 10:01:00 PM11/12/08
to vim_use
Thanks, I'll bookmark this reply in case I end up on other platforms
using GVim. For my purposes, this will work well:

if has("gui_running")
if has("unix")
set guifont=DejaVu\ Sans\ Mono:h12:cDEFAULT
else
set guifont=Courier_New:9:cDEFAULT
endif
" Other settings
endif

Thomas


On Nov 12, 9:41 pm, Tony Mechelynck <antoine.mechely...@gmail.com>
wrote:
> Seehttp://vim.wikia.org/wiki/Setting_the_font_in_the_GUI

Tony Mechelynck

unread,
Nov 12, 2008, 10:29:16 PM11/12/08
to vim...@googlegroups.com
On 13/11/08 04:01, 703designs wrote:
> Thanks, I'll bookmark this reply in case I end up on other platforms
> using GVim. For my purposes, this will work well:
>
> if has("gui_running")
> if has("unix")
> set guifont=DejaVu\ Sans\ Mono:h12:cDEFAULT

did you test the above? AFAIK, on X11 it will _not_ work. Depending on
the particular GUI flavour (and possibly on the same machine) you need
something ending in "\ 12" for a GTK2 GUI, in ":s12" for a Photon GUI,
and an |XLFD| (which is a long dash-separated string with no spaces or
colons in it) for other X11 GUIs such as Athena, Motif or GTK1 (but not
the obsolete kvim which might still be included in some Linux
distributions, and uses another format again); using the "wrong" one
won't work.

> else
> set guifont=Courier_New:9:cDEFAULT
> endif
> " Other settings
> endif
>
> Thomas

Best regards,
Tony.
--
A bug in the hand is better than one as yet undetected.

703designs

unread,
Nov 12, 2008, 10:32:59 PM11/12/08
to vim_use
For now, it only needs to work on my Mac (MacVim or GVim...I custom-
compiled GVim just now in a futile effort to get it to detect my
custom $PATH when not invoked from the shell) and on my office Windows
box (GVim as well). I'm sure that Debian would not like this one bit,
so once this Macbook breaks down I'll have to further evolve my font
settings.

Thanks for all the help!

Thomas

On Nov 12, 10:29 pm, Tony Mechelynck <antoine.mechely...@gmail.com>
wrote:

yosi izaq

unread,
Nov 16, 2008, 9:57:33 AM11/16/08
to vim...@googlegroups.com
Great tip!

I have added this section to my .vimrc on cygwin:
However, the selected font when I start gvim is Sans, regular 12.
While I'd have like to have Sans Bold 13. Can you please suggest how
to make do it?

BTW, I'm using this alias for gvim for setting font. It works well for me:
gvim -font Courier\ Bold\ 13

10x,
Yosi

Tony Mechelynck

unread,
Nov 16, 2008, 2:45:50 PM11/16/08
to vim...@googlegroups.com

Well, change the appropriate line(s). I would not recommend setting your
default font to Bold, because then you would lose the distinction (on
the gvim display) between bold (like e.g. the current statusline, or
<b>...</b> in HTML, etc.) and unbold (like the statuslines of
non-current split windows, non-bold HTML, etc.) Yours seem to be the
"GTK2" GUI flavour, in which case the line to change is the "set gfn="
under "has('gui_gtk2')". Increase the number for a larger font, etc. For
size-13 DejaVu Sans Mono Bold (which I don't recommend) on GTK2, I guess
it would be, well, "set gfn=DejaVu\ Sans\ Mono\ Bold\ 13".

The whole idea of the above snippet is to have something readable on all
possible platforms (and 'guifont' nonempty), which you can then tweak if
it isn't exactly what you want. If bitmapped Courier 13 bold is your
favourite font, well insert "set gfn=Courier\ Bold\ 13" under
has('gui_gtk2') and presto! voilà.


Best regards,
Tony.
--
"What is wanted is not the will to believe, but the will to find out,
which is the exact opposite."
-- Bertrand Russell, "Skeptical_Essays", 1928

yosi izaq

unread,
Nov 18, 2008, 8:18:25 AM11/18/08
to vim...@googlegroups.com
That was very helpful.
Thank you!

Yosi
Reply all
Reply to author
Forward
0 new messages