I would like to configure the default Emacs font in my init.el (as opposed to using M-x customize that a lot of other questions here on Emacs Stack Exchange has already explained).

I have tried the following;

(when (eq system-type 'gnu/linux)
    (set-default-font "-unknown-Hack-normal-normal-normal-*-14-*-*-*-m-0-iso10646-1"))

This works, the result is what I want, the font size is correct. If I place point on a char and do C-u C-x = I get the following;

position: 171 of 841 (20%), column: 0
character: R (displayed as R) (codepoint 82, #o122, #x52)
preferred charset: ascii (ASCII (ISO646 IRV))
code point in charset: 0x52
script: latin
syntax: w   which means: word
category: .:Base, L:Left-to-right (strong), a:ASCII, l:Latin, r:Roman
to input: type "C-x 8 RET 52" or "C-x 8 RET LATIN CAPITAL LETTER R"
buffer code: #x52
file code: #x52 (encoded by coding system utf-8-unix)
display: by this font (glyph code)
xft:-unknown-Hack-normal-normal-normal-*-14-*-*-*-m-0-iso10646-1 (#x4E)

But I have a feeling this is a somewhat temporary way of doing this.

Another way I tried was;

(when (eq system-type 'gnu/linux)
  (add-to-list 'default-frame-alist '(font . "Hack-10")))

This works so far as to set the font but the font size is just a bit to small;

position: 15130 of 22797 (66%), column: 4
character: d (displayed as d) (codepoint 100, #o144, #x64)
preferred charset: ascii (ASCII (ISO646 IRV))
code point in charset: 0x64
script: latin
syntax: w   which means: word
category: .:Base, L:Left-to-right (strong), a:ASCII, l:Latin, r:Roman
to input: type "C-x 8 RET 64" or "C-x 8 RET LATIN SMALL LETTER D"
buffer code: #x64
file code: #x64 (encoded by coding system prefer-utf-8-unix)
display: by this font (glyph code)
xft:-unknown-Hack-normal-normal-normal-*-13-*-*-*-m-0-iso10646-1 (#x87)

Now the font size is said to be 13 instead of 14 which is what I want. The numbers used when setting the default font using set-default-font and using default-frame-alist seems to have different meaning, that is the "14" and the "10".

Can I use default-frame-alist to set my default font to Hack with size 14 or is there a better way of doing this in the init file?

I have the following in my custom.el file:

(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(default ((t (:height 100 :family "Hack")))))

It sets up Hack font to be 10pt.

Upd:

This custom.el file is loaded from init.el:

;; Keep 'Customize' stuff separated
(setq custom-file (concat user-emacs-directory "custom.el"))
(load custom-file 'noerror)
  • I'm sure this works (I haven't tried it) but this places the configuration on custom.el. I want the configuration in init.el to keep all my configuration in one place. – Mikael Springer Mar 8 at 8:21
  • You should have the section in your init.el with (custom-set-faces...). But I like those to be in separate file. One separate file per each laptop or pc I work with. – Maxim Kim Mar 8 at 15:24

Add this to your init.el file: (set-frame-font "Hack-14" nil t)

  • This causes the font to be of size 19 which is to big. – Mikael Springer Mar 8 at 8:45

I doing

(add-to-list 'default-frame-alist '(font . "Hack"))
(set-face-attribute 'default nil
            :family "Hack")

This is what I used in the end, the result is exactly what I want:

(when (eq system-type 'gnu/linux)
  (set-face-attribute 'default nil
              :font "-unknown-Hack-normal-normal-normal-*-14-*-*-*-m-0-iso10646-1"))

With this I can set the font and font size in my init.el. The result of doing C-u C-x 0 is

position: 15347 of 23078 (66%), column: 30
character: u (displayed as u) (codepoint 117, #o165, #x75)    
preferred charset: ascii (ASCII (ISO646 IRV))
code point in charset: 0x75
           script: latin
           syntax: w    which means: word
         category: .:Base, L:Left-to-right (strong), a:ASCII, l:Latin, r:Roman
         to input: type "C-x 8 RET 75" or "C-x 8 RET LATIN SMALL LETTER U"
      buffer code: #x75
        file code: #x75 (encoded by coding system prefer-utf-8-unix)
          display: by this font (glyph code)
xft:-unknown-Hack-normal-normal-normal-*-14-*-*-*-m-0-iso10646-1 (#xD1)

The size is 14 which is what I want. I came to this conclusion using the answers above and the EmacsWiki.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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