Toggle Background_Opacity Option in Alacritty

Toggle Background_Opacity Option in Alacritty

I want to bind key that switchs background_opacity value between 0.9 and 1.0 and immedialty shows the result in Alactitty terminal. There is no action in key_bindings for this purpose.

I have live_config_reload enabled, so one possible approach is write zsh function, that will change alacritty.yml file.

1 Answer

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Elena Rostova
Author

Elena Rostova

Elena Rostova holds a Master's degree in Public Health Journalism. She covers groundbreaking medical research, holistic wellness trends, mental health awareness, and nutritional science.