Skip to content

Instantly share code, notes, and snippets.

@ThiefMaster
Last active August 29, 2015 13:56

Revisions

  1. ThiefMaster revised this gist Mar 3, 2014. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions tmux.conf
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,2 @@
    bind '"' run-shell -b 'tmux-smartsplit v'
    bind % run-shell -b 'tmux-smartsplit h'
    bind c run-shell -b 'tmux-smartsplit'
    bind % run-shell -b 'tmux-smartsplit h'
  2. ThiefMaster created this gist Mar 3, 2014.
    34 changes: 34 additions & 0 deletions tmux-smartsplit.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    #!/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
    3 changes: 3 additions & 0 deletions tmux.conf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    bind '"' run-shell -b 'tmux-smartsplit v'
    bind % run-shell -b 'tmux-smartsplit h'
    bind c run-shell -b 'tmux-smartsplit'