This gives each cmux terminal its own persistent zsh or Bash history file. The key is CMUX_SURFACE_ID, which identifies the terminal surface. Do not key this by CMUX_WORKSPACE_ID: a workspace can contain multiple terminals, and workspace identity is not the right granularity for command history.
Copy this prompt into your coding agent:
I use cmux on macOS and want the Up/Down arrows in each terminal to show only commands from that terminal. The history must survive cmux quit/reopen and a normal computer restart.
First inspect the current cmux environment contract. Use
CMUX_SURFACE_IDas the per-terminal key. Do not useCMUX_WORKSPACE_IDbecause it identifies a workspace, not an individual terminal. Do not useCMUX_TERMINAL_LIFECYCLE_IDbecause it changes whenever the shell process is recreated.Build a user-level shell configuration for both zsh and Bash. When
CMUX_SURFACE_IDis present, select a history file under~/.local/state/cmux/history/<shell>/<surface-id>. Sanitize the ID before using it as a filename, create the directory with mode 700, and leave normal non-cmux shell history unchanged.For zsh, enable
INC_APPEND_HISTORYandAPPEND_HISTORY, and disableSHARE_HISTORYso commands from other terminals are not imported. For Bash, sethistappend, clear any history loaded from the global file, reload the per-surface file, and append throughPROMPT_COMMANDwithout destroying an existing prompt command. Put the block after shell plugins so they cannot overwrite it.Test the implementation with isolated temporary HOME directories and at least two fake surface IDs. Prove that the files are separate, that a second shell with the same ID reads the first shell's commands, and that a shell without
CMUX_SURFACE_IDkeeps its normal history behavior. Explain the remaining limitation that a hard crash can prevent cmux from restoring a terminal snapshot even if the shell history file was written.
Download the shell file for the shell you use, then source it at the end of your startup file:
# ~/.zshrc
source "$HOME/.config/cmux/cmux-history.zsh"# ~/.bashrc, or ~/.bash_profile on a login-only Bash setup
source "$HOME/.config/cmux/cmux-history.bash"The snippets are guarded by CMUX_SURFACE_ID, so shells outside cmux keep their existing history file.
cmux stores the terminal surface UUID in its session snapshot and injects the same CMUX_SURFACE_ID when it recreates that terminal after a normal quit/reopen or computer restart. The history files are ordinary files on disk. Duplicating a live terminal gets a different identity and therefore a separate history file, which avoids merging two active shells.