
The thing that made me grin today: I tell [Claude Code](https://claude.com/claude-code) to *“work in a **[git worktree](https://code.claude.com/docs/en/worktrees)**,”* the agent jumps into a fresh checkout, and my [Ghostty](https://ghostty.org/) 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. **OSC** — *Operating 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](https://en.wikipedia.org/wiki/ANSI_escape_code#OSC_(Operating_System_Command)_sequences)):

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

Your shell fires it on every `cd`. I wire a [Claude Code hook](https://github.com/davidpoblador/dotfiles/blob/main/private_dot_claude/hooks/executable_cwd-changed-osc7.sh) 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 ](https://github.com/ghostty-org/ghostty/discussions/8514)`HOST`[ doesn’t match the machine’s](https://github.com/ghostty-org/ghostty/discussions/8514)`gethostname()` — 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](https://github.com/davidpoblador/dotfiles) hooks for [Claude Code](https://claude.com/claude-code) + [Ghostty](https://ghostty.org/).

