Take the 2-minute tour ×
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.

What does the section in bold mean? (I can't find an example of creating a session in the conf file).

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
    
Just found this (closed) question on SO. Same question with similar answers. stackoverflow.com/q/3432536/168034 –  phunehehe May 29 '13 at 12:34
add comment

4 Answers

up vote 6 down vote accepted

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
    
Yes - this seems to be the most effective way to achieve the what I requested. Likewise, I disapprove of new-session in the config file in case you want to edit it and reload during a tmux session. –  Geeb Jan 16 at 10:07
add comment

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
    
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
add comment

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
add comment

I find this works best for me on .bashrc

if [[ -z $TMUX ]]; then
  tmux attach-session || tmux new-session
fi
share|improve this answer
    
How does that explain the bold section in the OP? –  Anthon Oct 14 '13 at 5:53
add comment

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.