~/blog /til-one-escape-sequence-makes-my-terminal-follow-my-ai-agent-into-a-worktree$ ca sv es
David Poblador i Garcia David Poblador i Garcia

Senior Technologist, Advisor

Founder at All Tuner Labs

Jun 30, 2026

TIL: One escape sequence makes my terminal follow my AI agent into a worktree

How OSC 7 makes my terminal follow my coding agent

The thing that made me grin today: I tell Claude Code to “work in a git worktree,” the agent jumps into a fresh checkout, and my Ghostty terminal follows it — new tabs and splits open right inside the worktree, not where I launched. The agent moves; the terminal moves with it. Feels like magic, and it’s one escape sequence.

A bit about OSC

Escape sequences aren’t just colors. OSCOperating System Command — is the family that sends side-channel messages to your terminal: window title, clipboard, and the one here, the current directory (OSC 7):

ESC ] 7 ; file://HOST/path ESC \

Your shell fires it on every cd. I wire a Claude Code hook to fire it whenever the agent changes directory too. That’s the whole trick.

The Gotcha (The Actual TIL)

Ghostty silently drops OSC 7 when the HOST doesn’t match the machine’sgethostname() — logging OSC 7 host must be local — and freezes the directory at its last good value. My hook sent the short name; macOS wants the full one:

- file://lemon/...        # hostname -s   ✗ rejected
+ file://lemon.local/...  # hostname      ✓ accepted

One word. The hook had silently never worked.

Bonus Trap

You can’t test it at a prompt — your shell re-emits OSC 7 the instant the command returns and clobbers your test. Hold the prompt hostage:

printf '\033]7;file://%s/tmp/\033\\' "$(hostname)"; sleep 15
# open a new tab within 15s → it lands in /tmp

This was built while debugging my dotfiles hooks for Claude Code + Ghostty.