Title: Change Between Light and Dark Themes in tmux
Tags: tmux
How to toggle between light-background and dark-background themes in tmux.
tmux's `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:
```console
$ # 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:
```sh
#!/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:
```console
$ ~/.tmux/bin/toggle-theme
```
You can bind a keyboard shortcut to toggle the theme. For example add this to
your `~/.tmux.conf` file:
```
bind T run-shell ~/.tmux/bin/toggle-theme
```
Reload the file:
```console
$ 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 `pane-border-style` and `pane-active-border-style`
(the colours of the pane borders) between light and dark mode. See [the full version of the script](https://github.com/seanh/tmux/blob/master/bin/toggle-theme)
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 `~/.tmux.conf`):
```
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 `-p`
to change the colours of the current pane only:
```console
$ # 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 | #171421 |
Palette entry 1 | #C01C28 |
Palette entry 2 | #26A269 |
Palette entry 3 | #A2734C |
Palette entry 4 | #12488B |
Palette entry 5 | #A347BA |
Palette entry 6 | #2AA1B3 |
Palette entry 7 | #D0CFCC |
Palette entry 8 | #5E5C64 |
Palette entry 9 | #F66151 |
Palette entry 10 | #33DA7A |
Palette entry 11 | #E9AD0C |
Palette entry 12 | #2A7BDE |
Palette entry 13 | #C061CB |
Palette entry 14 | #33C7DE |
Palette entry 15 | #FFFFFF |
Cursor and highlight background | #D0CFCC |
Cursor and highlight foreground | #171421 |
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
.