From 532dc3896575bd274f8c406b9c170c218b3a9d36 Mon Sep 17 00:00:00 2001 From: wholteza Date: Fri, 14 Aug 2026 09:28:59 +0200 Subject: [PATCH] Automatic prefix change to C-a when in ssh or mosh session --- .config/tmux-prefix-detect.sh | 15 +++++++++++++++ .tmux.conf | 8 ++++++++ 2 files changed, 23 insertions(+) create mode 100755 .config/tmux-prefix-detect.sh diff --git a/.config/tmux-prefix-detect.sh b/.config/tmux-prefix-detect.sh new file mode 100755 index 0000000..b405975 --- /dev/null +++ b/.config/tmux-prefix-detect.sh @@ -0,0 +1,15 @@ +#!/bin/sh +# Auto-set tmux prefix based on connection type. +# Called from tmux hooks: session-created and client-attached. +if tmux show-environment SSH_CONNECTION 2>/dev/null | grep -qv '^-' || \ + tmux show-environment MOSH_SERVER_PID 2>/dev/null | grep -qv '^-'; then + tmux set -g prefix C-a + tmux bind-key C-a send-prefix + tmux unbind-key C-b 2>/dev/null + tmux display-message "Prefix: C-a (remote)" +else + tmux set -g prefix C-b + tmux bind-key C-b send-prefix + tmux unbind-key C-a 2>/dev/null + tmux display-message "Prefix: C-b (local)" +fi diff --git a/.tmux.conf b/.tmux.conf index 61c384d..05ac3a5 100644 --- a/.tmux.conf +++ b/.tmux.conf @@ -18,5 +18,13 @@ set -g @plugin 'tmux-plugins/tmux-sensible' # set -g @plugin 'git@github.com:user/plugin' # set -g @plugin 'git@bitbucket.com:user/plugin' set-window-option -g mode-keys vi + +# Auto-switch prefix: C-a for SSH/mosh, C-b for local +set -ga update-environment 'MOSH_SERVER_PID' +set-hook -g session-created 'run-shell ~/.config/tmux-prefix-detect.sh' +set-hook -g client-attached 'run-shell ~/.config/tmux-prefix-detect.sh' + +# Manual override: prefix+T to toggle +bind T run-shell ~/.config/tmux-prefix-detect.sh # Initialize TMUX plugin manager (keep this line at the very bottom of tmux.conf) run '~/.tmux/plugins/tpm/tpm'