Skip to content

Instantly share code, notes, and snippets.

@xqm32
Last active July 24, 2024 15:09
Show Gist options
  • Save xqm32/17777d035930d622d0ff7530bfab61fd to your computer and use it in GitHub Desktop.
Save xqm32/17777d035930d622d0ff7530bfab61fd to your computer and use it in GitHub Desktop.
Automatically switch color theme for Alacritty on macOS

Original issue comment: alacritty/alacritty#5999 (comment)

Run the following command to clone themes:

mkdir -p ~/.config/alacritty/themes
git clone https://github.com/alacritty/alacritty-theme ~/.config/alacritty/themes

Add the following content to ~/.config/alacritty/alacritty.toml:

import = ["~/.config/alacritty/active.toml"]

[env]
ALACRITTY = "true"

Add the following content to ~/.zshrc:

if [ "$ALACRITTY" = "true" ]
then
  theme() {
    ln -sf $HOME/.config/alacritty/themes/themes/$1.toml $HOME/.config/alacritty/active.toml
  }
  local ALACRITTY_THEME=$(defaults read -g AppleInterfaceStyle 2>/dev/null || echo "Light")
  if [ "$ALACRITTY_THEME" = "Dark" ]
  then
    theme "github_dark"
  else
    theme "github_light"
  fi
fi

Alacritty will then change the theme every time it starts.

Simple version

if [ "$ALACRITTY" = "true" ]
then
  local ALA_HOME=$HOME/.config/alacritty
  local ALA_THEME=$(defaults read -g AppleInterfaceStyle 2>/dev/null || echo "Light" | tr '[:upper:]' '[:lower:]')
  ln -sf $ALA_HOME/themes/themes/github_$ALA_THEME.toml $ALA_HOME/active.toml
fi

Simplified version with theme function

if [ "$ALACRITTY" = "true" ]
then
  local ALA_HOME=$HOME/.config/alacritty
  local ALA_THEME=$(defaults read -g AppleInterfaceStyle 2>/dev/null || echo "Light" | tr '[:upper:]' '[:lower:]')
  theme() { ln -sf $ALA_HOME/themes/themes/$1.toml $ALA_HOME/active.toml; }
  theme github_$ALA_THEME
fi

Thanks

@brecke
Copy link

brecke commented May 29, 2024

In fish shell it needs to be slightly different:

if test "$ALACRITTY" = "true"
    function theme
        ln -sf $HOME/.config/alacritty/themes/themes/{$argv[1]}.toml $HOME/.config/alacritty/active.toml
    end

    set -l ALACRITTY_THEME (defaults read -g AppleInterfaceStyle 2>/dev/null; or echo "Light")
    
    if test "$ALACRITTY_THEME" = "Dark"
        theme "github_dark"
    else
        theme "github_light"
    end
end

Thanks for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment