Skip to content

Instantly share code, notes, and snippets.

@andreypopp

andreypopp/s.sh Secret

Last active August 15, 2021 17:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreypopp/2f565b9ed7bad480d69cac4ba440151d to your computer and use it in GitHub Desktop.
Save andreypopp/2f565b9ed7bad480d69cac4ba440151d to your computer and use it in GitHub Desktop.
tmux-attach-session() {
local session="$1"
if tmux has-session -t="$session" 2> /dev/null; then
if [ -n "$TMUX" ]; then
tmux switch-client -t="$session"
else
tmux attach -t="$session"
fi
else
if [ -n "$TMUX" ]; then
tmux new -d -s "$session"
tmux switch-client -t="$session"
else
tmux new -d -s "$session"
tmux rename-window -t="$session" "main"
tmux attach -t="$session"
fi
fi
}
#
# s - provides UI for switching between tmux sessions
#
# List all available sessions:
#
# % s
#
# will present a fzf-based menu to switch to one of the existing session, if
# entered fzf query won't match any of sessions then it will be used as session
# name to create a new one and attach to it.
#
# Switch to a specified session by name:
#
# % s NAME
#
# similarly, if session NAME does not exist it will be created first.
#
s() {
if [ -n "$1" ]; then
tmux-attach-session "$1"
else
if tmux has-session 2> /dev/null; then
local session
session=$(
tmux list-sessions -F "#{session_name}" \
| fzf \
--inline-info \
--reverse \
--height=20 \
--bind=enter:replace-query+print-query \
--prompt "tmux> " \
| cut -f1 -d:
)
if [ "$session" != "" ]; then
tmux-attach-session "$session"
fi
else
local session
session=$(
echo -n '' |\
fzf \
--inline-info \
--reverse \
--height=20 \
--bind=enter:replace-query+print-query \
--prompt "tmux> "
)
if [ "$session" != "" ]; then
tmux-attach-session "$session"
fi
fi
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment