Skip to content

Instantly share code, notes, and snippets.

@xqm32
Last active May 7, 2024 10:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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

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