Switching solarized colors with mintty and tmux

colorssolarizedtmux

I am trying to find a way to easily toggle between light/dark solarized themes without needing to create a new terminal/tmux session. I am running mintty on a Windows machine and usually log into a Linux machine and connect to a tmux session.

Using mavnn's solarized mintty colors and seebi's solarized tmux colors, I have written some bash functions that can change the terminal colors on the fly. This is in my .bashrc:

function godark()
{
    ~/solarized/sol.dark
    tmux source-file ~/tmux/tmuxcolors-dark.conf
}

function golight()
{
    ~/solarized/sol.light
    tmux source-file ~/tmux/tmuxcolors-light.conf
}

So inside of sol.dark there are instructions such as:

echo -ne '\eP\e]10;#839496\a' # Foreground -> base0
echo -ne '\eP\e]11;#002B36\a' # Background -> base03

and inside of my tmuxcolors-dark.conf I'll have things such as:

set-option -g status-bg colour235 #base02
set-option -g status-fg colour130 #yellow

This is almost working. If I do not have tmux open I can type "godark" and mintty will change to a dark theme, but if I type this in tmux, it will change my tmux status bar to the correct theme, but my terminal background does not change color. I do not really understand ANSI escape sequences so maybe I'm doing something silly here. I'd appreciate any help in getting this working!

Best Answer

You can send an escape sequence from inside tmux to the containing terminal by transforming your escape sequence like this:

  1. double all occurrences of \e
  2. prepend \ePtmux;
  3. append \e\\

For example, using st as my terminal emulator, I can redefine color #1 (red) of its palette by executing

printf '\e]4;1;#aa0000\a'

To do the same from within tmux, I have to use

printf '\ePtmux;\e\e]4;1;#aa0000\a\e\\'

When using a shell script/function to switch colors, check for the $TMUX environment variable. If it is not empty, then you are inside tmux.

Related Question