Skip to content

Instantly share code, notes, and snippets.

@wadey
Last active November 3, 2024 16:25
Show Gist options
  • Save wadey/1140259 to your computer and use it in GitHub Desktop.
Save wadey/1140259 to your computer and use it in GitHub Desktop.
Change iTerm2 tab color when using SSH
# Usage:
# source iterm2.zsh
# iTerm2 tab color commands
# https://iterm2.com/documentation-escape-codes.html
if [[ -n "$ITERM_SESSION_ID" ]]; then
tab-color() {
echo -ne "\033]6;1;bg;red;brightness;$1\a"
echo -ne "\033]6;1;bg;green;brightness;$2\a"
echo -ne "\033]6;1;bg;blue;brightness;$3\a"
}
tab-red() { tab-color 255 0 0 }
tab-green() { tab-color 0 255 0 }
tab-blue() { tab-color 0 0 255 }
tab-reset() { echo -ne "\033]6;1;bg;*;default\a" }
function iterm2_tab_precmd() {
tab-reset
}
function iterm2_tab_preexec() {
if [[ "$1" =~ "^ssh " ]]; then
if [[ "$1" =~ "prod" ]]; then
tab-color 255 160 160
else
tab-color 160 255 160
fi
else
tab-color 160 160 255
fi
}
autoload -U add-zsh-hook
add-zsh-hook precmd iterm2_tab_precmd
add-zsh-hook preexec iterm2_tab_preexec
fi
@herrtreas
Copy link

Thanks!

@imazine
Copy link

imazine commented Mar 29, 2014

It's so fantastic !!

Copy link

ghost commented Apr 6, 2014

thx!

@rkJun
Copy link

rkJun commented Jul 13, 2014

Thanks! good.

@dstibrany
Copy link

This is amazing. Thanks for that!

@orourkek
Copy link

Awesome man, thanks!

@zeitos
Copy link

zeitos commented May 7, 2015

This is great, but does this break Ctrl+c for you, meaning: it cancels executions but if you want to cancel something you're typing then that doesn't work

@bpiwowar
Copy link

bpiwowar commented Jun 8, 2015

@zeitos I have the same problem - and I cannot use the ctrl+c key afterwards

Update: solved it with

tab_reset()
{
    echo -ne "\033]6;1;bg;*;default\a"
    trap - INT EXIT
}

@apjanke
Copy link

apjanke commented Jul 28, 2015

Updated URL for the iTerm2 proprietary escape code documentation: https://iterm2.com/documentation-escape-codes.html

@wadey
Copy link
Author

wadey commented Oct 20, 2015

Wow, glad to see so many people are finding this and finding it useful.

BTW, I am now using a slightly different implementation of this that doesn't require aliasing the ssh command (uses pre / post command hooks instead). Check it out here: https://github.com/wadey/dotfiles/blob/master/zsh/iterm2.zsh

@Glutnix
Copy link

Glutnix commented Dec 1, 2015

@wadey I'm working on making your dotfile above into a configurable Prezto package. With your blessings, I will post a pull-request on sorin-ionescu/prezto under an MIT licence. Let me know.

@hamzilla
Copy link

hamzilla commented Jan 4, 2016

@wadey thanks for the non-ssh method. Works great. One question, would it be possible to split the tab while maintaining two separate colors? Workflow: Open new tab -> split tab -> ssh into two separate hosts -> flip between two hosts and the tab color changes.

@Solisol
Copy link

Solisol commented Jul 1, 2016

@Glutnix How did it go with the configurable Prezto package? Would like to try it out if it became real :)

@sarinkuruvilla
Copy link

sarinkuruvilla commented Jan 9, 2017

@wadey Where do i put this file to use it?

@brouberol
Copy link

This is awesome, thanks!

@ahirag
Copy link

ahirag commented Oct 14, 2019

Thanks!

@wadey
Copy link
Author

wadey commented Oct 14, 2019

I updated this gist with the better version, using pre and post hooks!

@penn201500
Copy link

hi,
i'm a newbie, just try iterm2 few days. After source iterm2.zsh , how to use it ? do u have an example or README for newbie?
tks!

@urbontaitis
Copy link

hi,
i'm a newbie, just try iterm2 few days. After source iterm2.zsh , how to use it ? do u have an example or README for newbie?
tks!

At least how I'm using it, I have added it under plugin directory: $ZSH_CUSTOM/plugins/iterm2/iterm2.plugin.zsh and in .zshrc I have set ZSH_CUSTOM=~/example/zsh-custom and added to the plugins=(... iterm2)

@penn201500
Copy link

hi,
i'm a newbie, just try iterm2 few days. After source iterm2.zsh , how to use it ? do u have an example or README for newbie?
tks!

At least how I'm using it, I have added it under plugin directory: $ZSH_CUSTOM/plugins/iterm2/iterm2.plugin.zsh and in .zshrc I have set ZSH_CUSTOM=~/example/zsh-custom and added to the plugins=(... iterm2)

nice!! it's working now, tks!!

@jonathan-s
Copy link

@wadey do you know if there is a way to change the background as well?

@avatar-lavventura
Copy link

Is it possible to only color the title bar or use darker color on the tab bar location

@wadey
Copy link
Author

wadey commented Jul 29, 2020

The possibilities are documented here: https://www.iterm2.com/documentation-escape-codes.html

@avatar-lavventura
Copy link

avatar-lavventura commented Jul 30, 2020

Its like tab bar's color is brighter of the given color and not same color as the title bar. When the color is set it is assigned on the both title bar and tab bar but tab bar always makes the color much bright

For example for: tab-color 160 160 255
Screen Shot 2020-07-30 at 12 56 20

@lacostenycoder
Copy link

How do I use this?

@mitsh
Copy link

mitsh commented Oct 10, 2023

thanks for sharing this! nice job. If you put some comments to explain some lines, that would be great.

@andremacola
Copy link

Thank you for the reference. I created a fork with an update to customize the color by server and added the option to customize the background.

https://gist.github.com/andremacola/12337455f5dc43a3aa348a5aebcacbcf

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