Add tm command to dynamically attach to tmux sessions

If a session is running, run a detached session. (Detach
all other clients.) If no session with the name is
running, create a new session and attach. Session name
can be passed in, or defaults to current directory name.
This commit is contained in:
Marty Oehme 2019-02-26 10:08:57 +01:00
parent 26f06d51b4
commit f4b0d38148

43
.config/zsh/scripts/tm Executable file
View file

@ -0,0 +1,43 @@
#!/bin/sh
#
# Attach to a tmux session, or create it if it doesnt exist
if [ -e $XDG_CONFIG_HOME/tmux/tmux.conf ]; then
start="tmux -f $XDG_CONFIG_HOME/tmux/tmux.conf"
else
start=tmux
fi
path_name="$(basename "$PWD" | tr . -)"
curr_dir=${1-$path_name}
session_name=${1:-$curr_dir}
_not_in_tmux() {
[ -z "$TMUX" ]
}
_session_exists() {
$start has-session -t "$session_name"
}
_create_detached_session() {
(TMUX='' $start new-session -Ad -s "$session_name")
}
_get_session_name() {
#${1:-$session_name}
return "blubb"
}
_attach_or_create() {
if _not_in_tmux; then
$start new-session -As "$session_name"
else
if ! _session_exists; then
_create_detached_session
fi
$start switch-client -t "$session_name"
fi
}
_attach_or_create