Create a profiles directory inside of .zsh

Inside of your user’s directory find the .zsh directory, cd into it and create a directory called profiles.

This profiles directory is where we will put our different config files for zsh.

Create an .zshenv file

Create a .zshenv file inside of your user’s directory (/Users/~username) if you don’t already have one.

Add a ZSH_DIR path pointing to the ~/.zsh directory you created earlier.

Add the ZSH_PROFILE which will be a variable pointing the ITERM_PROFILE but has a default value of default. This variable is set by iTerm itself based on the profile you are using.

Add a conditional block that will check if the ZSH_PROFILE for a given file path ending in .env.zsh exists before doing a source on it. The code will look like the following:

[[ -f "${ZSH_DIR}/profiles/${ZSH_PROFILE}.env.zsh" ]] && source "${ZSH_DIR}/profiles/${ZSH_PROFILE}.env.zsh"

This is what our .zshenv file look like now:

# ZSH home directory
export ZSH_DIR=~/.zsh

# default to, well, "default"
export ZSH_PROFILE=${${ITERM_PROFILE:=default}:l}

[[ -f "${ZSH_DIR}/profiles/${ZSH_PROFILE}.env.zsh" ]] && source "${ZSH_DIR}/profiles/${ZSH_PROFILE}.env.zsh"

Modify existing .zshrc

We will now modify our ~/.zshrc file to load other files based on the profile we are using in iTerm.

Because we have already set up some variables inside of our ~/.zshenv, these variables can be used in our ~/.zshrc, this is because our ~/.zshenv gets loaded before ~/.zshrc is loaded. We will add a conditional block that will check if the ZSH_PROFILE exists before doing a source on it as we did for the env files under the profiles directory.

[[ -f "${ZSH_DIR}/profiles/${ZSH_PROFILE}.rc.zsh" ]] &&
    source "${ZSH_DIR}/profiles/${ZSH_PROFILE}.rc.zsh"

Create separate .env.zsh files based on iTerm profile

We will create a .env.zsh file for the default config inside of our ~/.zsh/profiles directory.

First cd into the directory

cd ~/.zsh/profiles

Next, we can create our new default env file in the terminal using:

touch default.env.zsh

Inside this file, we will store all of our environment variables specific to this profile.

Create separate .rc.zsh files based on iTerm profile

Let’s repeat the same steps as we did for the .env.zsh file.

This is where we can modify existing settings or add new ones for this iTerm prompt. In my case I am adding a plugin to the plugins list for ohmyzsh.

In my main .zshrc file I have the following list of plugins

plugins=(git brew docker npm z)

In my newly created profile file, I will update the list by adding the zsh-autosuggestions plugin.

plugins=(git brew docker zsh-autosuggestions npm z)