From f4b0d381485b504838aee1b9afe529822b584d28 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Tue, 26 Feb 2019 10:08:57 +0100 Subject: [PATCH] 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. --- .config/zsh/scripts/tm | 43 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100755 .config/zsh/scripts/tm diff --git a/.config/zsh/scripts/tm b/.config/zsh/scripts/tm new file mode 100755 index 0000000..21f0250 --- /dev/null +++ b/.config/zsh/scripts/tm @@ -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