So when I open a new terminal (I use terminator on Arch Linux), I have it configured to either open a new tmux session if one doesn't exist, or attach to an existing one. When I type exit at the prompt, it quits the tmux session. I have to type exit again to quit the terminal. What I want is that typing exit once will quit the terminal, but leave tmux running, so the next time I open the terminal it will reattach to the previous tmux session. Essentially, this is the equivalent of clicking the exit button for the window manager, but I want this functionality when typing exit.


This is something like what I want:

alias exit='if [[ $TMUX = "" ]]; then exit; else tmux detach; exit; fi'

but the issue is the exit after the tmux detach should get called in the terminal containing the tmux session, not the tmux session itself.

  • please elaborate how exactly you "have it configured to either open a new tmux session if one doesn't exist, or attach to an existing one" – lesmana Aug 5 '14 at 20:44
  • @lesmana I was just providing context. How that's configured it completely irrelevant to the question. You can imagine I don't even have that configured, and the question would be the same. – gsingh2011 Aug 5 '14 at 21:20
  • exit is not a tmux command; it's a shell command. tmux only exits if you terminate the command (usually, a shell) in the last pane of the last window. It sounds like you just want to detach from the current session, in which case you should use the appropriate tmux command or key binding. – chepner Aug 6 '14 at 15:36
  • @chepner, I know exit isn't a tmux command. I want to detach from the session and exit the terminal (not the session), but I want to use the exit command in a tmux session to do that. I think it should be possible with some bash-fu. – gsingh2011 Aug 6 '14 at 15:55
  • Why? You're trying to override the shell built-in exit to not exit a shell. Anyway, as long as you have tmux be the actual command that the terminal runs (as opposed to starting tmux as a child of a shell started by the terminal), detaching will cause the last program running in the shell to exit and, assuming your terminal is configured to do so, automatically close the window. – chepner Aug 6 '14 at 16:05

We have to declare two functions one to start tmux and the other to exit tmux :

function ttmux {
if     (pgrep tmux); then
       tmux attach
  else tmux
builtin exit

This will execute either (tmux attach or tmux) if tmux process existed or not , after you finish using tmux , built-in exit will be executed to close the terminal(if there isn't nested shell).

function exit {
if      [ ${TMUX} ]; then
          tmux detach
      else builtin exit

If you inside tmux will detach it , if not will execute built-in exit

Put them in your .bashrc or .zshrc and change function name if you wish, and call them.

>> ttmux # to start tmux
>> exit # to detach tmux
| improve this answer | |
  • Follow-up: is it possible for this exist function to detect panes and not exit? If so, how? – John Do May 21 '18 at 14:46
terminator -e tmux

this will start terminator with tmux running instead of a shell. if the tmux process exits terminator will close right after.

| improve this answer | |
  • Right, but typing exit will still kill the final terminal and exit tmux, right? – John Do May 21 '18 at 14:47

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.