Toggle background_opacity option in alacritty

Solution 1:

I happened upon this question while looking for the same feature and ended up implementing it myself. I have included a demo of the working in my blog.

For completeness, I will include the steps here.

~/bin/toggle_alacritty_opacity
#!/usr/bin/env bash

## If alacritty.yml does not exist, raise an alert
[[ ! -f ~/.config/alacritty/alacritty.yml ]] && \
    notify-send "alacritty.yml does not exist" && exit 0

## Fetch background_opacity from alacritty.yml
opacity=$(awk '$1 == "background_opacity:" {print $2; exit}' \
    ~/.config/alacritty/alacritty.yml)

## Assign toggle opacity value
case $opacity in
  1)
    toggle_opacity=0.9
    ;;
  *)
    toggle_opacity=1
    ;;
esac

## Replace opacity value in alacritty.yml
sed -i -- "s/background_opacity: $opacity/background_opacity: $toggle_opacity/" \
    ~/.config/alacritty/alacritty.yml

Make the above script executable by running in the terminal:
chmod +x ~/bin/toggle_alacritty_opacity

Ensure that you have the following line in ~/.zshrc or ~/.bashrc

## In order to use the executable scripts inside ~/bin directly
export PATH=$HOME/bin:$PATH

Now running toggle_alacritty_opacity from your terminal will toggle alacritty's opacity.

Bonus

If you're using i3 WM, append the following lines to ~/.config/i3/config

## Toggling alacritty opacity in i3 (inside ~/bin)
bindsym $mod+Shift+a exec --no-startup-id toggle_alacritty_opacity

Now you have a key binding to toggle alacritty's opacity.