Software Engineer's Blog

set guifont Syntax for gVim on Windows vs. Ubuntu

set guifont Syntax for gVim on Windows vs. Ubuntu

If you sync your .vimrc dotfiles between your Windows and Ubuntu (Linux) machines, you’ve probably run into this frustrating problem: your set guifont setting works perfectly on one OS but fails on the other. 🖥️

This is because Windows gVim and Linux (GTK) gVim use completely different syntax for defining fonts.

Here’s a simple guide to understanding the difference and how to make your .vimrc work on both.

1. The Windows Way: Colons (:)

Windows gVim uses a colon-separated string to define the font and its attributes. Spaces in font names are generally allowed without special handling.

The most common format is FontName:h[size].

" Windows gVim Syntax
set guifont=D2Coding:h12
set guifont=Consolas:h11:cDEFAULT
  • D2Coding: The name of the font.
  • :h12: Sets the height (size) to 12 points.
  • :cDEFAULT or :cHANGEUL: (Optional) Specifies the character set.

2. The Ubuntu/Linux Way: Backslashes (\)

Linux gVim (which usually uses the GTK toolkit) expects a single string where spaces are part of the syntax, separating the font name from the size.

This means any spaces inside the font name itself must be “escaped” with a backslash (\).

" Linux (GTK) gVim Syntax
set guifont=D2Coding\ 12
set guifont=Ubuntu\ Sans\ Mono\ Regular\ 13
  • Ubuntu\ Sans\ Mono\ Regular: The full font name. Each space is preceded by a \ so Vim reads it all as one name.
  • 13: The font size, separated by a final (unescaped) space.

If you tried to use set guifont=D2Coding:h12 on Linux, it would fail because it doesn’t understand the :h12 syntax.

Pro Tip: How to Use Both in One .vimrc

So, how do you create one config file for both systems? You can use a simple if conditional to check which OS you’re on.

Vim has a built-in function has("win32") which returns true only on Windows.

Here is the “best practice” to add to your .vimrc:

" --- Smart GUI Font Settings ---
if has("win32")
  " This is Windows
  set guifont=D2Coding:h12
else
  " This is Linux (or macOS)
  set guifont=D2Coding\ 12
endif

By adding this logic, your gVim will always have the correct font, no matter which operating system you launch it on.