Add session -p <project> flag

This flag sets the session name to `<project>` where `<project>` is a
git repository to make working with multiple worktrees more streamlined.
This commit is contained in:
2026-08-10 12:30:56 +01:00
parent d9d789f585
commit 06891857e5
2 changed files with 45 additions and 1 deletions

View File

@@ -1,12 +1,48 @@
session() {
if [[ "$1" == "" ]]; then
echo "usage: session [-h] <name> [<host>]"
echo "usage: session [-h] <name> [<host>]
session -p <project>"
elif [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then
echo "usage: session [-h] <name> [<host>]
session -p <project>
Create or attach to a tmux session by name either locally on on a remote host
via ssh.
With -p create or attach to a session for <project>, a path in ~/Projects with
the prefix removed, which must be a git repository. The session is named after
the project and holds a home window plus a window at the project, named after
its default branch.
"
elif [[ "$1" == "-p" ]] || [[ "$1" == "--project" ]]; then
local project=${2%/}
if [[ "$project" == "" ]]; then
echo "$fg[red]error:$reset_color missing <project> argument"
return 1
fi
if [[ "$3" != "" ]]; then
echo "$fg[red]error:$reset_color invalid argument: $3"
return 1
fi
local repo=$HOME/Projects/$project
if [ ! -d "$repo" ]; then
echo "$fg[red]error:$reset_color no such project: $project"
return 1
fi
if ! git -C "$repo" rev-parse --git-dir &>/dev/null; then
echo "$fg[red]error:$reset_color not a git repository: $repo"
return 1
fi
# tmux rewrites `.` and `:` in session names, so mirror that to keep the
# name we create and the name we target in sync.
local name=${project//[.:]/_}
if [[ "$TMUX" == "" ]]; then
tmux new-session -As "$name" -c $HOME -e "TMUX_PROJECT=$project"
else
tmux has-session -t "=$name" &> /dev/null || \
tmux new-session -Ads "$name" -c $HOME -e "TMUX_PROJECT=$project"
tmux switch-client -t "=$name"
fi
else
local name=$1
local host=$2