# Make Claude Code compact itself: a CLAUDE.md rule plus a script that types /compact into its own tmux pane once the turn ends

> Source: <https://gist.github.com/27medkamal/820124876cf750967c14b1ebe55c95f8>
> Published: 2026-11-03 10:00:39+00:00

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.

1. Save `compact-self` as`~/.claude/bin/compact-self` and make it executable (`chmod +x` ).
2. Merge `settings.json` into`~/.claude/settings.json` . The Stop hook records when a turn ends; the allow rule lets
Claude run the script without asking.
3. Add the rule in `rule-for-CLAUDE.md` to`~/.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.
