# Get Terminal Font Query As far as I can tell, there is no universal way to query what font is in use by the majority of terminal emulators. There are sometimes ways to get it anyway, with varying degrees of reliability. ## OSC Control Code This is the strongly preferred way to obtain the current font. It is a standard escape sequence and has the most support across terminals. [[ANSI - Control Codes]] using `OSC` to query font: ```sh echo -e "\e]50;?\e\\" ``` Example output[^1]: ``` \e]50;Fira Code\e\\ ``` Many terminals support multiple fallback fonts and/or use different fonts per text style, but this will only provide the primary font. ### Support - [[XTerm]] - [[ESPTerm]] - [[Contour Terminal]] - [[MinTTY]] - [[URXVT]] (partial?) - ... others ... ### Unsupported Tested [[2024-03-19]]. - [[Gnome Terminal]] (tested) - [[Kitty Term]] (tested, dev rejects the idea as "insufficient") - [[iTerm2]] (not tested, probably unsupported, since it previously mis-used it for clipboard access, even though `OSC 50` had been part of the standard since the 90s) - [[Alacritty]] (not tested, documentation says it only supports changing the cursor shape with `OSC 50`) - [[Konsole]] (tested, documentation is terrible, but actual testing confirms it does nothing) - [[QTerminal]] (not tested, probably unsupported) - [[st]] (tested, "unknown str") - [[Microsoft Terminal]] ([open issue](https://github.com/microsoft/terminal/issues/6779)) - [[xterm.js]] ([open issue](https://github.com/xtermjs/xterm.js/issues/3367)) - [[Foot Term]] ([open issue](https://codeberg.org/dnkl/foot/issues/1472)) - [[Wezterm]] ([open issue](https://github.com/wez/wezterm/issues/4181)) ### Setting Font Just replace the `?` with the name of the font. ```sh echo -e "\e]50;FONT_NAME\e\\" ``` This is part of the [[ECMA-48]] standard. ## Contour In addition to the standard OSC Control Code above, [[Contour Terminal]] also supports an "[enhanced](http://contour-terminal.org/vt-extensions/font-settings/)" version at `OSC 60` which can output more information about fallback and and different fonts in use for eg italics etc. ```sh echo -e "\e]60\e\\" ``` Example output[^1]: ``` \e]60;1200;Fira Code;monospace Bold;monospace Italic;monospace Italic;emoji\e\\ ``` ## Wezterm The font for a given character can be queried, but it also prints a bunch of extraneous info that will need to be removed to get to the actual font name. ```sh wezterm ls-fonts --text "x" | ...? ``` ## Gnome Terminal It is possible to query [[Gnome Terminal]]'s configuration using [[dconf]]. ```sh dconf read "/org/gnome/terminal/legacy/profiles:/$(dconf list '/org/gnome/terminal/legacy/profiles:/' | head -n 1)font" ``` This may not reflect live changes and it only pulls from the first profile. To specify the correct profile takes some workarounds where the terminal title is set to the default (which should be the name of the profile) and the title is then captured and used to find the profile. I may spend time later figuring this out. ## URXVT URXVT supports the [[#OSC Control Code]], but has two quirks: - Documents `BEL` in place of `ST`, but both work - The font name must be the full [[XLFD]] name including prefix - `xft:Fira Code:style=Regular` - Retrieve the name with style from `fc-list | grep "Fira Code"` ## Kitty Term Using Kitty's [[Python]] scripting it is possible to extract fonts. ```sh kitty +runpy 'from kitty.cli import *; print(create_default_opts().font_family)' ``` ## XTerm From a different section of the XTerm documentation, it says this: ``` One or more fonts can be specified, separated by commas. If prefixed with "x:" or "x11:" the specification applies to the XLFD font resource. A "xft:" prefix is accepted but unnecessary since a missing prefix for faceName means that it will be used for TrueType. ``` This appears to be a direct response to RXVT. # Footnotes [^1]: Note that the escape characters in the example output are not visible on the commandline but would be to a consuming application. # References - FastFetch detects some terminal font settings: https://github.com/fastfetch-cli/fastfetch/blob/f73c3b85c2056f28a0eae854d3b68f46a56a0334/src/detection/terminalfont/terminalfont_linux.c#L11 - https://espterm.github.io/docs/espterm-xterm.html - https://github.com/mintty/mintty/wiki/Tips - https://unix.stackexchange.com/questions/96962/how-can-i-find-the-default-font-resource-xterm-is-using - https://iterm2.com/documentation-escape-codes.html ## Open Issues - https://github.com/wez/wezterm/issues/4181 - https://github.com/neovim/neovim/issues/24829 ## Gnome Term - https://askubuntu.com/questions/527412/is-there-a-way-to-check-which-font-your-shell-terminal-emulator-is-using-from-th - https://askubuntu.com/questions/362633/using-installed-fonts-in-gnome-terminal - https://superuser.com/questions/198046/access-the-name-of-the-gnome-terminal-profile-from-the-command-line - https://gitlab.gnome.org/GNOME/gnome-terminal/-/issues/7673#submitted-by-christian-persch-chpe - https://askubuntu.com/questions/325235/command-line-to-switch-between-profiles-in-gnome-terminal/1274050#1274050 ## URXVT - https://addy-dclxvi.github.io/post/configuring-urxvt/ - https://en.wikipedia.org/wiki/X_logical_font_description