Skip to content

Instantly share code, notes, and snippets.

@andersevenrud
Last active November 13, 2024 05:31
Show Gist options
  • Save andersevenrud/015e61af2fd264371032763d4ed965b6 to your computer and use it in GitHub Desktop.
Save andersevenrud/015e61af2fd264371032763d4ed965b6 to your computer and use it in GitHub Desktop.
True Color (24-bit) and italics with alacritty + tmux + vim (neovim)

True Color (24-bit) and italics with alacritty + tmux + vim (neovim)

This should make True Color (24-bit) and italics work in your tmux session and vim/neovim when using Alacritty (and should be compatible with any other terminal emulator, including Kitty).

Testing colors

Running this script should look the same in tmux as without.

curl -s https://gist.githubusercontent.com/lifepillar/09a44b8cf0f9397465614e622979107f/raw/24-bit-color.sh >24-bit-color.sh
bash 24-bit-color.sh

colors

Configuration files

⚠️ IMPORTANT ⚠️ Don't set $TERM in your shell (zshrc, bashrc, etc.), but your terminal (alacritty).

Alacritty

In ~/.config/alacritty/alacritty.yml:

env:
  TERM: xterm-256color

tmux

In ~/.tmux.conf (or ~/.config/tmux/tmux.conf):

set -g default-terminal "tmux-256color"
set -ag terminal-overrides ",xterm-256color:RGB"

# Or use a wildcard instead of forcing a default mode.
# Some users in the comments of this gist have reported that this work better.
#set -sg terminal-overrides ",*:RGB"

# You can also use the env variable set from the terminal.
# Useful if you share your configuration betweeen systems with a varying value.
#set -ag terminal-overrides ",$TERM:RGB"

vim

In ~/.vimrc:

" You might have to force true color when using regular vim inside tmux as the
" colorscheme can appear to be grayscale with "termguicolors" option enabled.
if !has('gui_running') && &term =~ '^\%(screen\|tmux\)'
  let &t_8f = "\<Esc>[38;2;%lu;%lu;%lum"
  let &t_8b = "\<Esc>[48;2;%lu;%lu;%lum"
endif

syntax on
set termguicolors
colorscheme yourfavcolorscheme

neovim

If you use ~/.vimrc for your nvim configuration, use the above vim example because it's fully compatible.

💡 neovim recently (Dec 6th 2023) merged some changes that detects termguicolors automatically, so there's no need to set this in an upcoming release. I'll update this notice with a version once released as a public version. This came with the 0.10 release (May 16th 2024).

In ~/.config/nvim/init.vim

set termguicolors
colorscheme yourfavcolorscheme

Or ~/.config/nvim/init.lua:

vim.o.termguicolors = true
vim.cmd'colorscheme yourfavcolorscheme'

Not working correctly ?

Check out the comments below for possible solutions.

And if you found another solution I would really appreciate if you left a comment with instructions and the following information:

  • OS/Distro + version
  • Vim or Neovim + version
  • Terminal name + version

Mentions

Shout-out to the nice folks that provided insightful feedback:

@n0099
Copy link

n0099 commented Feb 29, 2024

echo 'set -as terminal-overrides ",'"$(echo ${TERM%%-*})"'*:Tc"' >> ~/.tmux.conf

https://github.com/tmux/tmux/wiki/FAQ#how-do-i-use-rgb-colour

tmux must be told that the terminal outside supports RGB colour. This is done by specifying the RGB or Tc terminfo(5) flags. RGB is the official flag, Tc is a tmux extension.

With tmux 3.2 and later this can be added with the terminal-features option:

set -as terminal-features ",gnome*:RGB"

Or for any tmux version the terminal-overrides option:

set -as terminal-overrides ",gnome*:Tc"

and replace gnome with the result of echo ${TERM%%-*} (the first part of $TERM delimited by -) from the terminal emulator that you are currently using outside tmux such as putty or xterm.

@ferBV
Copy link

ferBV commented Mar 24, 2024

Just what I needed, thank you a lot.

@RobertMiguel
Copy link

My configuration:

alacritty 0.13.2
NVIM v0.9.5

.tmux.conf

# Fix Colors
set -g default-terminal "screen-256color"
set -ag terminal-overrides ",xterm-256color:RGB

alacritty.toml

[env]
TERM="xterm-256color"

config neovim

vim.opt.termguicolors = true

@zachliu
Copy link

zachliu commented Jun 3, 2024

My configuration:

zsh 5.9
tmux 3.4
alacritty 0.13.2
NVIM v0.10.0-dev-3135+g7acf39dda

.tmux.conf

set -g default-terminal "tmux-256color"
set -sg terminal-overrides ",*:RGB"

.config/nvim/init.vim

if $COLORTERM ==# 'truecolor'
  set termguicolors
else
  set guicursor=
endif

nothing special on alacritty.toml and .zshrc
i didn't set env TERM explicitly. but i have

$ env | rg TERM
COLORTERM=truecolor
TERM=tmux-256color
TERM_PROGRAM=tmux
TERM_PROGRAM_VERSION=3.4

@rdyson
Copy link

rdyson commented Jun 8, 2024

Thanks for this!

@dffuller
Copy link

@LeFede This worked for me with Arch/Alacritty/tmux

alacritty.yaml

env:
  TERM: alacritty-direct

tmux.conf

set-option -a terminal-overrides ",alacritty:RGB"

default-terminal was not needed in the tmux.conf

This worked for me. Thanks so much for adding this information!

@rogerneel
Copy link

On a Wayland / Hyprland setup

My .tmux.conf only has the line:

set -g default-terminal "screen-256color"

I added to alacritty.toml, which seemed to be the key:

[colors]
transparent_background_colors = true
[window]
opacity = 0.8

Immediately both neovim and tmux started showing transparency. I did not have to explicitly set vim.opt.termguicolors = true in my neovim conf.

@delatorrejuanchi
Copy link

I've only added the following to alacritty.toml (i.e.: using a default tmux config) and it seems to work as expected.

[env]
TERM = "alacritty-direct"

Undercurls, italics, bold, strikethrough, etc. also work.

@arash28134
Copy link

Worked out of the box on Arch! Thanks!

@ElanMedoff
Copy link

Heads up that it's not enough to source your tmux config after making changes to it (i.e. with tmux source /path/to/tmux.conf), I needed to kill the session and start a new one for the true colors to take effect

@s3rgeym
Copy link

s3rgeym commented Oct 24, 2024

~/.tmux.conf

# Включение поддержки true color
set -g default-terminal "xterm-256color"
set -ga terminal-overrides ',xterm-256color:Tc'

# Включение поддержки мыши
set -g mouse on

~/.config/alacritty/alacritty.toml

[terminal.shell]
program = "tmux"
args = []

[env]
# Без этого в tmux true color не работает
TERM = "alacritty-direct"

~/.config/vim/vimrc

" Используем 20-битные цвета
set termguicolors

image

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