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

1

7 Answers 7

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!)

5
  • 1
    I write an ssh wrapper script in my ~/bin which makes the tab color changes (and other things like custom background with server names) and uses an EXIT trap to change them back.
    – Aaron
    Jun 27, 2013 at 18:12
  • Aaron, would you mind sharing your script? Feb 26, 2015 at 22:19
  • You can also do that: function ssh { command ssh $@; # RESET BACK -> don't know how yet! help needed here }
    – davidhq
    Mar 17, 2015 at 14:01
  • Somehow I found out... I pasted it as another answer
    – davidhq
    Mar 17, 2015 at 14:18
  • What should we do to change active tab's color?
    – alper
    Jan 29, 2022 at 11:23
20

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"
1
  • Wow this is useful (but not intuitive). Mar 28, 2022 at 21:31
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"
}
1
  • This changes it (back?) to some grey value which may or may not match the theme you're using. More useful is this: echo -e "\033]6;1;bg;*;default\a" found in Proprietary Escape Codes
    – Tom Hundt
    Sep 12, 2022 at 17:27
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

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    May 31, 2022 at 11:49
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

0

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
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Sep 7, 2022 at 7:19
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .