Skip to content

Instantly share code, notes, and snippets.

@bartman
Last active October 14, 2022 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bartman/de9aaac6dcd60bd127c92db0c9cc7f75 to your computer and use it in GitHub Desktop.
Save bartman/de9aaac6dcd60bd127c92db0c9cc7f75 to your computer and use it in GitHub Desktop.
tmuxify
#!/bin/zsh
#autoload -U
#vim: set ft=zsh
local use_exec=false
local base=${0##*/}
local sname=${base#tmux}
function err() { print -P >&2 - "%B%F{red}$@" ; }
while [[ "${1:0:1}" == "-" ]] ; do
case "$1" in
-h)
cat <<-END
$0 [ --sub | --exec ] [ -l ] [ -s session-name ]
END
return
;;
--sub)
shift
use_exec=false
;;
--exec)
shift
use_exec=true
;;
-l)
tmux list-sessions
return
;;
-s)
shift
if [[ -z "$1" ]] ; then
err "-t takes a name of the session"
return 1
fi
sname=$1
;;
*)
err "option $1 is not supported"
return 1
;;
esac
done
local existing=$( tmux list-sessions 2>/dev/null | awk -F: '/^'"$sname"'/ { print $1 ; exit }' )
local unatached=$( tmux list-sessions 2>/dev/null | awk -F: '/^'"$sname"'/ && $0 !~ /attached/ { print $1 ; exit }' )
if $use_exec ; then
if [[ -n "$unatached" ]] ; then
exec tmux attach-session -t $unatached
elif [[ -n "$existing" ]] ; then
exec tmux new -t $existing 2>/dev/null
fi
exec tmux new -s $sname
else
( [[ -n "$unatached" ]] && tmux attach-session -t $unatached ) \
|| ( [[ -n "$existing" ]] && tmux new -t $existing ) \
|| tmux new -s $sname
fi
#!/bin/zsh
if [[ -n "$SSH_CONNECTION" ]] ; then
if [[ -z "$TMUX" ]] ; then
if ( whence tmux >/dev/null 2>&1 ) ; then
# ~/.zsh/func/tmux0
autoload -U tmux0
exec tmux0 --exec
exec tmux0
fi
fi
fi

This gist causes automatic connection to a running tmux session. On first connection a tmux session is created.

There are two parts:

  1. .zsh/rc/S01_tmuxify - this file is sourced as part of .zshrc (in my setup)
  2. .zsh/func/tmux0 - this is a zsh function that makes sure a tmux session is available and attaches to it
@bartman
Copy link
Author

bartman commented Oct 14, 2022

Here is the bash to do "tmuxify"

#!/bin/bash

if [ "$PS1" ] ; then

tmux0() {
        local existing=$( tmux list-sessions 2>/dev/null | awk -F: '{ print $1 ; exit }' )
        local unatached=$( tmux list-sessions 2>/dev/null | awk -F: '$0 !~ /attached/ { print $1 ; exit }' )

        if [[ "$1" = "--exec" ]] ; then
                if [[ -n "$unatached" ]] ; then
                        exec tmux attach-session -t $unatached
                elif [[ -n "$existing" ]] ; then
                        exec tmux new -t $existing 2>/dev/null
                fi
                exec tmux new
        else
                ( [[ -n "$unatached" ]] && tmux attach-session -t $unatached ) \
                        || ( [[ -n "$existing" ]] && tmux new -t $existing ) \
                        || tmux new
        fi
}

if [ -n "$SSH_CONNECTION" ] ; then
        if [ -z "$TMUX" ] ; then
                if ( which tmux >/dev/null 2>&1 ) ; then
                        tmux0 --exec
                        tmux0
                fi
        fi
fi

fi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment