How to dynamically change terminal background color (Ghostty) - kdaisho/Blog GitHub Wiki
How to dynamically change terminal background color (Ghostty)
You can change the background color of individual Ghostty tabs/windows on the fly using an OSC 11 escape sequence. This is useful for visually distinguishing between terminal sessions (e.g., dev vs. staging vs. production).
Setup
Add the following aliases to your .zshrc (or .bashrc):
alias bg0='printf "\033]11;#000000\007"' # default/black
alias bg1='printf "\033]11;#330000\007"' # dark red
alias bg2='printf "\033]11;#002200\007"' # dark green
alias bg3='printf "\033]11;#000033\007"' # dark blue
alias bg4='printf "\033]11;#332200\007"' # dark orange
alias bg5='printf "\033]11;#220033\007"' # dark purple
alias bg6='printf "\033]11;#003333\007"' # dark teal
alias bg7='printf "\033]11;#331100\007"' # dark brown
alias bg8='printf "\033]11;#002233\007"' # dark cyan
alias bg9='printf "\033]11;#330022\007"' # dark magenta
alias bg10='printf "\033]11;#222200\007"' # dark olive
Then reload your shell:
source ~/.zshrc
Usage
Simply type the alias in a terminal tab:
bg3 # changes the current tab's background to dark blue
bg0 # resets back to black
Each tab maintains its own background color independently.
How it works
\033]11;#RRGGBB\007 is an OSC (Operating System Command) escape sequence:
\033]11;— set the background color#RRGGBB— the hex color value\007— the terminator (BEL character)
Ghostty supports this sequence natively. No configuration changes to Ghostty itself are needed.
Customization
Replace any hex value with your preferred color. Use dark/muted colors to keep text readable.