Skip to content

Instantly share code, notes, and snippets.

@codequokka
Last active May 8, 2024 09:00
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codequokka/f058216231d0d251536e367719f67296 to your computer and use it in GitHub Desktop.
Save codequokka/f058216231d0d251536e367719f67296 to your computer and use it in GitHub Desktop.
Start tmux automatically on zsh
# Start the tmux session if not alraedy in the tmux session
if [[ ! -n $TMUX ]]; then
# Get the session IDs
session_ids="$(tmux list-sessions)"
# Create new session if no sessions exist
if [[ -z "$session_ids" ]]; then
tmux new-session
fi
# Select from following choices
# - Attach existing session
# - Create new session
# - Start without tmux
create_new_session="Create new session"
start_without_tmux="Start without tmux"
choices="$session_ids\n${create_new_session}:\n${start_without_tmux}:"
choice="$(echo $choices | fzf | cut -d: -f1)"
if expr "$choice" : "[0-9]*$" >&/dev/null; then
# Attach existing session
tmux attach-session -t "$choice"
elif [[ "$choice" = "${create_new_session}" ]]; then
# Create new session
tmux new-session
elif [[ "$choice" = "${start_without_tmux}" ]]; then
# Start without tmux
:
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment