tmux – How to Attach to Different Windows in a Session

tmux

I have a pretty simple tmux session running with two open windows; one of them for local hacking and one of them for work.

What I'd like to do is to simply connect to the hacking window while leaving the work window open in another terminal. However, as soon as I connect to tmux, all commands are sent to both windows, so if I switch to another window, the same thing happens in the other terminal and vice-versa.

Is there a way for me to simply connect to each window separately?

Best Answer

The reason both clients switch windows at the same time is because they are both connected to the same session (the “current window” is an attribute of the session, not the client). What you can do is link one or more windows into multiple different sessions. Since each session has its own “current window”, you can then switch windows independently in each session.

The easiest way to use this feature is to use the “grouped sessions” feature of the new-session command:

$ tmux new-session -t 'original session name or number'

To see the sessions currently available:

$ tmux list-sessions

Each session in a group will automatically share the same set of windows: opening/linking (or closing/unlinking) a window in one session of the group automatically causes the same window to be linked (or unlinked) in all the other sessions of the group.

When you are done with your “extra” session, you can kill it with kill-session. The windows themselves will not be killed unless your session was the only one they were linked to. Alternatively, you can disconnect from your “extra” session like normal (Prefix d, or detach-client); if you do keep your “extra” session around (by simply detaching from it), you might want to give it a descriptive name (Prefix $, or rename-session) so that you easily identify it and reconnect to it later (you may also want to give the “original” session a name, too).


If you do not want to automatically share a dynamic set of windows, then you can use link-window (and unlink-window) to bring individual windows into (and out of) your own “personal” session; this offers non-automatic, and lower-level access to the same core functionality upon which “grouped sessions” are based (windows linked into multiple sessions).

Related Question