tmux grouped sessions

2023-09-23

I have two monitors at work. Normally I have a terminal running tmux open full screen on the right-hand monitor, in portrait, and use the left monitor in landscape for everything else. Occasionally however, I want to also have a terminal open on the left monitor, and to move windows easily between terminals. To make it easier to share windows across terminals, I have adjusted my tmux setup to take advantage of the grouped-sessions feature.

At the bottom of my .tmux.conf I have this chunk, which starts a new session called “main”, as a daemon.

new-session -s main -d

Then, at the bottom of my .bashrc I have this chunk. First it checks if tmux is already running (if [ -z "$TMUX" ]). Then if tmux isn’t running, it creates a new session within the same group as session “main” (new-session -t main). It then sets the option destroy-unnattached, which removes clients when you detach from them, ensuring that the automatically created sessions don’t pile up when they are detached from.

if [ -z "$TMUX" ]; then
    /usr/local/bin/tmux new-session -t main \; set-option destroy-unattached
fi

The result is that when I open a terminal, tmux checks if the “main” session exists, creates it if it doesn’t and attaches a client to that session.

All sessions in the same group share the same set of windows, but you can move freely between windows in each session.