Claude Code can't run /compact on itself, so this does it from outside. A rule in CLAUDE.md has Claude run
compact-self "<what to keep>" as the last thing it does when it finishes a task. The script waits in the background
until the turn is over, then types /compact <what to keep> into Claude's own tmux pane and presses Enter.
Needs tmux.
- Save
compact-selfas~/.claude/bin/compact-selfand make it executable (chmod +x). - Merge
settings.jsoninto~/.claude/settings.json. The Stop hook records when a turn ends; the allow rule lets Claude run the script without asking. - Add the rule in
rule-for-CLAUDE.mdto~/.claude/CLAUDE.md.
It won't type into a pane you've scrolled back in. It doesn't check the input line, so if you start typing in that pane just as the turn ends, the two get mixed.