Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exit terminal automatically after exiting tmux

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!

like image 580
WW00WW Avatar asked Aug 19 '18 15:08

WW00WW


1 Answers

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.

like image 58
Charles Duffy Avatar answered Nov 15 '22 07:11

Charles Duffy