Skip to content

Instantly share code, notes, and snippets.

@jfear
Created January 17, 2018 16:25
Show Gist options
  • Select an option

  • Save jfear/cfb9f97e30f997d79f5a90bb75324a61 to your computer and use it in GitHub Desktop.

Select an option

Save jfear/cfb9f97e30f997d79f5a90bb75324a61 to your computer and use it in GitHub Desktop.
Tmux function to look for .tmux file
# Tmux
function tmux() {
# Make sure even pre-existing tmux sessions use the latest SSH_AUTH_SOCK.
# (Inspired by: https://gist.github.com/lann/6771001)
local SOCK_SYMLINK=~/.ssh/ssh_auth_sock
if [ -r "$SSH_AUTH_SOCK" -a ! -L "$SSH_AUTH_SOCK" ]; then
ln -sf "$SSH_AUTH_SOCK" $SOCK_SYMLINK
fi
# If provided with args, pass them through.
if [[ -n "$@" ]]; then
env SSH_AUTH_SOCK=$SOCK_SYMLINK tmux "$@"
return
fi
# Check for .tmux file (poor man's Tmuxinator).
SESSION_NAME=$(basename "$(pwd)")
if tmux has-session -t $SESSION_NAME 2> /dev/null; then
# Attach to existing session
env SSH_AUTH_SOCK=$SOCK_SYMLINK tmux attach -t $SESSION_NAME
elif [ -f .tmux ]; then
# Prompt the first time we see a given .tmux file before running it.
local DIGEST="$(openssl sha -sha512 .tmux)"
if ! grep -q "$DIGEST" ~/.tmux.digests 2> /dev/null; then
cat .tmux
echo 'Trust (and run) this .tmux file? (t = trust, otherwise = skip) '
read REPLY
echo
if [[ $REPLY =~ ^[Tt]$ ]]; then
echo "$DIGEST" >> ~/.tmux.digests
. .tmux
return
fi
else
. .tmux
return
fi
else
# Create new session
env SSH_AUTH_SOCK=$SOCK_SYMLINK tmux new -A -s "$SESSION_NAME"
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment