Skip to content

Instantly share code, notes, and snippets.

@jphalip
Created January 18, 2025 22:45
Show Gist options
  • Save jphalip/13cf44f26033fcc30507cb08e9320f1b to your computer and use it in GitHub Desktop.
Save jphalip/13cf44f26033fcc30507cb08e9320f1b to your computer and use it in GitHub Desktop.
Quality-of-life shell functions and aliases for Tmux
# If no tmux sessions exist, starts a new session
# If sessions exist, attaches and shows session picker
function tm() {
tmux ls &> /dev/null
if [ $? -eq 1 ]
then
# No sessions exist, start new one
tmux
else
# Sessions exist, attach and show session picker
# choose-tree -s shows only sessions (not windows/panes)
tmux attach\; choose-tree -s
fi
}
# Creates a new tmux session with the specified name
# If already in tmux, detaches from current session before creating new one
function tm-create() {
if [[ $# -eq 0 ]]; then
# Show usage if no session name provided
echo "Arguments: <session_name>"
return
fi
if [[ -n "$TMUX" ]]; then
# If we're already inside a tmux session,
# detach first, then create the new session
# Using -E ensures the new session starts after detaching
tmux detach-client -E "tmux new-session -s $1"
else
# If not in tmux, create new session directly
tmux new-session -s $1
fi
}
# Attaches to a tmux session
# If no session name provided, attaches to the last active session
function tm-attach() {
if [ -z "$1" ]
then
tmux a # Attach to last active session
else
tmux a -t $1 # Attach to specific session
fi
}
# Renames a tmux session
# Can rename either current session or a specified session
function tm-rename() {
if [[ $# -eq 0 ]]; then
# Show usage if no arguments provided
echo "Usage: tm-rename [current-session-name] <new-session-name>"
echo " If only <new-session-name> is provided, renames the current session"
return 1
elif [[ $# -eq 1 ]]; then
# With one argument, rename current session
tmux rename-session $1
else
# With two arguments, rename specified session
tmux rename-session -t $1 $2
fi
}
# Deletes (kills) the specified tmux session
function tm-delete() {
tmux kill-session -t $1
}
# Sets the title of the current tmux pane
function tm-title() {
tmux select-pane -T "$1"
}
alias tm-detach="tmux detach"
alias tm-kill="tmux kill-server"
alias tm-ls="tmux ls"
alias tm-source="tmux source-file ~/.tmux.conf"
alias tm-edit="${EDITOR} ~/.tmux.conf"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment