Instantly share code, notes, and snippets.

Embed
What would you like to do?
Change iTerm2 tab color when using SSH
# Usage:
# source iterm2.zsh
# iTerm2 window/tab color commands
# Requires iTerm2 >= Build 1.0.0.20110804
# http://code.google.com/p/iterm2/wiki/ProprietaryEscapeCodes
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-reset() {
echo -ne "\033]6;1;bg;*;default\a"
}
# Change the color of the tab when using SSH
# reset the color after the connection closes
color-ssh() {
if [[ -n "$ITERM_SESSION_ID" ]]; then
trap "tab-reset" INT EXIT
if [[ "$*" =~ "production|ec2-.*compute-1" ]]; then
tab-color 255 0 0
else
tab-color 0 255 0
fi
fi
ssh $*
}
compdef _ssh color-ssh=ssh
alias ssh=color-ssh
@herrtreas

This comment has been minimized.

herrtreas commented Sep 11, 2013

Thanks!

@imazine

This comment has been minimized.

imazine commented Mar 29, 2014

It's so fantastic !!

@ghost

This comment has been minimized.

ghost commented Apr 6, 2014

thx!

@rkJun

This comment has been minimized.

rkJun commented Jul 13, 2014

Thanks! good.

@dstibrany

This comment has been minimized.

dstibrany commented Jul 27, 2014

This is amazing. Thanks for that!

@orourkek

This comment has been minimized.

orourkek commented Sep 18, 2014

Awesome man, thanks!

@zeitos

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

apjanke commented Jul 28, 2015

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

@wadey

This comment has been minimized.

Owner

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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

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

This comment has been minimized.

sarinkuruvilla commented Jan 9, 2017

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

@brouberol

This comment has been minimized.

brouberol commented Oct 3, 2017

This is awesome, thanks!

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