How to exit terminal automatically after exiting tmux
Asked Answered
I

1

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!

Iggy answered 19/8, 2018 at 15:17 Comment(3)
@CharlesDuffy Sorry I forgot to state how I start tmux, I will reedit my question now.Iggy
BTW, for future note, questions about interactive shell use (as opposed to scripting) are often a better fit for Unix & Linux or SuperUser.Tidwell
@CharlesDuffy OK, Thanks for the remind.Iggy
T
10

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.

Tidwell answered 19/8, 2018 at 23:59 Comment(3)
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.Iggy
Been a while since I used GNOME, but I remember it being configurable via clicking around GUI menus.Tidwell
...per unix.stackexchange.com/questions/81165/…, there should be an option to use a custom command instead of invoking your default shell in the gnome-terminal profile editor.Tidwell

© 2022 - 2024 — McMap. All rights reserved.