Skip to content

Instantly share code, notes, and snippets.

Last active December 9, 2023 23:07
  • Star 68 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save chakrit/5004006 to your computer and use it in GitHub Desktop.

TMUX - Single window group, multiple session.

So I have been using tmux for a while and have grown to like it and have since added many many customizations to it. Now once you start getting the hang of it, you'll naturally want to do more with the tool.

Now tmux has a concept of window-group and session and if you are like me you'll want multiple session that connects to the same window group instead of a new window group every time. Basically I just need different views into the same set of windows that I have already created, I don't want to create a new set of windows every time I fire up my terminal.

This is the default case if you simply use the tmux command as your login shell, effectively creating a new group of windows every time you start tmux.

This is less than ideal because, if you are like me, you fire up one-off terminals all the time and you don't want all those one-off jobs to stay running in the background. Plus sometimes you need information from another window and if your windows are each connecting to different tmux session, then that's an unnecessary PIA to development.

So I did a Google Search and eventually hit on this question on SO.

But the question has been closed and the answers does not really solve my problem.

What I want to do is basically:

  • Only a single group of windows, ever. I want to see the same group of windows whenever I fire up my terminal.
  • Each terminal is a separate session. This is a problem if you use the tmux attach command as your login shell because then all your windows now synchronizes to the same view and you lose the benefit of having a separate window.

So after a few hours of experimenting with various tmux commands and configuration I have been successful in getting the exact setup that I want without having to switch away from tmux at all.

Here's the gist of it:

I. Creates a new session (and a new window group) in your .conf file.

This is the best place to create the initial window group and session. Put this at the end of your .tmux.conf:

new-session -s main

This command will simply creates a new session named "main" if none exists. If the session already exists, this command will just fail silently in the background.

II. Always create a new session but also always attach to the main session.

Use this as your login shell command (or "Run Command" for OS X

/usr/local/bin/tmux new-session -t main

This effectively creates a new session attached to the "main" session without creating any new window groups. So every time you open a new window, it will be using the main window group in a new session.

That's it!

Now how this is different from other solutions in that SO post because:

  • Each window is now a new session into an existing group that works and is usable separately from each other, as it should be.

  • There is always only a single window group unless you explicitly create new ones. This way you only need to worry about multiple windows in the main group, not multiple windows spread across multiple window groups that you did not knew you have left accidentally left running.

  • You'll never leave any session dangling when you exit all windows (since the main session will also exits and there is only a single main group of window.) thus leaving you with perfectly clean environment. No orphaned processes, no orphaned sessions or window groups. No more EADDRINUSE in a forgotten window.

  • However, the main window group (and processes running in it) will only close when all windows inside it have exited, because you are never using the main session, only a new session attached to it. The main session always stay running keeping the main window group alive. This effectively lets you close (or crash) any (or all) terminal window any time without actually terminating whatever you have running and be able to view it again instantly just by opening a new window (which is actually a new session into an existing window group which never really have exited.)

Hope this helps!

Copy link

I like it! This really helped. Thanks.

Copy link

ghost commented Nov 2, 2013

That absolutely rocks! Thank you so much!

Copy link

ghost commented Nov 2, 2013

One remark.
Substituting the usual attach-session with new-session -t is really cool, but one has to change their way of leaving tmux accordingly. If you keep using C-b d (detach-client by default), detached sessions will start building up.
To match the new model I ended up remapping C-b d to kill-session.

Copy link

werkshy commented Jul 4, 2017

This StackExchange answer points to an alternative approach for the 'zombie detached grouped sessions' problem: set the destroy-unattached option in the grouped sessions (but not in the main one).

tmux new-session -t main \; set-option destroy-unattached || tmux new-session -s main

Hitting $PREFIX-d will detach and automatically destroy the other client sessions but will only detach the original 'main' session.

Copy link

Thank you for this!

Copy link

patstew commented Nov 15, 2017

werkshy's comment seems to almost work but the initial session is never created because the part before the || always succeeds.

tmux new-session -d -s main; tmux new-session -t main \; set-option destroy-unattached

seems to work properly

Copy link

neverfox commented Apr 2, 2018

I'm having trouble wrapping my head around the use-case for this. I understand your goals and how this meets them, I just don't see the usefulness of "only a single group of windows, ever" personally. I often want a few windows to be shared across all of my terminals (e.g. weechat, htop), but I also want those terminals to have windows that are unique to them (e.g. personal, work, school). Window groups are two-way, so that any window I create is now part of all sessions in the group, so there's no way to have that partial overlap (unlike with link-window to a "shared" session). It would be nice if there was a way to do something like this where a new session brings in all windows from a shared session but also has its own unique windows (whatever you create while in the new session).

Copy link

Have a look at "Tmuxinator" which might be a solution to your problem

Copy link

thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment