MacOS – tmux session appears twice with different names

macostmux

I've run into an odd situation with tmux just now, and I'm not sure if it's a bug, or (more likely) something that I did by accident:

I have two sessions:

foo-0
foo-1

Attaching to them with tmux attach-session -t foo-[0/1] works, but I see the same thing in both cases. Changes made to one session (closing a pane, running commands, etc) apply to the other.

I created a new session with tmux new-session -t bar, and that produced a new session which shares nothing with the first two.

What causes this behavior, and how do I "consolidate" my two sessions into one without losing both of them?

Best Answer

It's not a bug.

What causes this behavior?

In tmux you can link any window to multiple sessions. In effect you can have two (or more) sessions with exactly the same set of windows in the same order. In such case the sessions look the same. There are two possibilities:

  1. Either you created two separate sessions and then linked (link-window) and arranged (move-window) windows, so the sessions look the same. I think it's hard to do this by accident, so probably this is not what happened.

  2. Or you created two sessions in the same session group. It's very easy to do this if you use new-session -t instead of new-session -s. Your question makes me believe you used -t when you meant -s. The rest of my answer investigates this possibility.


This is the relevant part of man 1 tmux:

new-session [-AdDEP] [-c start-directory] [-F format] [-n window-name] [-s session-name] [-t group-name] [-x width] [-y height] [shell-command]

(alias: new)

Create a new session with name session-name.

[…]

If -t is given, it specifies a session group. Sessions in the same group share the same set of windows - new windows are linked to all sessions in the group and any windows closed removed from all sessions. The current and previous window and any session options remain independent and any session in a group may be killed without affecting the others. The group-name argument may be:

  1. the name of an existing group, in which case the new session is added to that group;
  2. the name of an existing session - the new session is added to the same group as that session, creating a new group if necessary;
  3. the name for a new group containing only the new session.

[…]

My hypothesis is you wanted to create a new session with name foo. You mistook -t for -s and instead of tmux new-session -s foo you invoked

tmux new-session -t foo

Here foo is the name for a new group. You did not provide session-name. My tmux in such circumstances names the new session foo-N and assigns it to the new group foo. N is 0 if it's the very first session within the tmux server. I think this way you got foo-0.

Then you forgot about this session and invoked again:

tmux new-session -t foo

Here foo is the name of an existing group. You did not provide session-name this time either. The tool named the new session foo-1.

In effect the two sessions are in the same session group, they behave like the manual states. If you're not familiar with session groups then it's easy to interpret them as one session that appears twice with different names.


How do I "consolidate" my two sessions into one without losing both of them?

Kill all the grouped sessions but one. In your case

tmux kill-session -t foo-1
# xor
# tmux kill-session -t foo-0

should work. Usually killing a session kills its windows (so also panes in these windows); but any window that is linked to a session that survives will survive. Windows linked to a session belonging to a session group are linked to all sessions belonging to the group. So only killing the last session standing in the group actually kills its windows.


You may want to make sure the sessions in question are truly in the same session group before you kill any session. In general it may be they share some windows but not all of them; or they just look alike. Invoke

tmux list-sessions

This will provide you information about session groups (if any). I expect both sessions (foo-0 and foo-1) will report (group foo). This shows they belong to the same session group. You can request a custom format (-F) or even query for the session group of a particular session. In your case:

tmux display-message -p -t foo-0 '#{session_group}'
tmux display-message -p -t foo-1 '#{session_group}'
Related Question