---
title: "Per-App Shell History for Zsh"
description: "Keep your Zsh history organized across different terminal applications. This snippet automatically creates separate history files for each terminal emulator you use."
author: "Mohammad Abu Mattar"
canonical: https://mkabumattar.com/codesnippets/post/zsh-per-app-history
---

# Per-App Shell History for Zsh

**Terminal Chaos? Organize Your Shell History!**

Ever jumped between iTerm2, Ghostty, and VS Code's terminal only to have your command history get all mixed up? This Zsh snippet keeps things clean by creating separate history files for each terminal app you use.

## The Problem

### Mixed Command History

When you use multiple terminal emulators (iTerm, Alacritty, Ghostty, VS Code, etc.), they all share the same shell history file by default. This means commands you ran in one app show up when you press the up arrow in another. It gets messy fast, especially when you use different terminals for different projects or contexts.

### Context Switching Issues

Different terminals used for different purposes (work, personal, testing) end up with mixed command histories, making it difficult to maintain context.

## The Solution

### Per-App History Files

This Zsh function detects which terminal application you're using and automatically assigns a dedicated history file for it. Apple Terminal and Ghostty on Linux get the default history, while everything else gets its own isolated file. Clean, simple, and automatic.

### Automatic Organization

**TL;DR**

- Automatically creates separate history files for each terminal app.
- Keeps Apple Terminal and Ghostty (Linux) on the default history.
- Works seamlessly with iTerm2, Alacritty, Warp, Kitty, and more.
- Just add it to your `~/.zshrc` and forget about it.

## Implementation

### Zsh Function Setup

```shell title="~/.zshrc" showLineNumbers
function set_app_history() {

  # Get terminal app name (fallback: Apple_Terminal)
  local app=${TERM_PROGRAM:-Apple_Terminal}

  # Default history file
  HISTFILE="$HOME/.zsh/.zhistory"

  # Ghostty on Linux / WSL → use default history
  if [[ "$app" == "Ghostty" && "$(uname -s)" == "Linux" ]]; then

    return
  fi

  # Apple Terminal → use default history
  if [[ "$app" == "Apple_Terminal" ]]; then

    return
  fi

  # Everything else → per-app history
  app=${app//[^A-Za-z0-9]/_}
  HISTFILE="$HOME/.zsh/.zhistory_${app}"

}

set_app_history
```

## Benefits and Usage

### Organization Advantages

**Why This Helps**

This approach keeps your shell history organized without any manual intervention. You can switch between terminals without worrying about command pollution or losing context. Plus, it's especially useful if you use different terminals for work, personal projects, or testing each gets its own clean slate.

### Managing History Files

**Bonus Tip**

Want to see which history files you have? Run this:

```shell
ls -lh ~/.zsh/.zhistory*
```

You'll see all your per-app history files. Each one is tied to a specific terminal emulator, so you can even back them up or clean them individually.

## Community Discussion

### Your History Management

**Your Turn**

How do you organize your shell history? Do you use per-app separation or something else? Drop your setup below!

### Alternative Approaches

Share your favorite methods for keeping shell history organized across different terminal applications.
