Skip to content

Instantly share code, notes, and snippets.

@ThiefMaster
Last active August 29, 2015 13:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ThiefMaster/9327191 to your computer and use it in GitHub Desktop.
Save ThiefMaster/9327191 to your computer and use it in GitHub Desktop.
#!/bin/bash
# tmux-smartsplit
# open a new tmux (split-)window which will automatically
# continue the existing ssh session if one exists
SIP=$(tmux display-message -p "#S:#I:#P")
PTY=$(tmux server-info |
egrep flags=\|bytes |
awk '/windows/ { s = $2 }
/references/ { i = $1 }
/bytes/ { print s i $1 $2 } ' |
grep "$SIP" |
cut -d: -f4)
PTS=${PTY#/dev/}
PID=$(ps h -eao pid,tty,command --forest | awk '$2 == "'$PTS'" {print $1; exit}')
CHILD=$(cat /proc/[0-9]*/stat | awk '$4 == '$PID' {print $1; exit}')
NEW_WIN_CMD=""
if [[ "$CHILD" != "" ]]; then
NEW_WIN_CMD=$(python3.3 -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
case "$1" in
h)
tmux splitw -h "$NEW_WIN_CMD"
;;
v)
tmux splitw -v "$NEW_WIN_CMD"
;;
*)
tmux neww "$NEW_WIN_CMD"
;;
esac
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