Skip to content

Instantly share code, notes, and snippets.

@lucaspar
Last active July 22, 2024 21:27
Show Gist options
  • Save lucaspar/94b7c0043f2403d8188ed1b84387a50c to your computer and use it in GitHub Desktop.
Save lucaspar/94b7c0043f2403d8188ed1b84387a50c to your computer and use it in GitHub Desktop.
Helpers for `tmux` to make creating and re-attaching to sessions more sensible.
# Usage examples:
# Create a session and immediately attach to it:
# tm mysess
# Detach using the tmux prefix + D, as usual.
# List sessions:
# tl
# Re-attach to mysess with the exact same line:
# tm mysess
# Kill a session without attaching:
# tk mysess
# tmux wrapper to re-attach to a tmux session by name
# If the session does not exist, it will be created.
# If the session exists, the terminal will be re-attached.
# If no session name is provided, the latest attached session is then re-attached.
function tm() {
# if already in a tmux session, do nothing
if [ -n "$TMUX" ]; then
echo "Already in tmux session $TMUX"
return
fi
# if no argument is provided, try to re-attach to the last session
if [ -z "$1" ]; then
# try to grab the name of the most recently created session
TMUX_LAST_SESSION=$(tmux ls -F "#{session_created} #{session_name}: #{session_windows} windows (created #{t:session_created}){?session_grouped, (group ,}#{session_group}#{?session_grouped,),}#{?session_attached, (attached),}" 2>/dev/null | sort -Vr | sed 's/\S*\s//' | head -n 1 | awk '{print $1}')
if [ -n "$TMUX_LAST_SESSION" ] && [[ $TMUX_LAST_SESSION != *"no server"* ]]; then
echo "Re-attaching to last session: $TMUX_LAST_SESSION"
tmux attach -t "$TMUX_LAST_SESSION"
return
fi
echo "No tmux sessions found. Use: 'tm <SESSION_NAME>' to create a new one."
return
# if there is an argument, try to attach to it or create a session with that name
else
echo "Attaching to session $1..."
tmux attach -t "$1" 2>/dev/null || (echo "Did not find session '$1', creating one..." && tmux new -s "$1")
return
fi
}
# prettier tmux ls
alias tl='tmux ls -F "?#{session_name}?#{session_windows} window(s)?cr. #{t:session_created} #{?session_grouped, ?group ,}#{session_group}#{?session_grouped,,} #{?session_attached, ?attached,}" | column -s ? -t -o " | "'
# kill a named session
alias tk='tmux kill-session -t'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment