emacs - Change fonts

How do I change Emacs’ default font size and font type?

  1. https://www.emacswiki.org/emacs/SetFonts
  2. http://xahlee.info/emacs/emacs/emacs_list_and_set_font.html
  3. https://stackoverflow.com/questions/6026713/how-do-i-change-emacs-default-font-size-and-font-type

What font am I currently using?

Method 1

https://superuser.com/questions/422968/how-to-find-out-current-font-used-in-my-emacs

(face-attribute 'default :font)

That will give you outputs like this

#<font-object "-UKWN-Adwaita Mono-regular-normal-normal-*-15-*-*-*-m-0-iso10646-1">

or

(In Windows machines)

#<font-object "-outline-Courier New-regular-normal-normal-mono-20-*-*-*-c-*-iso8859-1">

Method 2

  1. Put your mouse point on a character in the buffer.
  2. Run this
    C-u C-x =
    
  3. What command is “C-u C-x =” a shortcut to?
  4. C-x = is bound to what-cursor-position, but when called with a C-u prefix, it mostly delegates the work to describe-char.
  5. A new buffer will open showing information about the character, including the font name. Example output:
                    position: 561 of 1293 (43%), column: 20
                character: u (displayed as u) (codepoint 117, #o165, #x75)
                  charset: ascii (ASCII (ISO646 IRV))
    code point in charset: 0x75
                   script: latin
                   syntax: w	which means: word
                 category: .:Base, L:Strong L2R, 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 undecided-unix)
                  display: by this font (glyph code):
        ftcrhb:-UKWN-Adwaita Mono-regular-normal-normal-*-15-*-*-*-m-0-iso10646-1 (#x58)
    
    Character code properties: customize what to show
      name: LATIN SMALL LETTER U
      general-category: Ll (Letter, Lowercase)
      decomposition: (117) ('u')
    
    There is an overlay here:
     From 541 to 578
      face                 hl-line
      priority             -50
      window               #<window 156 on 20250413223511-emacs_change_fonts.org>
    
    
    There are text properties here:
      fontified            t
    
    [back]
    
  6. Look for frcrhb output

What fonts are available in my computer?

  1. Look at these folders:
    1. ~/.local/share/fonts/ttf/
    2. usr/share/fonts
  2. Download and put files in this location.
$ fc-list -f "%{fullname} : %{foundry} : %{family}\n"

To filter results

$ fc-list -f "%{fullname} : %{foundry} : %{family}\n"  | grep Ubuntu

$ fc-list -f "%{fullname} : %{foundry} : %{family}\n"  | grep "Noto Sans"

How can I install more new fonts?

Using package managers

sudo pacman -S ttf-ubuntu-font-family

To install manually, download ttf files and put them in the fonts directory.

mv Ubuntu-R.ttf ~/.local/share/fonts/

Refresh font cache

fc-cache -fv

What exactly is the difference between `font-family` and `font-foundry`?

https://emacs.stackexchange.com/questions/53152/what-exactly-is-the-difference-between-font-family-and-font-foundry

How to pick a font that would not mess up dired view or org-tables view in org files?

Why are Sans fonts messing up alignment?

Using sans-serif or other variable-width fonts in Emacs causes alignment issues because Emacs is fundamentally designed for monospaced (fixed-width) fonts. In a monospaced font, every character occupies the exact same amount of horizontal space, which allows Emacs to calculate indentation and column alignment reliably. Variable-width fonts, on the other hand, use different amounts of horizontal space for each character, which breaks Emacs’ alignment-dependent features.

How monospaced fonts ensure alignment

  1. Uniformity: In a monospaced font, the letters ‘i’, ’m’, and ‘w’ all take up the same width. This makes it possible to create perfect vertical columns of text, a critical feature for code, tables, and other text-based data.
  2. Predictable spacing: Emacs uses a character’s position on a grid to determine its location and indentation level. With a fixed-width font, it can calculate that moving a certain number of characters will always place you at a specific column.
  3. Tab character display: Emacs relies on a fixed character width to correctly render tab characters as a consistent amount of space.

Why variable-width fonts cause alignment to break

  1. Inconsistent character width: In a sans-serif (or proportional) font, narrow characters like ‘i’ and ’l’ take up less space than wide characters like ’m’ and ‘w’. When you combine characters of different widths, columns no longer line up vertically.
  2. Issues with features like : Features in Emacs that align text based on regular expressions, like , depend on the predictable spacing of a monospaced font. These features will produce misaligned, uneven output when used with a variable-width font.
  3. Problems with tables and wrapping: Text-based tables in Org mode and other features that rely on precise spacing, like word-wrapping, will break with a variable-width font.

How to use variable-width fonts without breaking Emacs

It is possible to use variable-width fonts for some portions of your text while retaining fixed-width for code and other aligned structures. A common approach is to set different fonts for different contexts.

  1. Mixed fonts for Org mode: For a better reading experience in Org mode files, you can configure Emacs to use a variable-width font for regular text while keeping a monospaced font for code blocks and tables. The package is designed for this purpose.
  2. Set faces for specific content: You can use Emacs’s face system to apply different fonts to specific types of text. For instance, you could set the face for code blocks and tables while using a face for regular prose. [8, 10, 11, 12, 13]

How to configure a monospaced font in Emacs

To fix alignment issues, ensure you are using a monospaced font as your default. You can do this by setting the font in your file.

(set-frame-font "Cascadia Code-15")

This example sets the font for the graphical frame. Replace with the desired monospaced font and size.

Changing fonts

Option 1

(set-frame-font "Open Sans 10" nil t)

or

(set-frame-font "Open Sans" nil t)

This font is messing up the dired view though.

DejaVu Sans did not mess up the dired view.

(set-frame-font "DejaVu Sans Mono-10" nil t)
  1. The nil argument means to apply the font to the current frame.
  2. The t argument indicates that the font should be saved for future frames as well.

Font-specific properties: Some fonts might support additional properties like spacing or weight which can be specified in the font string, e.g., “Courier Prime:spacing=100:size=18”.

Option 2

  1. M-x customize-group
  2. Enter faces
  3. Click on “Basic Faces”
  4. Open / Expand “Default”
  5. Change the font name (like “Inconsolata”) and deselect “foundry”
  6. Click “Apply and save”

Links to this note