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
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
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 Terminal.app 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 attachcommand as your login shell because then all your Terminal.app 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 Terminal.app)
/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 Terminal.app window, it will be using the
main window group in a new session.
Now how this is different from other solutions in that SO post because:
Each Terminal.app 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
exitall windows (since the main session will also exits and there is only a single
maingroup of window.) thus leaving you with perfectly clean environment. No orphaned processes, no orphaned sessions or window groups. No more
EADDRINUSEin 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!
I like it! This really helped. Thanks.