Switch Alacritty’s Color Scheme With a Keyboard Shortcut

Updated: 2 minute read

Background

I use Alacritty [1] as my terminal emulator. Alacritty uses a configuration file called alacritty.toml (it used to be alacritty.yml). This file can be used to set the color theme. I wanted to be able to cycle through my favorite color themes with a keyboard shortcut. Having to edit the configuration file manually was just to cumbersome.

Solution

The color theme for Alacritty can either be directly defined in alacritty.toml or in a separate color theme file that is than imported into the alacritty.toml file. Separate color theme files is a great solution if you want to change color theme easily. Such a color theme file looks like this (this example is my own dark color theme):

# Default colors
[colors.primary]
background = "#000000"
foreground = "#aaaaaa"

# Normal colors
[colors.normal]
black   = "#262626"
blue    = "#006cd8"
cyan    = "#00a1a1"
green   = "#00a100"
magenta = "#ff00ff"
red     = "#cc0000"
white   = "#aaaaaa"
yellow  = "#a6a600"

# Bright colors
[colors.bright]
black   = "#626262"
blue    = "#0080ff"
cyan    = "#00eaea"
green   = "#00ff00"
magenta = "#ff55ff"
red     = "#ff4400"
white   = "#dedede"
yellow  = "#ffff00"

To be able to cycle through different color themes, I set up the directory: $XDG_CONFIG_HOME/alacritty/themes/ and placed a sub-directory into it, called my_themes. In case I ever decide to use further color themes created by someone else, I will place them into different sub-directories. Directly in $XDG_CONFIG_HOME/alacritty/themes/ I created a symbolic link to all the color themes that I want to be able to cycle through. So the contents of $XDG_CONFIG_HOME/alacritty/themes/ looks like this:

/home/andreas/.config/alacritty/themes/
├── schuam_theme_1.toml -> my_themes/schuam_theme_1.toml
├── schuam_theme_3.toml -> my_themes/schuam_theme_3.toml
├── schuam_theme_4.toml -> my_themes/schuam_theme_4.toml
└── my_themes
    ├── schuam_theme_1.toml
    ├── schuam_theme_2.toml
    ├── schuam_theme_3.toml
    └── schuam_theme_4.toml

The alacritty.toml file contains the following lines:

import = [
    "/home/andreas/.config/alacritty/themes/schuam_theme_1.toml",    # COLOR_THEME
]

The # COLOR_THEME comment is important for the script that I will describe next.

Then I wrote a bash script (set_color_theme), that does the following:

  • It gets a sorted list of files and links that are in directly in $XDG_CONFIG_HOME/alacritty/themes/ (sub-directories are ignored).
  • It looks for the line containing # COLOR_THEME in $XDG_CONFIG_HOME/alacritty/alacritty.toml.
  • It checks if the currently configured color theme is in the list from the first bullet point.
    • If so, it replaces the path of the currently configured color theme with the path of the next color theme in the list.
    • If not, it places the path of the first color theme from the list into the color theme configuration line.

Afterwards I made the script executable and placed it in a directory that is part of my $PATH. I also added the following lines to my alacritty.toml file:

[[keyboard.bindings]]
key = "F"
mods = "Control|Shift"

[keyboard.bindings.command]
program = "set_color_theme"

Well actually alacritty migrate placed the lines in the configuration file. I used alacritty migrate to migrate my previous alacritty.yml file to alacritty.toml. Alacritty changed the configuration file format from YAML to TOML in version 0.13.0.

Now I’m able to switch the color scheme in Alacritty with a keyboard shortcut. If you’re interested in my solution, checkout my dotfiles on github. You can find my color theme, the alacritty.toml file, and the set_color_theme script there.

Change Log

2024-01-02:

  • Re-wrote the whole post. First of all I did this, because Alacritty switched from the YAML to TOML file format for the configuration file. But also because I didn’t like the old post any more.



Take care,
Andreas


References

  1. alacritty, “A cross-platform, GPU-accelerated terminal emulator.” [Online]. Available at: https://github.com/alacritty/alacritty. [Accessed: 28-Aug-2020].

Updated:

Leave a comment