To me it looks like nobody knows what is talking about...
How does our opening procedure works:
- open a terminal (whichever)
- open a
tmux
- open
vim/nvim
1 Terminal
Open a terminal (whichever you use - I use st) and write a command:
┌───┐
│ $ │ ziga > ziga--workstation > ~
└─┬─┘
└─> echo $TERM
st-256color
As answer I got a string st-256color because this string is compiled as the C string literal termname in the st (check here). It is similar with whichever terminal you use. Somewhere in the binary there is a string literal that is set as a enviromental variable TERM when terminal is started.
Here you can't do anything but to remember this string.
2 Tmux
Now lets continue the chain... tmux is opened after your terminal. In configuration file ~/.tmux.conf I must now first make sure to identify enviromental variable TERM that my terminal set. So I write like this:
set-option -sa terminal-overrides ",st*:Tc"
This line checks if enviromental variable $TERM starts with st (therefore st*) and makes sure to use true color (Tc) because current "indexed colors" (8 bits = 2^8 = 256 colors) is not enough for us. We want tmux to use "true colors" (24 bits = 2^24 = 16777215 colors). Here we could add some more lines just to be sure we will match the value of TERM. In my case the below line also works because it searches for 256col in the middle of TERM:
set-option -sa terminal-overrides ",*256col*:Tc"
And if I would use xfce4-terminal that sets TERM = xterm-256color I could use a line like this instead:
set-option -sa terminal-overrides ",xterm*:Tc"
So far tmux is covered. But before we continue the chain, we must make sure that tmux as well will export it's enviromental variable TERM.
In this case we can set it to whatever we want using this line inside the ~/.tmux.config:
set -g default-terminal "tmux-256color"
Now make sure to exit the tmux and kill it's server:
tmux kill-server
Then enter tmux by using command tmux and execute command:
┌───┐
│ $ │ ziga > ziga--workstation > ~
└─┬─┘
└─> echo $TERM
tmux-256color
You should get answer tmux-256color. Now we can continue to vim...
3 Vim
In our final step we just have to configure vim by adding this line inside it's configuration file ~/.vimrc:
if exists('+termguicolors') && ($TERM == "st-256color" || $TERM == "tmux-256color")
let &t_8f = "\<Esc>[38;2;%lu;%lu;%lum"
let &t_8b = "\<Esc>[48;2;%lu;%lu;%lum"
set termguicolors
endif
This properly sets escape sequences &t_8f and &t_8b and finally sets "true colors" inside vim. But this is done only if TERM value is:
If you are using multiple terminals, here you should add them like this:
if exists('+termguicolors') && ($TERM == "st-256color" || $TERM == "tmux-256color" || $TERM == "xterm-256color")
let &t_8f = "\<Esc>[38;2;%lu;%lu;%lum"
let &t_8b = "\<Esc>[48;2;%lu;%lu;%lum"
set termguicolors
endif
Otherwise colors will not work across all the terminals... Good luck!