Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
How to have change the tab color in iTerm2 based on what folder or directory you are in
#!/usr/bin/env python
Set terminal tab / decoration color by the server name.
Get a random colour which matches the server name and use it for the tab colour:
the benefit is that each server gets a distinct color which you do not need
to configure beforehand.
import socket
import random
import colorsys
import sys
import os
__copyright__ = "Copyright 2012 Mikko Ohtamaa -"
__author__ = "Mikko Ohtamaa <>"
__licence__ = "WTFPL"
__credits__ = ["Antti Haapala"]
USAGE = """
Colorize terminal tab based on the current PWD.
Usage: [0-1.0] [0-1.0] # Lightness and saturation values
An iTerm 2 example (recolorize dark grey background and black text): 0.7 0.4
def get_random_by_string(s):
Get always the same 0...1 random number based on an arbitrary string
# Initialize random gen by hash of string s
return random.random()
def decorate_terminal(color):
Set terminal tab / decoration color.
Please note that iTerm 2 / Konsole have different control codes over this.
Note sure what other terminals support this behavior.
:param color: tuple of (r, g, b)
r, g, b = color
# iTerm 2
sys.stdout.write("\033]6;1;bg;red;brightness;%d\a" % int(r * 255))
sys.stdout.write("\033]6;1;bg;green;brightness;%d\a" % int(g * 255))
sys.stdout.write("\033]6;1;bg;blue;brightness;%d\a" % int(b * 255))
# Konsole
def rainbow_unicorn(lightness, saturation):
Colorize terminal tab by your server name.
Create a color in HSL space where lightness and saturation is locked, tune only hue by the server.
name = os.getcwd()
hue = get_random_by_string(name)
color = colorsys.hls_to_rgb(hue, lightness, saturation)
def main():
From Toholampi with love
if(len(sys.argv) < 3):
lightness = float(sys.argv[1])
saturation = float(sys.argv[2])
rainbow_unicorn(lightness, saturation)
if __name__ == "__main__":
# Change tab color based on pwd.
function tab_color_precmd {
~/.change-tab-color-pwd 0.5 0.5
autoload -U add-zsh-hook
add-zsh-hook precmd tab_color_precmd
Copy link

jamie commented Mar 3, 2021

Love this.

These days chpwd (runs on dir change) is prob a better hook choice than precmd (runs before each prompt).

Copy link

notsure commented Apr 5, 2022

just found and love it!

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