Tell me more ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I would like to know if there is a way to get tmux to behave like screen -D -R so I could say, have the command as a default command in Putty.

These screen switches would force detach of an existing screen session for my user (even if it was still active and logged-in somewhere else) and reattach it to the current session. Also, in the case that no screen session existed, it would create a new one.

I like tmux and can see clear benefits over screen, but the existence of this feature would really seal the deal.

tmux attach doesn't seem to create a new session if there isn't one.

The man page for tmux says:

If no server is started, attach-session (attach) will attempt to start it; this will fail unless sessions are created in the configuration file.

I'm not sure exactly what the section in bold means: I can't find an example of creating a session in the conf file.

Any suggestions?

Thanks.

share|improve this question
From the man page: "The configuration file is a set of tmux commands which are executed in sequence when the server is first started." -- you could actually have attach or new-session as a line in your .tmux.conf. – sr_ Mar 5 '12 at 13:35
@sr_ Well, the when the server is first started new-session would have just happened anyway... – Chris Down Mar 5 '12 at 13:36

3 Answers

up vote 2 down vote accepted

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 :)

share|improve this answer
Yes! I'm marking this as the answer because it sticks most closely to the details of the original question (i.e. about what the man page entry referring to the .tmux.conf means), but I think all the suggestions so far do work. Thanks. – Geeb Mar 5 '12 at 16:29
I prefer not to have new-session in ~/.tmux.conf because if I reload the configuration using command source-file ~/.tmux.conf (see this) a new session will be launched. Yes, I can kill it and then attach to the previous one but that is a little annoying. – mmoya May 14 '12 at 11:04

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
share|improve this answer

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.

share|improve this answer
Thanks @Chris, I'll give it a try when I get chance. – Geeb Mar 5 '12 at 14:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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