diff --git a/mksh/mkshrc b/mksh/mkshrc index cc4967b..198f6d0 100644 --- a/mksh/mkshrc +++ b/mksh/mkshrc @@ -220,8 +220,20 @@ PS4='[$EPOCHREALTIME] '; PS1='#'; (( USER_ID )) && PS1='→'; PS1=$'\001\r''${| # grants each connection its own session grouped with "main" (shared panes, # independent window navigation). No -exec, so if tmux is missing or # exits/crashes for any reason, this shell just continues normally. +# +# Guards, in order: interactive; stdin and stdout are both a terminal; not +# already inside tmux; not below a tmux we started ($TMUX_AUTOSTART survives +# into panes and into nested shells that strip $TMUX, e.g. sudo -i, env -i, +# ssh, dropbear); login shell only, so tools that spawn `$SHELL -i` (fzf-tmux, +# file-manager shell escapes, editor :sh) don't drag in a whole tmux client; +# terminal can actually host tmux; tmux is installed. tmuxbin=$(\\builtin whence -p tmux 2>/dev/null) -if [[ $- = *i* ]] && [[ -t 0 ]] && [[ -z $TMUX ]] && [[ -n $tmuxbin ]]; then +if [[ $- = *i* ]] && [[ -t 0 ]] && [[ -t 1 ]] \ + && [[ -z $TMUX ]] && [[ -z $TMUX_AUTOSTART ]] \ + && [[ $0 = -* ]] \ + && [[ $TERM != dumb && $TERM != linux ]] \ + && [[ -n $tmuxbin ]]; then + \\builtin export TMUX_AUTOSTART=1 "$tmuxbin" fi unset tmuxbin diff --git a/tmux/tmux.conf b/tmux/tmux.conf index 3f1a75b..0662e00 100644 --- a/tmux/tmux.conf +++ b/tmux/tmux.conf @@ -2,7 +2,10 @@ # set-option -g default-shell /bin/mksh set-option -g focus-events on set -s escape-time 0 -set -g detach-on-destroy off +# Must stay `on` (the default): the per-client sessions below set +# destroy-unattached, so with `off` a detach would destroy the session and then +# punt the client into "main" instead of letting it leave. +set -g detach-on-destroy on set -g destroy-unattached off # bare `tmux` (no subcommand, e.g. from mkshrc's auto-attach) gets its own