Search code examples
tmux

Is there any way to detect exiting a session on tmux?


I was wondering if there is a way to detect when I exit a tmux session. It is common that I have multiple sessions in my workflow and I manage them with tmux-fzf plugin. Everything is good but there are some times when I am done with a session and want to exit it. after exiting, it would completely exit tmux and put me where I ran tmux command in the first place, and if I want to reattach to my sessions, I have to type tmux a. So ideally, I wanted to detect when I exit a session and in my tmux.conf file set a hook to check for existing tmux sessions and if there is attached to them instead of completely detaching from tmux.


Solution

  • Ok, I finally got it, by putting this line on the .tmux.conf file, Whenever a session is exited, the client will switch to the most recently active of the remaining sessions.

    set -g detach-on-destroy off
    

    As the explanation for this option on the tmux man page says :

    If on (the default), the client is detached when the session it is attached to is destroyed. If off, the client is switched to the most recently active of the remaining sessions. If no-detached, the client is detached only if there are no detached sessions; if detached sessions exist, the client is switched to the most recently active.