Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iTerm 2 profiles

Tags:

macos

iterm

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?

like image 768
Miles McCrocklin Avatar asked Dec 22 '11 00:12

Miles McCrocklin


People also ask

What are iTerm profiles?

A profile enables you to open a new command line window in a certain directory and run commands automatically. Let's make one! First, close all open command line windows in iTerm, and start with a new command line window. Next, you'll want to navigate to “Profiles” in the Menubar and click “Open Profiles…”

Where are iTerm2 profiles?

iTerm stores profiles in `~/Library/Saved Application State` which is not backed up by time machine.

How do I open multiple windows in iTerm2?

iTerm2 allows you to divide a tab into many rectangular "panes", each of which is a different terminal session. The shortcuts cmd-d and cmd-shift-d divide an existing session vertically or horizontally, respectively. You can navigate among split panes with cmd-opt-arrow or cmd-[ and cmd-].

How do I save my iTerm2 profile?

Go to iTerm2 -> Preferences -> General . Under the Preferences tab , Tick Load Preferences from a custom folder or a URL: and choose a folder and click on Save Now .


2 Answers

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.

like image 138
George Avatar answered Oct 07 '22 13:10

George


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

like image 40
lfender6445 Avatar answered Oct 07 '22 11:10

lfender6445