Overriding exit to tmux detach I was using mosh like this: ```bash function mssh { echo "> mosh --no-init $1 -- tmux new-session -A -s main" mosh --no-init $1 -- tmux new-session -A -s main } ``` But sometimes I would enter `exit` instead of `tmux detach` and I would lose the session I had on the remote! So I found this code block that I inserted into my remote `~/.bashrc` that would help me. It's simple to read, if the session is not tmux, we exit, but if it is, check if it is desirable to preserve the session. ```bash # also see https://medium.com/@toja/tip-using-mosh-with-scrollback-257a54a848b3 exit() { if [[ -z $TMUX ]]; then builtin exit return fi panes=$(tmux list-panes | wc -l) wins=$(tmux list-windows | wc -l) count=$(($panes + $wins - 1)) if [ $count -eq 1 ]; then tmux detach else builtin exit fi } ``` Note, pressing (CTRL+D) will still exit the session, if you want. tags: code