Skip to content

Instantly share code, notes, and snippets.

@farfanoide
Last active August 29, 2015 14:08
Show Gist options
  • Select an option

  • Save farfanoide/a54b1890f501f8faafbf to your computer and use it in GitHub Desktop.

Select an option

Save farfanoide/a54b1890f501f8faafbf to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# ------------------------------------------------------------------------------
# Description
# -----------
#
# Little script to jump between any tmux session and an always running one,
# received as an argument, ie: if you had a session 'main' and you always kept
# a named window 'Music', you could easily jump to it by calling this scipt
# like so:
# $ music_session_toggle "main:Music"
# Or even have a tmux binding for it like this one (in your ~/.tmux.conf):
# $ bind m run 'music_session_toggle main:Music'
#
# ------------------------------------------------------------------------------
# Author
# -------
#
# * Farfanoide (https://github.com/farfanoide)
#
# ------------------------------------------------------------------------------
set -e
_get_prev_session() { tmux show-environment -g previous_session | cut -d'=' -f2 ;}
_get_current_session(){ tmux display-message -p '#S' ;}
jump_session=$1; shift
if [ -n "${TMUX}" ]; then
current_session=$(_get_current_session)
if [[ ! "${jump_session}" =~ "${current_session}" ]]; then
tmux set-environment -g previous_session $(_get_current_session)
tmux switch-client -t $jump_session
else
previous_session=$(_get_prev_session)
tmux set-environment -g -u previous_session
tmux switch-client -t $previous_session
fi
else
tmux has-session -t main 2> /dev/null && tmux attach-session -t main:Music
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment