I want to use the same configuration for both Ubuntu and Windows, so I want to share my ~/.emacs between boxes. I'd like, however, to use the Consolas font in Windows, and Ubuntu Mono font in Ubuntu.

How can I autodetect which OS emacs is running in, and define the font code conditionally on the environment?


You can check the system-type and use set-frame-font accordingly.

Here is a short example:

(setq my-prefered-font
      (cond ((eq system-type 'windows-nt) "consolas")
            ((eq system-type 'gnu/linux) "mono")
            (t nil)))

(when my-prefered-font
  (set-frame-font my-prefered-font nil t))

Note, that you can also set font size. For example I do (setq my-prefered-font "Hack-10").

Hint: I found list-fontsets useful to list available fonts.

And one last note, I do the following to set system specific configurations in my init.el:

(load-file (expand-file-name
            (cond ((eq system-type 'windows-nt) "windows.el")
                  ((eq system-type 'gnu/linux) "linux.el")
                  (t "default-system.el"))

with windows.el, linux.eland default-system.el in folder ~\.emacs.d with all the system specific stuff.

| improve this answer | |
  • I really like your last suggestion, but it comes back with the following error: Symbol's function definition is void: load-with-warning. Maybe you defined this function? Is there a vanilla way of doing this, without custom functions? – philsf Aug 23 '16 at 12:54
  • 1
    @philsf You are right, I defined load-with-warning. I edited my answer accordingly to avoid the function. – theldoria Aug 23 '16 at 14:38
  • Thanks. I was trying to fix it unsuccesfully, but now I see what I was doing wrong. – philsf Aug 23 '16 at 14:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.