How to toggle between light-background and dark-background themes in tmux.
window-style setting changes the default foreground and background colours for windows, and you can use it to toggle your terminal between light-on-dark
and dark-on-light themes:
$ # Change the current window (all panes) to light background. $ tmux set window-style 'fg=#171421,bg=#ffffff' $ # Change back to dark background. $ tmux set window-style 'fg=#d0cfcc,bg=#171421'
Here’s a shell script to toggle between light and dark:
#!/usr/bin/env sh # # Toggle the current window (all panes) between light and dark themes. set -e default_window_style='fg=#d0cfcc,bg=#171421' alternate_window_style='fg=#171421,bg=#ffffff' current_window_style=$(tmux show -Av window-style) case $current_window_style in $default_window_style|'default') # Change to the alternate window style. tmux set window-style $alternate_window_style ;; *) # Change back to the default window style. tmux set window-style $default_window_style ;; esac
Save this script as
~/.tmux/bin/toggle-theme and mark it executable
chmod u+x ~/.tmux/bin/toggle-theme) and you can toggle between dark and
light mode with a single command:
You can bind a keyboard shortcut to toggle the theme. For example add this to
bind T run-shell ~/.tmux/bin/toggle-theme
Reload the file:
$ tmux source ~/.tmux.conf
Now Ctrl + b Shift + t toggles between light and dark mode.
You might want to add more lines to the script to change other options like
(the colours of the pane borders) between light and dark mode. See the full version of the script
in my tmux config for an example.
The script changes the default foreground and background colours of the current window (all panes).
You’ll want your status line colours to be ones that work well against either a dark
or a light background, since the same session might contain both light and dark windows
at the same time. I’m using these (in my
set -g status-style 'fg=#d0cfcc,bg=#171421' set -g window-status-current-style 'bg=default,reverse'
You can also use
-g to change the colours of all windows across all sessions or
to change the colours of the current pane only:
$ # Change the colours of all windows across all sessions. $ tmux set -g window-style 'fg=#171421,bg=#ffffff' $ # Change the colours of the current pane only. $ tmux set -p window-style 'fg=#171421,bg=#ffffff'
There’s also a
window-active-style setting that you can use to highlight the active pane by giving it a different background colour than the other panes.
This all requires your terminal emulator to have a colour palette that works on either a light or a dark background. I’m using the default colour palette from GNOME Terminal (as of GNOME 3.38):
|Palette entry 0|
|Palette entry 1|
|Palette entry 2|
|Palette entry 3|
|Palette entry 4|
|Palette entry 5|
|Palette entry 6|
|Palette entry 7|
|Palette entry 8|
|Palette entry 9|
|Palette entry 10|
|Palette entry 11|
|Palette entry 12|
|Palette entry 13|
|Palette entry 14|
|Palette entry 15|
|Cursor and highlight background|
|Cursor and highlight foreground|
As you can see from the script my dark mode background and foreground colours are
#D0CFCC on #171421
and light mode is
#171421 on #FFFFFF.