Every terminal window I open automatically attaches to a new tmux session. Closing the shell inside such a tmux session should detach the tmux client for the terminal window to close. This can be done by setting this option:

set -g detach-on-destroy on

However, when I kill the current session from the session overview (Ctrl+b,w,x,y), the tmux client also detaches. Instead, I would like it to stay attached so I can select another session from the session overview.

The question is, how can I have tmux detach when the session exits because the process it's running (i.e. the shell) exits but stay attached when the session is killed from the session overview?


You might be able to do this by having detach-on-destroy off but putting detach-client in the pane-exited hook.

| improve this answer | |
  • Thanks, that sounds promising. Do you have an example how this might look like? – danijar May 15 '19 at 3:44
  • This works for me, but you won't be able to use remain-on-exit normally at the same time: set -g remain-on-exit on set -g pane-died 'if -F "#{&&:#{==:#{session_windows},1},#{==:#{window_panes},1}}" "killp; detach" "killp"' set -g detach-on-destroy off – Nicholas Marriott May 15 '19 at 14:46

Here it is a bit more readably:

set -g remain-on-exit on
set -g pane-died 'if -F "#{&&:#{==:#{session_windows},1},#{==:#{window_panes},1}}" "killp; detach" "killp"'
set -g detach-on-destroy off
| improve this answer | |

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.