If you do a lot of context switching between projects, recreating your terminal environment & window layout can easily eat up hours of your day. Here’s a quick tip to help you create and manage persistent terminal workspaces so that with a few keystrokes, you can jump straight back into whatever you were working on as quickly as possible.

How it works

The whole idea behind this setup is to make it so you will have 1) The ability to name your terminal workspaces with easily memorizable names and 2) the ability to keep persistent terminal workspaces running even if you’ve closed your terminal window.

All of this is achieved with the following shell script which we’ll configure iTerm2 to execute anytime you open a new window.

export PATH=$PATH:/usr/local/bin

# abort if we're already inside a TMUX session
[ "$TMUX" == "" ] || exit 0 

# startup a "default" session if none currently exists
tmux has-session -t _default || tmux new-session -s _default -d

# present menu for user to choose which workspace to open
PS3="Please choose your session: "
options=($(tmux list-sessions -F "#S") "NEW SESSION" "BASH")
echo "Available sessions"
echo "------------------"
echo " "
select opt in "${options[@]}"
    case $opt in
        "NEW SESSION")
            read -p "Enter new session name: " SESSION_NAME
            tmux new -s "$SESSION_NAME"
            bash --login
            tmux attach-session -t $opt 
Setting it up

First off, place the above script in a location that’s accessible to iTerm2 (I usually place it in ~/.dotfiles/tmux.start.sh).

Then open up iTerm2’s terminal preferences and have it execute this script anytime you open a new window:



Once you’ve done all the above, everytime you open a new window, you’ll be prompted to choose which previous workspace you want to join.


You’ll also have the opportunity to create new work spaces by choosing NEW SESSION. Or if you don’t want to open a full blown tmux session, you can choose to just open up a BASH prompt. All of these sessions are persistent. So if you decide to close your terminal window, they will remain active in the background and ready for you to rejoin at a later point in time.