tmux equivalent of "screen -R"?

After consulting the wizards of IRC I am confident there is no single tmux command that has this behavior. Luckily, it is fairly easy to emulate using the shell:

(tmux ls | grep -vq attached && tmux at) || tmux

Despite what it says in tmux manual:

             The target-session rules for attach-session are slightly
         adjusted: if tmux needs to select the most recently used session,
         it will prefer the most recently used unattached session.

Drew's answer won't work properly in a situation like:

0: 1 windows (created Wed Nov  7 23:51:08 2012) [177x47]
1: 1 windows (created Wed Nov  7 23:51:33 2012) [177x47] (attached)

tmux at will attach to the last session (#1) (even though this session is still attached somewhere else). This ruins the whole idea of running several tmux sessions and attaching only to a detached one (to use mosh + tmux + iterm2 to create a perfect roaming terminal).

Another approach would be to manually select a non-attached session:

tmux ls | grep -vq attached && tmux at `tmux ls | grep -vm1 attached | cut -d: -f1`


I tend to only have two tmux sessions at a time, so I do something like this in each terminal window.

terminal window 0:

tmux attach -t 0 || tmux new

terminal window 1:

tmux attach -t 1 || tmux new

Tags:

Tmux