Skip to content

tmux

tmux attach session: tmux a, -d, and the resize problem

tmux attach -t name reattaches to a session by name; tmux a is shorthand for the most recent one. Use -d to detach other clients first.

Ivo Hart6 min read

tmux attach -t name reconnects your current terminal to the session called name. tmux a is shorthand for the same command and, with no -t, attaches to the most recently used session. The part that actually causes confusion isn't attaching — it's what happens when two terminals attach to the same session at different sizes, which is covered below.

attach vs attach-session vs tmux a

These are three ways to type the same command — all covered alongside the rest of tmux's session commands in tmux commands:

FormNotes
tmux attach-session -t workFull name, rarely typed in full
tmux attach -t workattach is a built-in alias
tmux a -t worka is a shorter alias for attach
tmux aNo -t: attaches to the most recently used session

There's no functional difference between them. tmux a with no target is the one worth building a muscle-memory habit around — it's the fastest way back into whatever you were doing last.

Attaching by name or index

-t takes either the session name or its index:

~ — attaching by name
[object Object]

tmux ls (alias for list-sessions) before you attach is worth the habit — it's a two-second check that saves you attaching to the wrong session, and it's the only reliable way to see a session name you don't remember setting. See tmux new session for why naming sessions at creation time makes this list actually readable.

-d: detach other clients before you connect

If a session is already attached somewhere else — another terminal on the same machine, or a second SSH connection — attaching again doesn't kick that client out by default. Both clients stay attached, sharing the same view. -d changes that:

tmux attach -d -t work

This detaches every other client first, then attaches yours. It's the right call any time you left a session open on another machine or terminal and don't want to fight over which client's window size wins — which is the actual problem, covered next.

The size problem: two clients, two terminal sizes

This is the single most common source of "tmux is being weird" reports, and it isn't a bug — it's tmux doing exactly what it's configured to do. A tmux window (not the terminal emulator, the tmux window) has one size. If two clients are attached to the same session and looking at the same window, and their terminals are different sizes, tmux has to pick one size for the window. By default it picks the smallest.

That's the window-size option, and its default is latest in modern tmux, but the practical effect people hit is still governed by aggressive-resize:

SettingBehaviour
window-size smallestWindow is sized to the smallest attached client viewing it — the classic default
window-size latestWindow sizes to whichever client had the most recent activity
window-size largestWindow sizes to the largest attached client
aggressive-resize onWindow resizes based only on clients where it's the *current* window, not every client in the session

The practical version: if you attach from a small terminal (say, a phone SSH client or a narrow split) while a wide session is already attached elsewhere, everyone's view can shrink to fit yours — even the person who isn't looking at that window. aggressive-resize on scopes that to only the clients actually viewing the window in question, which is what most people actually want:

~/.tmux.conf
setw -g aggressive-resize on

If you don't want a second attach affecting the layout at all, use -d to detach the other client instead of sharing the view, or attach with -r for a read-only client that observes without contributing to the size negotiation.

Attaching over SSH

Nothing about tmux attach changes over SSH — it's the same command, run on the remote machine, not your laptop:

local — attaching to a remote session
[object Object]

The session lives on deploy-box's tmux server, not on your laptop, so closing the SSH connection (network drop, laptop sleep, closing the terminal) detaches you but leaves the session running exactly like a manual prefix d would. Reconnect and tmux a gets you back to where you left off. That's the entire reason to run long jobs inside tmux over SSH in the first place — an unstable connection stops mattering.

Checking a remote session without attaching

ssh deploy-box tmux capture-pane -p -t build prints the pane's current contents without attaching at all — useful for a quick check from a script or a phone terminal app where a full interactive attach is awkward.

"No sessions" and "can't find session" errors

tmux gives you two different messages depending on what's actually missing:

~ — the two failure modes
[object Object]

no sessions means the tmux server isn't running at all — nothing has been created since boot, or tmux kill-server was run. can't find session: bar means the server is running fine but no session by that name exists — check tmux ls for the actual name, or create it with tmux new -s bar. Either way, tmux new-session -A -s bar sidesteps both errors: it attaches if bar exists and creates it if it doesn't.

Checking a session when you aren't at a terminal at all

tmux attach assumes you have a terminal to attach from — SSH client, laptop, phone terminal app. The case it doesn't cover is wanting to glance at a session's output from a plain browser, with no SSH client installed. That's the specific gap mtmux fills: it serves the tmux session you already have to a browser tab and can push you a notification when a pane goes quiet, without changing anything about how attach itself works.

What is the difference between tmux attach and tmux a?

None functionally — a is a shorter built-in alias for attach, which is itself an alias for attach-session. tmux a with no -t attaches to the most recently used session.

Why does my tmux session shrink when someone else attaches?

By default a tmux window is sized to the smallest terminal among clients viewing it. Set setw -g aggressive-resize on in ~/.tmux.conf so resizing only accounts for clients actually looking at that window, not every client in the session.

How do I attach to a tmux session and kick everyone else off?

tmux attach -d -t name. The -d flag detaches every other client attached to that session before yours connects.

What does 'no sessions' mean in tmux?

The tmux server itself isn't running — no session has been created since the machine booted, or you ran tmux kill-server. Create one with tmux new -s name.

Does tmux attach work the same way over SSH?

Yes. Run it on the remote machine after connecting. The session lives on the remote tmux server, so a dropped SSH connection detaches you without killing anything running inside it.

Put this to work — mtmux attaches to the tmux server you already run.

Related reading

mobile

tmux from your phone: the modifier key problem

A native SSH app, a web terminal, or a purpose-built client can reach tmux from a phone — the real obstacle is Ctrl-b on a screen with no keyboard.

Ivo Hart7 min read