How can I attach to the the first tmux session that has no attached clients or, if there are no unattached sessions, create a new session and attach to that?

(The use case is for a command for my terminal emulator to run when opening a new terminal window. I don't want it to create a new session each time I open a window, if there are detached sessions lying around. Nor do I want it to re-attach to the same named session every time, if there's already a window attached to that session. I want it to recycle existing unattached sessions but create new sessions when there are none unattached.)

3 Answers 3


There may be a simpler way to do this using tmux itself, without resorting to shell scripts. But I got it working by combining a couple of scripts.

The first script prints out the name of the first unattached session. This is ,tmux-first-unattached-session:

#!/usr/bin/env sh
# Print the name of the first tmux session that has no clients attached.
tmux ls -F '#{session_name}|#{?session_attached,attached,not attached}' 2>/dev/null | grep 'not attached$' | tail -n 1 | cut -d '|' -f1

The second script attaches to the first unattached session or a new session:

#!/usr/bin/env sh
# Attach to the first tmux session that has no attached clients.
# If there are no unattached sessions, then create a new session.
tmux attach -t `,tmux-first-unattached-session` 2> /dev/null || tmux
  • Do you actually have a comma in your filename? Strictly speaking, there’s nothing wrong with that, but it is unconventional. Jul 8, 2019 at 21:57
  • 2
    I really do: rhodesmill.org/brandon/2009/commands-with-comma Jul 8, 2019 at 22:16
  • ­Interesting philosophy. Jul 9, 2019 at 0:59
  • 1
    This does not work anymore as of tmux 3.3: github.com/tmux/tmux/issues/3218#issuecomment-1156343466 This is because tmux attach -t now looks for a session if an invalid or none have been specified. The second script needs to be changed to check the return code of tmux-first-unattached-session Jun 15, 2022 at 12:41
  • @LarsFrancke yeah, thanks for the tip. I was just wondering why my config suddenly stopped working :-). Now have to fiddle a bit to make it working: bindsym $mod+Return exec --no-startup-id "xterm -e \\"tmux attach -t $(sess=$(tmux ls -F '#{session_name}|#{?session_attached,attached,not attached}' 2> /dev/null | grep 'not attached$' | tail -n 1 | cut -d '|' -f1) 2> /dev/null; echo ${sess:-fakesession}) || tmux\\"" Jun 30, 2022 at 2:10

I have something like this which is pretty simple.

  1. Detect if there is an un-attached session, try to attach
  2. Else start a clean session
# Start TMUX first; try to reattach a session
if [[ -z $TMUX ]]; then
  ATTACH_OPT=$(tmux ls | grep -vq attached && echo "attach -d")
  exec eval "tmux $ATTACH_OPT"
  • 1
    Is there a reason you're doing exec eval and not just tmux $ATTACH_OPT?
    – evan
    Sep 12, 2020 at 1:06

I created a tmux2 script to handle this problem:

# tmux2 - attach to first unattached session or
#         create new session if none are found
N=$(tmux ls | grep -v attached | head -1 | cut -d: -f1)

if [[ ! -z $N ]]
    ATTACH_OPTS="attach -t $N"

exec tmux -CC $ATTACH_OPTS

In my case I'm using this for an iterm connection from my Mac. The connection is set up like this:

enter image description here

Now if my terminal session drops, I just reconnect and it picks up where I left things.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.