Last active
August 29, 2015 13:56
-
-
Save ThiefMaster/9327191 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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