24

My daily workflow includes me

  1. Launching iTerm2
  2. Creating 3 tabs
  3. Setting one tab each to red, orange and yellow
  4. Changing to a specific path in each tab

I would like to script this process; shell, applescript, etc. However, I cannot seem to find a hook that allows me to change the tab color. Is this possible? Here is a screen shot with an example of what I am trying to achieve.

iTerm tab setup

John Kramlich
  • 365
  • 1
  • 3
  • 6

7 Answers7

21

I added this function to my ~/.profile file:

function color {
    case $1 in
    green)
    echo -e "\033]6;1;bg;red;brightness;57\a"
    echo -e "\033]6;1;bg;green;brightness;197\a"
    echo -e "\033]6;1;bg;blue;brightness;77\a"
    ;;
    red)
    echo -e "\033]6;1;bg;red;brightness;270\a"
    echo -e "\033]6;1;bg;green;brightness;60\a"
    echo -e "\033]6;1;bg;blue;brightness;83\a"
    ;;
    orange)
    echo -e "\033]6;1;bg;red;brightness;227\a"
    echo -e "\033]6;1;bg;green;brightness;143\a"
    echo -e "\033]6;1;bg;blue;brightness;10\a"
    ;;
    esac
 }

After adding this function you have to open a new terminal session. Now you can enter:

$ color green

or

$ color orange

to change the Tab color.

I use Photoshop to compose colors:

Photoshop color picker

This color picker values can be converted to the following commands (Just insert the R -> red, G -> green, B -> blue values into the right line after "brightness;" to get a different color):

echo -e "\033]6;1;bg;red;brightness;57\a"
echo -e "\033]6;1;bg;green;brightness;197\a"
echo -e "\033]6;1;bg;blue;brightness;77\a"
18

That's possible and you should read iterm escape codes for details.

^[]6;1;bg;red;brightness;N^G

I tried to setup the color of the terminal when I do ssh (.ssh/config) and it worked but surprise, when I close the ssh session, it will not call the script again, in order to restore the title/color.

Added a feature request to auto-colored tabs - do not forget to star it, or add your comments (patches are also welcome!)

Alan Dong
  • 193
sorin
  • 12,189
4

To reset the tab color after exiting the ssh session use:

function ssh {
  command ssh $@
  echo -e "\033]6;1;bg;red;brightness;176\a"
  echo -e "\033]6;1;bg;green;brightness;181\a"
  echo -e "\033]6;1;bg;blue;brightness;175\a"
}
davidhq
  • 255
2

Add this to your .bashrc/.zshrc/.whateverrc file to get a random tab color every time you open a new tab in iTerm2:

function tabcolor {
  echo -n -e "\033]6;1;bg;red;brightness;$1\a"
  echo -n -e "\033]6;1;bg;green;brightness;$2\a"
  echo -n -e "\033]6;1;bg;blue;brightness;$3\a"
}

tabcolor $(jot -r 1 0 255) $(jot -r 1 0 255) $(jot -r 1 0 255)

each time you open a new terminal it calls the tab color method which calculates a new color and sets it to tab.

1

I have found this plugin that allows you to set colors programatically. (you need ohmyzsh)

https://github.com/bernardop/iterm-tab-color-oh-my-zsh

simPod
  • 222
0

Update the best answer, to support random color automaticly when using color without argument

add the following lines to ~/.profile or ~/.zshrc:

PRELINE="\r\033[A"

function random { echo -e "\033]6;1;bg;red;brightness;$((1 + $RANDOM % 255))\a"$PRELINE echo -e "\033]6;1;bg;green;brightness;$((1 + $RANDOM % 255))\a"$PRELINE echo -e "\033]6;1;bg;blue;brightness;$((1 + $RANDOM % 255))\a"$PRELINE }

function color { case $1 in green) echo -e "\033]6;1;bg;red;brightness;57\a"$PRELINE echo -e "\033]6;1;bg;green;brightness;197\a"$PRELINE echo -e "\033]6;1;bg;blue;brightness;77\a"$PRELINE ;; red) echo -e "\033]6;1;bg;red;brightness;270\a"$PRELINE echo -e "\033]6;1;bg;green;brightness;60\a"$PRELINE echo -e "\033]6;1;bg;blue;brightness;83\a"$PRELINE ;; orange) echo -e "\033]6;1;bg;red;brightness;227\a"$PRELINE echo -e "\033]6;1;bg;green;brightness;143\a"$PRELINE echo -e "\033]6;1;bg;blue;brightness;10\a"$PRELINE ;; *) random esac }

#uncomment to enable automatically set random color when tab created #color

After each time a new iterm2 tab created, use command color to automaticly give it a new/random color.

if you want the iterm2-tab color set automatically whenever it is created, then .just add color to then end of .zshrc / .profile or just after the function color

Simon Huang
  • 101
  • 2
0

Add the below function in .bash_profile , it will automatically set random color to tab

setcolor() {
  x=0
  rand=$(( ( RANDOM % 148 )  + 1 ))
  for i in aliceblue antiquewhite aqua aquamarine azure beige bisque black blanchedalmond blue blueviolet brown burlywood cadetblue chartreuse chocolate coral cornflowerblue cornsilk crimson cyan darkblue darkcyan darkgoldenrod darkgray darkgreen darkgrey darkkhaki darkmagenta darkolivegreen darkorange darkorchid darkred darksalmon darkseagreen darkslateblue darkslategray darkslategrey darkturquoise darkviolet deeppink deepskyblue default dimgray dimgrey dodgerblue firebrick floralwhite forestgreen fuchsia gainsboro ghostwhite gold goldenrod gray green greenyellow grey honeydew hotpink indianred indigo ivory khaki lavender lavenderblush lawngreen lemonchiffon lightblue lightcoral lightcyan lightgoldenrodyellow lightgray lightgreen lightgrey lightpink lightsalmon lightseagreen lightskyblue lightslategray lightslategrey lightsteelblue lightyellow lime limegreen linen magenta maroon mediumaquamarine mediumblue mediumorchid mediumpurple mediumseagreen mediumslateblue mediumspringgreen mediumturquoise mediumvioletred midnightblue mintcream mistyrose moccasin navajowhite navy oldlace olive olivedrab orange orangered orchid palegoldenrod palegreen paleturquoise palevioletred papayawhip peachpuff peru pink plum powderblue purple rebeccapurple red rosybrown royalblue saddlebrown salmon sandybrown seagreen seashell sienna silver skyblue slateblue slategray slategrey snow springgreen steelblue tan teal thistle tomato turquoise violet wheat white whitesmoke yellow
  do
    if [ $rand -eq $x ]
    then
     tabset --color $i
    fi
    x=$(($x+1))
  done
  unset x
}
setcolor
Markus Meyer
  • 1,910