aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorToby Vincent <tobyv13@gmail.com>2023-03-09 18:28:04 -0600
committerToby Vincent <tobyv13@gmail.com>2023-03-09 18:28:04 -0600
commitdbaadba4e453197929b29c039216ef8f712f7732 (patch)
treef99a2463b4bcb780aca029c2279fc56dc3a2e66f
parent90bf5e428bdef16eca8ad989ac3b6a64243cc305 (diff)
feat(tmux): impl ssh host switching
It works by creating a new tmux server on a specific socket (`ssh`) that runs an additional config to change the prefix key and hide the status bar. It then uses a hook to reattach to the local session if the pane, and therefor ssh process, is exited.
-rw-r--r--tmux/.config/tmux/ssh.conf10
-rw-r--r--tmux/.config/tmux/tmux.conf8
-rwxr-xr-xtmux/.local/bin/tmux-ssh49
3 files changed, 45 insertions, 22 deletions
diff --git a/tmux/.config/tmux/ssh.conf b/tmux/.config/tmux/ssh.conf
new file mode 100644
index 0000000..4bee96b
--- /dev/null
+++ b/tmux/.config/tmux/ssh.conf
@@ -0,0 +1,10 @@
+# vim: ft=tmux
+
+set-option -g remain-on-exit on
+set-option -g prefix C-b
+set-option -g status off
+
+bind-key -n C-a send-prefix
+
+set-hook -g pane-died "detach -E 'tmux -L ssh kill-pane; tmux attach'"
+
diff --git a/tmux/.config/tmux/tmux.conf b/tmux/.config/tmux/tmux.conf
index 5967869..1bcef1f 100644
--- a/tmux/.config/tmux/tmux.conf
+++ b/tmux/.config/tmux/tmux.conf
@@ -15,10 +15,10 @@ set -g base-index 1
set -g pane-base-index 1
set -g detach-on-destroy off
+bind-key -n C-s run-shell ~/.local/bin/tmux-ssh
+
bind-key -N "Switch last session" A switch-client -l
bind-key -N "Clock" T clock-mode
-bind-key -N "Host: new" s run-shell ~/.local/bin/tmux-ssh
-# bind-key -N "Host: switch" S run-shell ~/.local/bin/tmux-ssh
bind-key -N "Session: new" f run-shell ~/.local/bin/tmux-sessionizer
bind-key -N "Session: switch" F run-shell ~/.local/bin/tmux-sessions
bind-key -N "Window: new" t new-window
@@ -45,3 +45,7 @@ if "test ! -d $XDG_CONFIG_HOME/tmux/plugins/tpm" \
&& $XDG_CONFIG_HOME/tmux/plugins/tpm/bin/install_plugins'"
run '$XDG_CONFIG_HOME/tmux/plugins/tpm/tpm'
+
+%if #{m:*ssh,#{socket_path}}
+source "$XDG_CONFIG_HOME/tmux/ssh.conf"
+%endif
diff --git a/tmux/.local/bin/tmux-ssh b/tmux/.local/bin/tmux-ssh
index 335e75c..b1875e2 100755
--- a/tmux/.local/bin/tmux-ssh
+++ b/tmux/.local/bin/tmux-ssh
@@ -4,30 +4,39 @@
# shellcheck disable=2016,2089
if [ "$#" -eq 0 ]; then
- ATTACHED=$(tmux display -p '#{session_path}')
+ ATTACHED=$(tmux display -p '#S' 2>/dev/null)
+ HOST=$(tmux display -p '#{host}' 2>/dev/null)
- # shellcheck disable=2046
- set -- $(
- grep -P '^[Hh]ost ([^*]+)$' "$HOME/.ssh/config" | grep -Po ' \K(\w+)' |
- sed "\|^$HOST\$|d" | sed "\|^${ATTACHED#SSH_}\$|d" | sort -u |
- timestamp.sh --type="ssh" --format="{}:{1}" | sort -r | cut -d':' -f2 |
- fzf-tmux "$FZF_TMUX_OPTS" -- --multi --print-query -d/ --with-nth -1 |
- tr -s '\n' ' '
- ) && [ $# -gt 1 ] && shift
+ set -- "$(
+ (
+ tmux display -p '#{?#{m:*ssh,#{socket_path}},localhost,}' 2>/dev/null
+ grep -P '^[Hh]ost ([^*]+)$' "$HOME/.ssh/config" | grep -Po ' \K(\w+)' | sed "\|^$HOST\$|d" |
+ sed "\|^${ATTACHED}\$|d" |
+ sort -u |
+ timestamp.sh --type="ssh" --format="{}:{1}" |
+ sort -r |
+ cut -d':' -f2
+ ) |
+ sed '\|^$|d' |
+ fzf-tmux -p20%,20% -- --print-query -d/ --with-nth -1 |
+ tail -1
+ )"
fi
-while [ $# -gt 0 ]; do
- ssh_host="$1"
- shift
+if [ -z "$1" ]; then
+ exit 0
+fi
- name="SSH_$ssh_host"
- if ! tmux has-session -t "$name" 2>/dev/null; then
- tmux new-session -ds "$name" ssh -t "$ssh_host" # 'tmux new -As $USER'
+if [ "$1" = "localhost" ] || [ "$1" = "$HOST" ]; then
+ tmux detach -E 'tmux attach'
+else
+ if ! tmux -L ssh has-session -t "$1"; then
+ tmux -L ssh new-session -ds "$1" ssh -t "$1" zsh -l -c tmux new -As "$USER"
fi
-done
-if [ -z "$TMUX" ] && [ -z "$name" ]; then
- tmux attach-session -t "$name"
-elif tmux has-session -t="$name" 2>/dev/null; then
- tmux switch-client -t "$name"
+ if [ -z "$TMUX" ]; then
+ tmux -L ssh attach -t "$1"
+ else
+ tmux detach -E "tmux -L ssh attach -t $1"
+ fi
fi