Created
October 31, 2024 07:15
-
-
Save itsdonnix/e15444f9ddf8c48d7c96ebd67b99430b to your computer and use it in GitHub Desktop.
Start tmux automatically on zsh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Let's kick things off by checking if we're already in a tmux session | |
if [[ ! -n $TMUX ]]; then | |
# Gather the mystical details of existing tmux sessions | |
session_details="$(tmux list-sessions)" | |
# Uncomment this block to create a new session if no sessions exist | |
# if [[ -z "$session_details" ]]; then | |
# tmux new-session # Bring a new session into existence! | |
# fi | |
# Presenting the user with choices to manage their tmux experience: | |
# - Attach to an existing session if there are any | |
# - Create a new tmux session for fresh adventures | |
# - Or, take the plunge and start without tmux altogether! | |
create_new_session="Create new tmux session" | |
start_without_tmux="Start without tmux" | |
choices="${create_new_session}\n${start_without_tmux}" # Initialize the choice menu | |
# If there are existing sessions, add them to the choices | |
if [[ ! -z "$session_details" ]]; then | |
choices="$session_details\n$choices" # Add existing sessions to the mix | |
fi | |
# Use fzf to present the choices in a friendly way and get the user's selection | |
choice="$(echo -e $choices | fzf | cut -d: -f1)" | |
# Fetch the current session IDs for validation | |
session_ids="$(tmux list-sessions -F '#S')" | |
# Check if the choice is valid: alphanumeric and exists in session IDs | |
if expr "$choice" : "^[a-zA-Z0-9]*$" >&/dev/null && echo "$session_ids" | grep -q -w "$choice"; then | |
# If valid, let's attach to the chosen session like a pro | |
tmux attach-session -t "$choice" | |
elif [[ "$choice" = "${create_new_session}" ]]; then | |
# Time to create a brand new session—let the magic happen! | |
tmux new-session | |
elif [[ "$choice" = "${start_without_tmux}" ]]; then | |
# Starting without tmux? You do you! Nothing to do here. | |
: | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You need to install
fzf
first: FZF