Skip to content

Instantly share code, notes, and snippets.

Last active August 4, 2023 17:46
Show Gist options
  • Save algal/9fc1d9a1b3f35f84e94937eef90887c7 to your computer and use it in GitHub Desktop.
Save algal/9fc1d9a1b3f35f84e94937eef90887c7 to your computer and use it in GitHub Desktop.
Setting up truecolor (24 bit color) in emacs in the terminal, under iTerm2,, and others.
# sourcing this file will define a bash functions that
# tries to run subsequent calls to emacs with 24 bit color.
# It sets TERM=xterm-emacs-leg if
# - we've created a user-local terminfo record for xterm-emacs-leg, and
# - we're using iTerm2 or something has set COLORTERM=truecolor
# This will cause emacs to use 24 bit color only when it will work,
# inside or outside of tmux. I haven't found a way to auto-detect yet.
# To setup an xterm-emacs-leg record, run the other script: install-terminfo-xterm-24bit.bash
# To check how many colors emacs thinks it can display, evaluate (tty-display-color-cells)
# details:
emacs () {
if test -f "$HOME/.terminfo/x/xterm-emacs-leg" && ( test "$LC_TERMINAL" == "iTerm2" || test "$COLORTERM" == "truecolor" )
TERM=xterm-emacs-leg command emacs "$@"
command emacs "$@"
# To get 24 bit color in emacs, you need to set TERM to a terminal
# type that provides 24 bit color.
# For that, you probably don't need this script! If you're running
# emacs 27.1 or later, and if your system already has the terminal
# type `xterm-direct` installed (which you can check with `toe -a`),
# then you can get 24 bit color by setting the environment
# variable `TERM=xterm-direct` before running emacs. This works with
# almoost all terminal emulators I have tested.
# So when do you want one of these custom terminal type definitions?
# You might want one if your terminal emulator does not work properly
# with `xterm-direct` (like, if you are running an older
# version of emacs (26.1 or later), if you are on a system where
# `xterm-direct` is not already defined, or if you want a slightly
# more accurate 24 bit color where the first few color codes are treated
# as normal 24 bit colors (certain dark blues) rather than
# reserved as configurable palette colors.
# Then, running this script adds two terminal types which provide support
# for emacs to use 24 bit color.
# Both of these terminal types start from the xterm256-color terminal
# type and add support for terminal operations which are used only by
# emacs. They differ only in the format of the color code used:
# # xterm-emacs
# This uses the ISO 8613-6 format (e.g., "38:2::R:G:B"), which is
# recommended by emacs, and which works almost everywhere. But alas it
# does not work, and presumably not in other terminal
# emulators based on hterm.
# xterm-emacs-leg
# This uses the legacy semicolon format for color escape codes (e.g.,
# "38;2;R;G;B"). In my testing, it works with all terminal
# emulators. This is also the format currently emitted by tmux
# and mosh. I would use this.
cd $(mktemp -d)
cat <<EOF > terminfo-24bit.src
xterm-emacs|xterm with 24-bit color for Emacs (ISO8613-6 format),
xterm-emacs-leg|xterm with 24-bit color for Emacs (legacy format),
tic -x -o ~/.terminfo terminfo-24bit.src
rm terminfo-24bit.src
Copy link

@algal Thank you very much! Looking for this for days. 🥳

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment