88

If I use

tmux attach

I can attach to a running session but if there is no session running, I only get the error

no sessions

How can I automatically start a new session if there is none running? something like

tmux attach-or-create-new-session
50

The answer is much simpler. Just put this in your ~/.tmux.conf file:

# if run as "tmux attach", create a session if one does not already exist
new-session -n $HOST

If you run tmux attach and there is a session, then it will attach to that session (whether it's already attached or not). If there is not a session already then it will create one for you.

  • 11
    this is a neat trick, but there is a caveat: if tmux is invoked without arguments, it will create a new session and then create a second session as soon it reaches this line in your ~/.tmux.conf. You can see this issue by executing tmux ls after creating the first session. In practice, after you put this in your file, you cannot call tmux with no arguments anymore – Bruno Polaco Nov 23 '14 at 23:35
  • 4
    So you would have to add an alias alias tmux="tmux attach" to prevent this problem – rubo77 Mar 21 '15 at 15:07
  • @BrunoPolaco: what is the big caveat with an extra empty tmux session running all the time (apart from that it doesn't look tidy in the task-list)? – rubo77 Sep 26 '16 at 3:14
  • @rubo77 Resources. Some people have a few default tools, windows, panes, etc created when they launch tmux. – rovr138 Mar 24 '17 at 17:05
128

If naming your session is okay, then it's easy to do with the new-session command:

tmux new-session -A -s main

where main is the session name that will be attached to or created if needed.

From man tmux:

The -A flag makes new-session behave like attach-session if session-name already exists; in this case, -D behaves like -d to attach-session.

Please also note that the -A option is introduced in tmux version 1.8 from 26 March 2013, for earlier versions, use tmux attach || tmux.

  • 10
    +1 for using built-in functionality and providing man doc snippet – Ari Patrick Mar 21 '15 at 14:10
  • 1
    In my alias list: alias "tmux-attach-or-create-main-session=tmux new-session -A -s main". Thanks for the tip! Manual page: openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man1/tmux.1 – Jeroen Wiert Pluimers Oct 14 '15 at 10:40
  • tmux: unknown option -- A This only seems to be available in recent versions. – xApple Mar 26 '16 at 18:15
  • This is the method I used, but you can no longer use "tmux detach". You need to use the shortcut <ctrl>b - d to save and exit an existing session. – Jess Sep 20 '17 at 17:14
  • 2
    [ -z "$TMUX" ] && exec tmux new -As . this is what I use in my .bashrc. – ryenus Nov 24 '17 at 3:26
29

This will start a new session if attach gives an error:

tmux attach || tmux new

So an alias will do the job:

tm="tmux attach || tmux new"
  • You can even make it shorter by changing tmux attach for tmux a – Bruno Polaco Nov 24 '14 at 10:46
  • While this is functional, Wesley Baugh's solution uses tmux's built-in functionality to do the same thing – Ari Patrick Mar 21 '15 at 14:08
  • fwiw this does seem like the best answer for older versions, before -A was introduced (e.g. v1.6) – Christopher Peterson Jan 16 at 14:35
3

Consider adding the following to your .bashrc

if [ -z "$TMUX" ]; then
    base_session='my_session'
    # Create a new session if it doesn't exist
    tmux has-session -t $base_session || tmux new-session -d -s $base_session
    # Are there any clients connected already?
    client_cnt=$(tmux list-clients | wc -l)
    if [ $client_cnt -ge 1 ]; then
        session_name=$base_session"-"$client_cnt
        tmux new-session -d -t $base_session -s $session_name
        tmux -2 attach-session -t $session_name \; set-option destroy-unattached
    else
        tmux -2 attach-session -t $base_session
    fi
fi

You can see my use of this in my ZSH resource file at my github repo

2

To expand on Wesley Baugh's answer (which was double-nesting sessions for me when used in .bashrc on logins) and add a bit of flexibility since I often use sudo -s on servers (which would dutifully load my .bashrc again and double nest), here's what I have in my .bashrc:

if [ -z "$TMUX" ] && [ ${UID} != 0 ]
then
    tmux new-session -A -s main
fi

This checks for a tmux session and makes sure you aren't superuser before creating a new session or attaching to that existing one named main.

1

Drew Frank answered this here: https://superuser.com/questions/487363/tmux-equivalent-of-screen-r

Here's the script I now use for this (though, I've switched back to screen due to another issue with tmux) /somewhere/on/your/path/ttmux or as a shell function:

#!/bin/sh
# many thanks to Drew Frank: https://superuser.com/questions/487363/tmux-equivalent-of-screen-r
(tmux ls | grep -vq attached && tmux -2 at) || tmux -2

The -2 options make tmux assume 256 color terminal support, so those may not be appropriate for your situation.

1

If you're using this inside a .shrc file or similar with exec I'd recommend

if tmux ls
  exec tmux attach
else
  exec tmux
fi
1

I improved on @SuperMagic answer a little. I put this block at the top of my .zshrc

if [[ $TMUX = "" ]]; then
  # try to reattach sessions
  tmux ls | grep -vq attached && TMUXARG="attach-session -d"
  exec eval "tmux -2 $TMUXARG"
fi

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.