Skip to content

Instantly share code, notes, and snippets.

@ThiefMaster
Last active April 27, 2017 07:36
Show Gist options
  • Save ThiefMaster/e074ef6f690b49279844f5075388407b to your computer and use it in GitHub Desktop.
Save ThiefMaster/e074ef6f690b49279844f5075388407b to your computer and use it in GitHub Desktop.
Automatically ssh to the current pane's target if applicable
#!/bin/bash
# tmux-smartsplit
# open a new tmux (split-)window which will automatically
# continue the existing ssh session if one exists
PID=$(tmux display-message -p -F "#{pane_pid}")
if [[ $(readlink /proc/$PID/exe) == $(which ssh) ]]; then
CHILD=$PID
else
CHILD=$(cat /proc/[0-9]*/stat | awk '$4 == '$PID' {print $1; exit}')
fi
CMD=""
if [[ "$CHILD" != "" ]]; then
CMD="$(python3.4 -c 'import shlex, sys; parts = list(filter(None, open(sys.argv[1]).read().split("\0"))); print(" ".join(map(shlex.quote, parts)) if parts[0] == "ssh" else "")' "/proc/$CHILD/cmdline")"
fi
cd "$(tmux display-message -p '#{pane_current_path}')"
case "$1" in
h)
tmux splitw -h -c "#{pane_current_path}"
;;
v)
tmux splitw -v -c "#{pane_current_path}"
;;
w)
tmux neww -c "#{pane_current_path}"
;;
esac
if [[ "$CMD" != "" ]]; then
ID=$(tmux display-message -p -F "#{pane_id}")
tmux send-keys -t "$ID" C-l
tmux send-keys -l -t "$ID" " $CMD"
tmux send-keys -t "$ID" Enter
fi
bind '"' run-shell -b 'tmux-smartsplit v'
bind % run-shell -b 'tmux-smartsplit h'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment