6

I am not sure whether similar questions were asked, but I did't find in SO.

I am using gnome-terminal + tmux. I've add if [ "$TMUX" = "" ]; then tmux; fi in my .zshrc so when I start terminal, I enter tmux automatically. Every time I press Ctrl-D and quit from the tmux, I have to press Ctrl-D again to quit from the terminal.

Now, what can I put in my .zshrc or tmux.conf so as to quit my tmux and terminal with only one press.

What I thought is that I can add a listener to capture tmux exiting event. And if that event occurs, let me quit from the window. But I have no idea how to achieve this. Any help will be appreciated!

WW00WW
  • 417
  • 4
  • 15

1 Answers1

9

If you start tmux with exec, as in:

exec tmux

...the parent-process shell will no longer even be in memory, so exiting tmux won't quit back to it.


Thus, in your dotfiles:

if [ -t 0 ] && [[ -z $TMUX ]] && [[ $- = *i* ]]; then exec tmux; fi

[ -t 0 ] is a safety feature: It avoids moving forward if your stdin is not a TTY. Similarly, the $- check avoids running on non-interactive shells.


By the way -- I would generally advise making this part of your terminal configuration, not part of your shell configuration, to avoid inadvertently impacting other kinds of shells (such as ones started via sshd, particularly by an automated tool rather than a human user). Scripts shouldn't simulate TTYs, or claim to a shell that they're interactive when they aren't, but it happens in practice, and this kind of practice can thus lead to surprises.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • But I am using Ubuntu18 gnome-terminal, I haven't found the terminal configuration file. I use zsh for my default shell, so I put it in my `.zshrc`. – WW00WW Aug 20 '18 at 00:24
  • Been a while since I used GNOME, but I remember it being configurable via clicking around GUI menus. – Charles Duffy Aug 20 '18 at 00:25
  • ...per https://unix.stackexchange.com/questions/81165/use-a-different-shell-by-default-in-each-terminal-emulator, there should be an option to use a custom command instead of invoking your default shell in the gnome-terminal profile editor. – Charles Duffy Aug 20 '18 at 00:26