Is there a tmux equivalent of "screen -D -R"

Yes:

$ tmux attach -d || tmux new

-d is necessary to behave like screen -D, ie, detach everybody else.

Connect by ssh, then attach or create could be something like:

$ cat bin/stmux
#!/bin/sh
exec ssh -t $@ 'tmux attach -d || tmux new'

$ stmux my.remote.box

To make tmux attach create a new session when there isn't one, use the option new-session in the tmux config file. Create the file ~/.tmux.conf if it doesn't exist, and add

new-session

to it. Also, I alias tmux to tmux attach :)


You can emulate this with a shell function, this should work for any POSIX-compliant shell:

tmux() {
    if [ "$#" -ge 1 ] && [ "$1" = -z ]; then
        shift
        command tmux detach 2>/dev/null
        command tmux attach "$@" || command tmux new-session "$@"
    else
        command tmux "$@"
    fi
}

Now if you launch it as tmux -z, it should perform the actions you're looking for.