i3 + termite + custom themes = <3

posted on bydotcs

I love the sun – especially in Spring when temperatures are not too hot and taking a sunbath is a real pleasure. And I love working in the sun, which is often a challenge because of the poor contrast - especially if, like me, you like to use dark themes.

Since I recently changed to i3 as my window manager, I use termite as my default terminal which has by default no simple way to switch themes – at least not to my knowledge. But working in the sun requires a light theme, so I decided to work on a simple theme changer that is also integrated in i3.

So let's get to work.

First we need custom color themes. I have taken the two themes from this GitHub repository and put them in ~/.config/termite/themes. Of course you can also define custom themes and put them there. The theme files should have the following format:

[colors]
foreground = #000000
background = #ffffff
# and so on

Then copy your current ~/.config/termite/config to ~/.config/termite/config.base and remove the colors section.

We will now need a way to append a theme file to the config.base file. I've created a simple script that does this job:

#!/usr/bin/env bash

USAGE="$0 light|dark"

TERMITE_CONF_FOLDER=~/.config/termite
THEME=$1

change_theme() {
    echo "# THIS FILE HAS BEEN AUTOGENERATED. DO NOT CHANGE MANUALLY!" | \
        cat - /tmp/termite-switch-theme.hint $TERMITE_CONF_FOLDER/config.base $TERMITE_CONF_FOLDER/themes/$1 > $TERMITE_CONF_FOLDER/config
    killall -USR1 termite || true
}

case $THEME in
    light|solarized-light) change_theme solarized-light ;;
    dark|solarized-dark) change_theme solarized-dark ;;
    *) echo $USAGE ;;
esac

Put this script somewhere and make it executable. In my case I've saved the file in ~/.local/scripts/termite-switch-theme.sh. You should be able to switch themes now by calling the script, e.g. ~/.local/scripts/termite-switch-theme.sh light.

This is already great, but for a perfect integration with i3 put these lines into your ~/.config/i3/config:

set $mode_term_theme Terminal theme (l) light, (d) dark
mode "$mode_term_theme" {
    # choose which theme should be applied by pressing l (light) or d (dark)
    bindsym l exec --no-startup-id "~/.local/scripts/termite-switch-theme.sh light", mode "default"
    bindsym d exec --no-startup-id "~/.local/scripts/termite-switch-theme.sh dark", mode "default"
    # back to normal: Enter or Escape
    bindsym Return mode "default"
    bindsym Escape mode "default"
}
bindsym $mod+t mode "$mode_term_theme"

Finally reload the i3 config with mod+shift+c. Now you can change themes with mod+t and choose which theme should be applied (light or dark). All running terminals will immediately reflect your theme changes. Have fun!

Base16 Themes

If you want to have even more themes to choose from take a look at the wonderful repository khamer/base16-termite. Those themes are working out-of-the-box just as described above.

Acknowledgements

All work licensed underunless otherwise stated.