When I exit the last terminal in a tmux session it will automatically close tmux and return me to the parent terminal. Is it possible to instead have tmux switch to another, already running, session instead of closing?

I create a new named session for every task that I work on, such as TRxxx, email, scratch etc and when I am done with one of them I close all the terminals by running exit in every one. When you run exit in the last terminal of a session tmux will return you to the parent terminal but is it possible to get it to just switch to one of the other open sessions instead so I don't have to reattach?

This question is similar to "Kill a tmux session and select another tmux session" but I want to close my terminals the nice way by using exit rather than killing the window.


The closest I got was a tmux function I wrote. I normally exit the shell by hitting Ctrl+D, so I programmed tmux to exit and switch sessions when hitting [PREFIX] Ctrl+D. Put the following in your .tmux.conf:

bind C-d run-shell "                                        \
    if [ #{session_windows} -eq 1 ] &&                      \
       [ #{window_panes}    -eq 1 ] &&                      \
       [ #{pane_current_command}  = 'bash' ]; then          \
        if [ \$(tmux list-sessions | wc -l) -ge 2 ]; then   \
            tmux switch-client -ln;                         \
        fi; tmux kill-session -t \"#S\";                    \
    else                                                    \
        tmux display-message \"Ignoring kill session...\";  \
    fi;                                                     \

Hit [PREFIX] Ctrl+D and it exits the current session if (and only if) it holds only one shell which is not running any other commands. It will switch to another session if possible. I use the bash shell, so you might need to change it to something you are using.

ps: in case it matters, I'm currently using tmux 1.9a.


I added this to my ~/.tmux.conf:

set-option -g detach-on-destroy off

When I destroy the last shell in a session, it switches to another active session. Once all sessions are closed, tmux exits.

  • 6
    right answer should be updated to this – cviejo Nov 19 '16 at 10:55
  • Please petition the poster of the question. – RichN Sep 19 '17 at 14:43
  • It's important to note that tmux does not update that setting when detaching and reattaching to existing sessions. You need to restart the tmux server for it to have an effect. – nyi Jun 11 '18 at 12:21

Add these lines to your .bashrc and try:

if which tmux 2>&1 >/dev/null 
    # start a new session if not exist
    test -z ${TMUX} && tmux

    # when quitting tmux, try to attach to other session
    while test -z ${TMUX}; do
        tmux attach || break
  • 1
    It's almost what I want but is it possible to change it so that it only runs if I exit from inside tmux? This script will attach to existing tmux sessions whenever I open a new terminal. I try to have a tmux session for local stuff and then I open a new terminal when ssh:ing to another machine where I will have a separate tmux session to avoid having them nested. – David Holm Mar 26 '14 at 9:54
  • No, this script will start new tmux session whenever you open a new terminal, not attach to existing. Can you provide more details, what do you mean "inside tmux"? – cuonglm Mar 26 '14 at 10:04
  • 1
    Let's say I have session X and Y, where Y only has one terminal open. I run 'exit' in the last terminal in Y which will cause the session to terminate. In this case I would like tmux to switch to session X instead of terminating. But I don't want a tmux session to be attached/created whenever I launch a new terminal. – David Holm Mar 26 '14 at 11:42

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.