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

@@ -4,6 +4,8 @@ __session_sessions() {
list() { list() {
for item in $HOME/.local/share/tmux/layouts/session-*; do for item in $HOME/.local/share/tmux/layouts/session-*; do
item=${item#$HOME/.local/share/tmux/layouts/} item=${item#$HOME/.local/share/tmux/layouts/}
# session-project is parameterized by -p, not a session of its own
[ "$item" = session-project ] && continue
echo ${item#session-} echo ${item#session-}
done done
} }
@@ -12,6 +14,10 @@ __session_sessions() {
_describe 'session' sessions _describe 'session' sessions
} }
__session_projects() {
_files -W $HOME/Projects -/
}
__session_hosts() { __session_hosts() {
list() { list() {
declare -A hosts declare -A hosts
@@ -28,5 +34,7 @@ __session_hosts() {
} }
_arguments \ _arguments \
'(- *)'{-h,--help}'[Show usage information]' \
'(- *)'{-p,--project}'[Create or attach to a session for a project in ~/Projects]:project:__session_projects' \
':session:__session_sessions' \ ':session:__session_sessions' \
':host:__session_hosts' ':host:__session_hosts'

View File

@@ -1,12 +1,48 @@
session() { session() {
if [[ "$1" == "" ]]; then if [[ "$1" == "" ]]; then
echo "usage: session [-h] <name> [<host>]" echo "usage: session [-h] <name> [<host>]
session -p <project>"
elif [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then elif [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then
echo "usage: session [-h] <name> [<host>] 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 Create or attach to a tmux session by name either locally on on a remote host
via ssh. 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 else
local name=$1 local name=$1
local host=$2 local host=$2