What you'll get out of this post
- Why every serious multi-agent setup — Claude Code in one pane, Codex in another, Gemini in a third — seems to run inside tmux, and what tmux provides that made it the default harness
- The reason a dropped SSH connection (or a closed window) kills your long-running job, traced to the exact mechanism — and why tmux is the real fix, not a folk remedy
- The trick that lets one program type into another program's terminal —
tmux send-keysdriving Claude Code as if a human were at the keyboard
There's a catch to explaining tmux: every honest answer to "what does tmux actually do?" leans on three older ideas — terminal, shell, and PTY. Skip them and tmux stays a memorized incantation; build them first and tmux collapses into one precise, almost obvious move. So the build-up is the post, and tmux is the payoff.
The stack is easier to understand than it looks, because every layer of it is a fossil of a real machine from the 1960s. Start there.
A terminal used to be a physical machine
In the 1960s a computer was a mainframe: a room-sized, million-dollar machine you were not allowed to touch. You talked to it through a separate, much dumber device — a terminal.
The first terminals were teletypes: a keyboard and a printer wired to a phone line, with the mainframe on the other end. Press a key, an electrical signal travels down the wire; the mainframe answers, and the reply gets printed on paper. Later models (the VT100) swapped the paper for a CRT screen, but the shape stayed the same: keyboard, display, wire.
That's where the name comes from — the terminal is the end of a communication line. And tty, the abbreviation that still shows up everywhere in Unix, is just teletype.
Key Concept
Terminal
A terminal is an I/O endpoint: it sends your keystrokes down a line and displays the characters that come back. It runs none of your programs and makes no decisions about them. A useful mental model: the terminal is a TV set, the mainframe is the broadcast station — the TV doesn't produce shows, it renders a signal and sends your remote presses back.
The 1960s stack
fingers / eyes
↓
serial line / phone line — pure electrical signal, no software in the middle
↓
Keep this picture. Everything that follows is the story of replacing the middle of it with software while the top and bottom pretend nothing changed.
The shell: the program on the other end of the wire
The terminal moves characters. Something on the mainframe has to receive your command, understand it, run it, and send the result back. That program is the shell — bash, zsh, fish.
Strip away the features and a shell is a small loop:
# a shell, structurally
while True:
print_prompt("$ ") # "I'm ready"
line = read_line() # blocks until you press Enter
result = run(line) # parse, fork + exec, pipes, redirects
print_output(result)
The part that intuition tends to blur: the shell doesn't decide what commands you have. ls, git, python are independent programs sitting in /usr/bin; every shell can run all of them. What the shell owns is the interaction: how your input gets parsed, what Tab completion suggests, what the up-arrow recalls, how | and > wire programs together.
A concrete split — type git and press Tab:
| Terminal (Ghostty) | Shell (zsh) | |
|---|---|---|
| Sees | a Tab keypress | the byte 0x09 arriving after git |
| Does | encodes the key, sends it down the line, waits | searches for commands starting with git, computes completions |
| Then | renders whatever bytes come back | writes the suggestions back down the line |
The terminal never understood that git is a command. It moved a byte and painted the reply. The shell did all the thinking — which is why the same Ghostty window behaves completely differently when zsh's completion is configured differently.
| Terminal | Shell | |
|---|---|---|
| What it is | an I/O program (the window) | a command interpreter |
| It decides | how things look | how things work |
| Owns | fonts, colors, rendering, key encoding, scrollback | parsing, completion, history, pipes, job control |
| Examples | Ghostty, iTerm2, Terminal.app, Alacritty, Warp | bash, zsh, fish, PowerShell |
| Swappable? | freely, without touching the other | freely, without touching the other |
Two footnotes that resolve common config-folder mysteries:
- oh-my-zsh is not a shell. It's a bundle of zsh configuration — themes and plugins. zsh is the car; oh-my-zsh is the trim package. Same engine.
- Switching shells is an experience upgrade, not a capability upgrade. Bare bash has no autosuggestions or highlighting; zsh + plugins adds them; fish ships them out of the box at the cost of bash-incompatible syntax. All of them run the same
git.
Quick check
You type ls in Ghostty and press Enter. Who executes the command?
Then the wire disappeared
By the late 1980s the computer was on your desk. Keyboard and monitor plug straight in — no remote mainframe, no phone line, no terminal hardware. But Unix's entire I/O model assumed users connect through terminals. Decades of software, the shell included, was written against that interface.
The fix was not to rewrite Unix. It was to fake the terminal in software. Ghostty, Terminal.app, iTerm2 — these are terminal emulators: ordinary GUI programs that draw a window, render monospace text, and convert keystrokes into the same byte stream a teletype would have produced.
Which leaves one structural question, and it's the crux of this post: the teletype and the mainframe were connected by a real wire. The emulator and the shell are two processes on the same machine. What replaced the wire?
1960s: the middle leg is copper
the signal physically travels, then the kernel takes over at the far end
Today: the middle leg is the kernel
write() is a syscall — the data is in the kernel immediately; transmission distance: zero
Note what didn't change: the kernel was always the thing receiving terminal data on the computer side — in the 1960s its TTY driver decoded serial signals arriving over copper. All that changed is the wire itself got replaced by a kernel construct. That construct has a name.
The PTY: a fake wire with two ends
Key Concept
Pseudo-terminal (PTY)
A PTY is a pair of virtual device files the kernel creates to simulate a serial line. Whatever is written into one end can be read from the other. The master end goes to the terminal emulator; the slave end becomes the shell's stdin/stdout. To the shell, the slave end is indistinguishable from a 1970s hardware terminal — which is the entire point.
The data flow is symmetric and worth watching once. The terminal writes keystrokes into the master; the kernel ferries them to the slave, where the shell reads them as stdin. The shell writes output to stdout — the slave — and the kernel ferries it back to the master, where the terminal reads and renders it:
Click the buttons to push data through the two ends of a PTY:
Master end
Held by: the terminal emulator (Ghostty)
Job: write your keystrokes in, read the shell's output out
Slave end
Held by: the shell process (zsh / bash)
Job: read user input as stdin, write command output as stdout
Put the PTY into the full stack and the modern picture is the 1960s picture with the hardware swapped out:
The modern stack
GUI events: keystrokes in, pixels out
↓
PTY: master ↔ slave, ferried by the kernel
↓
fork + exec
↓
This isn't hidden machinery — you can ask your shell where it's plugged in:
Why "pseudo"? Because no hardware exists — and yet the shell's code didn't change by a single line between the teletype era and today. It reads stdin, writes stdout, and the abstraction holds. When a program on the slave end asks "am I talking to a terminal?" (isatty()), the kernel says yes.
When an abstraction is that airtight, programs also can't tell who is on the other end — a point that turns out to be load-bearing for everything in the tmux section.
Quick check
Who holds the master end of the PTY?
Buffers: where your keystrokes wait
One more mechanism and the foundation is complete. "Buffer" sounds abstract; in this stack it's three specific waiting rooms.
The line buffer answers a question with a surprising answer: when you've typed ls -la but haven't pressed Enter, where are those characters? Not in the shell — the shell hasn't seen anything yet. They're staged in a kernel buffer attached to the PTY:
Press the keys to stage characters in the kernel's line buffer:
Waiting for input… characters stage here until you press Enter.
This is the kernel's canonical mode (line discipline): input is delivered to the program line-by-line, and until Enter, editing keys operate on the buffer directly. It's why Backspace works identically in every CLI tool — the tool isn't handling it; the kernel is.
The screen buffer lives on the other side: the terminal emulator keeps a 2-D character grid in memory — what character and color sits at every cell. The window is just a rendering of this grid. Hold this one; it's the key to tmux's capture-pane.
The output buffer: programs don't emit output one character at a time — they accumulate it and flush in chunks (on newline, or when the buffer fills). Cheaper syscalls. It's why a program's output can appear in bursts, and why print(..., flush=True) exists.
Quick check
You type hello at a shell prompt but have not pressed Enter. Does the shell know?
tmux: move the master to a process that never quits
Now the foundation pays off, because tmux stops being magic and becomes one precise move.
First, the failure it fixes. Ghostty holds the PTY master. Close the window:
- Ghostty exits → the master end closes
- The kernel sees the "wire" go dead and sends
SIGHUP— hang up, vocabulary straight from the phone-line era — to the shell - The shell dies, taking every child process with it: vim, the 30-minute build, the migration script
A dropped SSH connection is the same chain with a network hiccup at step 1. The job didn't crash; its terminal died, and it got hung up on.
tmux's move: take the PTY master away from the window and hand it to a background daemon that never exits.
Without tmux: the window owns the session
close the window → master closes → SIGHUP → everything dies
With tmux: a daemon owns the session
close the window → you only unplugged the remote — server, shell, and jobs continue
Reattach later — from the same machine or over SSH from a different one — with tmux attach, and the server hands you the screen back as if you never left.
Since the tmux server took over the terminal emulator's role, it does the terminal emulator's jobs: it holds a PTY master per pane, maintains each pane's screen buffer (that 2-D grid again), forwards keystrokes, and multiplexes many panes and windows over one connection. Which makes two of its commands almost obvious:
capture-pane— export a pane's screen buffer as plain text. Reading memory the server already maintains.send-keys— write bytes into a pane's PTY master. Exactly what Ghostty does when you type.
And here's where the PTY's airtight abstraction becomes a superpower: the program on the slave end cannot distinguish send-keys from human typing. Same device, same bytes, same line discipline. Any program that "needs a human at the terminal" can be driven by another program holding the master.
That's the entire mechanic behind an orchestrator agent driving Claude Code. Step through one full loop:
An orchestrator agent drives Claude Code through tmux
data flow
—The loop is capture-pane (read the screen) → decide → send-keys (type) — a robot with eyes on the screen buffer and fingers on the PTY master. tmux turned human-computer interaction into program-program interaction without the program on the other end ever knowing.
One server, a whole agent team
Nothing above is limited to a single pane. A tmux server multiplexes any number of sessions, windows, and panes — each pane with its own PTY and its own process. That's exactly the shape of a multi-agent setup: Claude Code in pane one, Codex in pane two, Gemini in pane three, every pane addressable by name.
One tmux server, three agents, one control loop
tmux client commands, addressed by pane name
↓
one PTY per pane
↓
each agent thinks it has a human and a keyboard to itself
Spelled out as the properties agent harnesses actually need, the reason they all converged on tmux instead of building something custom:
- Persistence. The server owns every PTY master, so agents keep working after you close the laptop, drop the VPN, or hand off to a coworker. A half-finished refactor survives the commute.
- A universal API.
capture-paneandsend-keyswork on any terminal program with zero integration work — Claude Code, Codex, Gemini, or a build script from 1995. No plugin, no SDK on the agent's side: the PTY guarantee means the driven program can't even tell it's being driven. - Cross-agent communication. Any client can read any pane and type into any pane — including a program running in one of the panes. Agent A captures agent B's screen, reads its output, and types a reply into B's terminal: a team of peers reviewing each other's work, with the terminal itself as the message bus.
- Observability.
tmux attachand the robot theater becomes a human UI — you watch your agents type in real time, and when one wedges itself, you grab the keyboard for exactly that pane. The same session is simultaneously an automation API and a dashboard.
None of these are features tmux added for the agent era. They fall out of one 2007-era design decision — the daemon holds the masters — meeting one 1980s design decision: programs talk to terminal-shaped interfaces, not terminals.
Strictly speaking, there are two PTYs in the tmux picture
One connects Ghostty to the tmux client; another connects the tmux server to the shell. The simplified diagrams above are the right mental model — the detail only matters when you notice both layers doing line-discipline work. SSH is the same pattern stretched over a network: Ghostty → PTY #0 → ssh client → encrypted tunnel → sshd → PTY #1 → remote shell. Two fake wires, real network in between.
Where the shell sits in the OS
A final placement, since "the shell is part of the operating system" is true but easy to over-read. The OS has two layers: the kernel — manages hardware, processes, file systems, and PTY devices; you never touch it directly — and userland programs. The shell is userland, same as ls, same as Ghostty, same as your browser. What's special about it is position, not privilege: it's the first program launched for you at login, the front desk of the building. The kernel is the building's utilities — invisible, and running everything.
The whole story, one paragraph
A terminal was a physical machine at the end of a wire. The machine became a window (terminal emulator), the wire became a kernel object (the PTY), and the program on the far end — the shell — never noticed any of it. Buffers stage the traffic: a kernel line buffer holds your typing until Enter; the emulator's screen buffer holds what you see. tmux re-homes the PTY master from the fragile window to a daemon that never exits, which both survives disconnects and exposes the session to automation: capture-pane reads the screen buffer, send-keys writes the master, and the program on the slave end can't tell a robot from a human. Give every agent its own pane and the control room assembles itself — Claude Code, Codex, and Gemini each behind their own fake wire, one daemon holding every master, and anything (human or program) able to read every screen and type on every keyboard.
This is also a textbook instance of another level of indirection: the shell never holds a reference to real hardware, only to a terminal-shaped kernel interface — so the thing behind that interface could be swapped from a teletype to a window to a daemon across six decades, and the control point in the middle is exactly where the kernel implements line editing and where tmux taps the stream.
Quick check
Why can tmux send-keys 'operate' an interactive program like Claude Code?
Iron rules
⚡ Iron Rule #1
Terminal decides how it looks; shell decides how it works
They meet only at a byte stream, so they swap independently: change Ghostty for iTerm2 and zsh doesn't notice; change zsh for fish and the window doesn't care. When something misbehaves, route the bug first — rendering, fonts, key encoding → terminal; completion, history, parsing → shell; line editing before Enter → the kernel between them.
⚡ Iron Rule #2
On the slave end of a PTY, bytes are bytes
A program cannot distinguish keystrokes from bytes written into the master by a script, a test harness, or an agent. Any tool that 'requires an interactive terminal' is automatable through a PTY — that's the foundation under expect, pexpect, CI fakes, and tmux-driven agents alike.
⚡ Iron Rule #3
Whoever holds the PTY master owns the session's lifetime
Master closes → SIGHUP → the shell and all its children die. That single fact explains why closed windows and dropped SSH connections kill jobs, and why tmux fixes it: it moves the master into a daemon that never exits. Long-running job? Decide who holds the master before you start it.