Configure iTerm2 to run commands automatically when a new pane opens by using the "Run Command" feature in Profiles. Set the command to launch your shell, such as zsh, and enable the "Restore Sessions" option if you need persistent state. This method ensures that specific initialization scripts or environment variables are applied consistently across every new terminal pane.
Accessing Profile Settings
The foundation of this automation lies within the iTerm2 configuration interface. To begin, open iTerm2 and navigate to the settings menu. On macOS, this is typically found under iTerm2 > Settings (or Preferences in older versions). Once the settings window is open, locate the Profiles section in the left-hand sidebar. You will see a list of existing profiles, with the default profile usually named "Default." Select the profile you wish to modify. If you want all new panes to behave this way, ensure you are editing the active profile. The settings here apply globally to any session spawned from this specific profile. It is crucial to understand that changes made here do not affect windows created via AppleScript unless that script explicitly specifies a different profile. For most users, modifying the default profile is the safest approach to ensure consistency.Configuring the Run Command
Within the selected profile, click on the General tab. Scroll down until you find the Command section. By default, this is often set to "Login Shell," which allows the system to load standard login configurations. To run custom commands, change this dropdown to Custom Command. In the text field that appears, enter the path to your shell. For example, if you are using zsh, enter:/bin/zsh
This tells iTerm2 to explicitly invoke zsh instead of relying on the system's default shell interpretation. This step is vital because it guarantees that your .zshrc file is sourced correctly, regardless of how the parent process was started. Without this explicit path, some environments might spawn a different shell variant, leading to inconsistent behavior.
Handling Initialization Scripts
Simply launching zsh is often not enough if you have complex initialization logic. You may need to source specific files or run setup scripts before the prompt appears. To achieve this, append your commands to the custom command string. For instance, if you have a setup script at~/setup.sh, you can use:
/bin/zsh -c "source ~/setup.sh && exec /bin/zsh"
The -c flag passes the subsequent string as a command to the shell. The source command executes your setup script, loading any aliases, functions, or environment variables defined there. The && operator ensures that the final exec /bin/zsh only runs if the source command succeeds. This effectively replaces the current process with a fresh zsh instance that has your desired environment loaded.
For more complex scenarios involving multiple steps, consider creating a wrapper script. Place all your logic in a single executable file, such as ~/iterm-init.sh, and point the Custom Command to that file. This keeps your profile settings clean and makes debugging easier since you can test the script independently in a regular terminal.
Restoring Session State
A common issue with running custom commands is losing the ability to resume previous sessions. If you close iTerm2 and reopen it, you might expect your previous panes to reappear. To support this, go to the Sessions tab in the same profile settings. Ensure that Restore last session is checked. When combined with the custom command, this feature saves the state of your panes. Upon restart, iTerm2 will recreate the panes and execute the custom command for each one. This means your initialization scripts run again, ensuring that your environment is fresh but consistent. Note that if your initialization scripts rely on dynamic data (like current time or network status), they will update appropriately upon each restore.Testing and Verification
After applying these changes, it is essential to verify the behavior. Close all existing iTerm2 windows and relaunch the application. Check if the initialization script ran as expected. You can add a simple echo statement to your script, such asecho "Pane initialized", to confirm execution. If the message does not appear, check the logs in the General tab for error messages.
Additionally, test creating a new pane manually using the shortcut Cmd+D. The new pane should also trigger the custom command. If it does not, ensure that the shortcut is associated with the correct profile. Sometimes, user-specific overrides in key bindings can bypass profile settings, so verifying the key binding configuration is a good troubleshooting step.
Frequently Asked Questions
Will this override my .zshrc file?
No, it does not override it. When you launch zsh via the custom command, it still sources your standard .zshrc file located in your home directory. The custom command simply adds an extra layer of initialization before the interactive shell starts. You can keep your usual configuration intact while adding pane-specific setups.Can I use different commands for different profiles?
Yes, absolutely. Each profile in iTerm2 has its own independent settings. You can create a "Development" profile with specific tooling commands and a "Production" profile with minimal logging. When you open a new pane, iTerm2 uses the profile associated with that window or pane, allowing for distinct behaviors based on your workflow needs.Why is my script not executing on startup?
This often happens if the path to the script is incorrect or if the script lacks execute permissions. Ensure the script is in a location accessible to your user and has the +x permission set. Also, verify that the Custom Command path in iTerm2 matches the exact location of the script. Using absolute paths is recommended to avoid ambiguity.How do I debug issues with the custom command?
Redirect output to a log file for easier inspection. Modify your custom command to include redirection, such as/bin/zsh -c "~/setup.sh > ~/iterm-log.txt 2>&1". This captures both standard output and errors into a text file in your home directory. Reviewing this log after launching iTerm2 will reveal any syntax errors or missing dependencies in your initialization script.