Last active
December 7, 2025 20:26
-
-
Save zAbuQasem/b4ab4575c3ef3fea247a9caab7a37f5d to your computer and use it in GitHub Desktop.
Per-terminal Zsh history: use a separate HISTFILE for each terminal app (VS Code, iTerm, etc.) while keeping a common history for Apple Terminal and Ghostty on Linux/WSL.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| set_app_history() { | |
| local app=${TERM_PROGRAM:-Apple_Terminal} | |
| local default_histfile="$HOME/.zsh_history" | |
| # Terminals that should use the shared/default history file | |
| local -a SHARED_HISTORY_TERMINALS=( | |
| Apple_Terminal | |
| iTerm.app | |
| Ghostty | |
| ) | |
| local use_shared=false | |
| # If running inside tmux, always share history | |
| if [[ -n "$TMUX" ]] || [[ "$app" == "tmux" ]]; then | |
| use_shared=true | |
| else | |
| # Check if $app is in SHARED_HISTORY_TERMINALS | |
| local t | |
| for t in "${SHARED_HISTORY_TERMINALS[@]}"; do | |
| if [[ "$app" == "$t" ]]; then | |
| use_shared=true | |
| break | |
| fi | |
| done | |
| fi | |
| if [[ "$use_shared" == true ]]; then | |
| HISTFILE="$default_histfile" | |
| else | |
| # Everything else → per-app history | |
| local safe_app=${app//[^A-Za-z0-9]/_} | |
| HISTFILE="$HOME/.zsh_history_${safe_app}" | |
| fi | |
| export HISTFILE | |
| } | |
| set_app_history |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment