48

I have recently switched over to iTerm2 and love it. I am wondering though if there is a way to use profiles to correspond to what environment/specific machine you are on.

Say if I am doing tasks in one window on my mac the profile is displayed as default, but if I ssh into a machine (lets say dev0), the profile on iTerm will update to profile dev0. Once I've finished with dev0 and call exit, the profile will switch to default again.

I realize one work around is to open up a specific profile whenever I want to ssh into another machine and have a way to distinguish, but if the connection is closed it requires you to notice based off text rather than say the background of the window.

Is this possible? If not how can this feature be added, and is there a way I can contribute?

3
  • It is a good question, but I am not really sure if this belongs on Stackoverflow.
    – sosborn
    Dec 22, 2011 at 0:23
  • you're right I just joined the community, is there a way to move this to SuperUser? Dec 23, 2011 at 20:38
  • 3
    There is at least one question on SuperUser that references this question in the comments :) superuser.com/questions/1052305/… Mar 13, 2016 at 17:12

7 Answers 7

59

iTerm2 supports a custom escape code that changes the profile on the fly. Put it in your .bashrc or .bash_profile.

<esc>]50;SetProfile=X^G

where X is the profile. For instance, to change the profile to one called "Foo", us this shell script:

#!/bin/bash
echo -e "\033]50;SetProfile=Foo\a"

To change it back when you log out, put code to change the profile back to default in ~/.bash_logout.

9
  • Just an issue with this for anyone else who reads this: if you end up nesting your ssh sessions, you will have to re-source (. ~/.bashrc) after logging out of the second ssh into your first. I normally wouldn't do this but have to if I am working remotely. Jan 4, 2012 at 22:56
  • Is it possible to set default profile like this? I tried SetDefaultProfile, but that didn't work.
    – sent-hil
    Jan 29, 2013 at 2:24
  • 1
    Just found this list of escape codes, interestingly - SetProfile isn't listed.. I wonder what else is hidden away in there.. iterm2.com/#/section/documentation/escape_codes
    – Tim Kane
    May 17, 2013 at 16:15
  • 2
    Just adding to this, I found the following snippet useful for changing profile based on whether I'm ssh'd to a host or not: sshToMyFavouriteHost() { echo -e "\033]50;SetProfile=Profile2\a"; ssh my.favourite.host.com && echo -e "\033]50;SetProfile=Profile1\a" }
    – scubbo
    Feb 13, 2014 at 12:33
  • Apparently SetProfile=\a gives me the default profile back.
    – Nobu
    Mar 1, 2014 at 1:54
16

for zsh users

lets say you have 2 profiles, one named mac (for your primary machine) and one for linux (your remote machine)

when entering the session, we need to tell zsh to load our profile

  1. connect to remote linux
  2. in ~/.zshrc add echo -e "\033]50;SetProfile=linux\a"
  3. source your files for immediate effect: source ~/.zshrc
  4. your new theme should be visible within the iterm session.

when exiting the session, we need to tell zsh to switch back to our original profile

  1. connect to remote linux
  2. in linux ~/.zlogout add the following
if [ "$SHLVL" = 1 ]; then
  echo -e "\033]50;SetProfile=mac\a"
  clear
fi

now you can swap profiles with ease <3.

if you are using bash, i believe the steps are similar but you would instead modify ~/.bashrc and ~/.bash_logout

demo

demo of session based profiles in iterm2

3
  • 1
    I gave this a thumbs-up specifically for the ~/.bash_logout information. Mar 15, 2018 at 23:14
  • Hello, does this still work with version 3.4.12 of iTerm? I can't manage to get it to work although I know it worked before. I can't figure out when it stopped working.
    – eexit
    Nov 3, 2021 at 16:20
  • what shells are you using both local and remote? echo $0 Nov 3, 2021 at 16:38
14

The latest iTerm2 nightly (Build 2.9.20150329-nightly at the time of writing) allows you to do that easily. You can download it here.

Once you've installed and opened it:

  1. Log in to your remote machine via ssh and click iTerm2 (the app menu) > Install Shell Integration. It will download a script with curl and install it. Do the same on your local machine.
  2. Go to Preferences > Profiles.
  3. Create a new profile for your local machine. Customize it to fit your needs (change background color, name, etc)
  4. Go to the Advanced tab and scroll to the bottom.
  5. In Automatic Profile Switching, click '+' and add the hostname of your local machine. The hostname is the one you get when running echo $HOST on the target machine. It is not always the one you see in your prompt.
  6. Create another profile, this time for your remote machine, and customize it.
  7. Add the hostname of the remote machine in Automatic Profile Switching.
  8. Now, if you ssh into the remote machine, your profile will change, and if you exit out of the ssh session, you will be back to your local profile.

You can combine this solution with @esod's answer seamlessly.
Note: it didn't work for me until I created a profile specifically for the desktop instead of using the default profile.

See the documentation for more info.

1
  • 1
    Excellent, very concise answer. As my computer is a work computer, under the ~Automatic Profile Switching~ option, I chose to use @myusername vs. the host name because echo $HOST was blank, it was a work computer, and I didn't want to edit my host name. Mar 13, 2016 at 17:53
5

step 1:

custom your iterm profile, e.g. dark, light

step 2:

add code before to your shell profile, e.g .bashrc or .zshrc

# Change iterm2 profile. Usage it2prof ProfileName (case sensitive)
it2prof() { echo -e "\033]50;SetProfile=$1\a" }

step 3:

make sense your profile

exec $SHELL -l

step 4:

toggle your iterm theme profile

it2prof dark
it2prof light
1

I had this same wish and found this can be accomplished in iTerm 2 (Build 1.0.0.20130319) in the application's preferences.

You can assign a profile (say a remote profile) with a different preset than your default preset by going to:

Profiles -> Open Profiles select the profile and click Edit Profiles... Go to the Colors Tab and choose a preset for this profile from the list in Load Presets...

Further, I've set up Keys shortcut for different profiles so I can have one iTerm window look different than another window. I did this by:

  1. Creating a new a Profile in Preferences
  2. Creating a new Profile Shortcut Key in Preferences-> Keys whose action is New Window with Profile

My Default profile has a black background but sometimes it helps me to have a white background. I duplicate my Default profile and name the new profile DefaultLight. On my Default profile I go to the Keys tab where I create a new Profile Shortcut Key whose Keyboard Shortcut is ^+cmd+n, whose action is New Window with Profile, and whose Profile is DefaultLight.

After saving the prefrences, cmd+n opens a new window with a black background and ^+cmd+n opens a new window with a white background.

There's also a New Tab with Profile action in the Keyboard Shortcut Keys Preference if you're interested in taking this even further.

0

In my case, I need to combine Yohaï Berreby's answer with my hosts' setting to implement this feature.

  1. In Automatic Profile Switching, click '+' and add the hostname of your local machine. The hostname is the one you get when running echo $HOST on the target machine. It is not always the one you see in your prompt.

My staging server doesn't set $HOST and host name is :

[devel@alveo-staging ~]$ hostname
alveo-staging

But I can't set the rule with hostname as alveo-staging. Because in fact alveo-staging is just an alias of the real hostname (alveo-staging.xxx), which can be set in /etc/hosts.

So the quick solution to this is to use * wildcards, to set the rule as *staging*.

Then it works.

0

For Fishell user: 1. Create a fish function:

functions it2prof > ~/.config/fish/functions/it2prof.fish

  1. Add this content to ~/.config/fish/functions/it2prof.fish file and save:
function it2prof
  echo -e "\033]50;SetProfile=$argv\a"
end
  1. enjoy your command:

it2prof whatever_profile_you_define

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.