forked from spencertipping/bashrc-tmux
/
bashrc-tmux
31 lines (25 loc) · 1.07 KB
/
bashrc-tmux
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/bin/bash
# Bashrc SSH-tmux wrapper | Spencer Tipping
# Licensed under the terms of the MIT source code license
# Source this just after the PS1-check to enable auto-tmuxing of your SSH
# sessions. See https://github.com/spencertipping/bashrc-tmux for usage
# information.
TMUX_SESSION=ssh-$USER
if [[ -n "$BASHRC_TMUX_SESSION" ]]; then
TMUX_SESSION="$TMUX_SESSION-$BASHRC_TMUX_SESSION"
fi
if [[ -z "$TMUX" && -n "$SSH_CONNECTION" ]] && which tmux >& /dev/null; then
if ! tmux ls -F '#{session_name}' | egrep -q "^$TMUX_SESSION$"; then
tmux new-session -s $TMUX_SESSION \; detach
fi
# Allocating a session ID: always just bump the counter. Because of
# differences between bash and zsh, working with arrays to densely pack
# session IDs is cumbersome.
session_max=$(tmux ls -F '#{session_name}' \
| egrep "^$TMUX_SESSION-[0-9]+$" \
| sed "s/^$TMUX_SESSION-//" \
| sort -rn \
| head -n1)
session_index=$((${session_max:--1} + 1))
exec tmux new-session -s $TMUX_SESSION-$session_index -t $TMUX_SESSION
fi