session() { if [[ "$1" == "" ]]; then echo "usage: session [-h] [] session -p " elif [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then echo "usage: session [-h] [] session -p 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 , 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 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 if [[ "$3" != "" ]]; then echo "$fg[red]error:$reset_color invalid argument: $3" return 1 fi declare -A hosts if [ -f ~/.config/session ]; then source ~/.config/session fi local url=$hosts[$host] host=${url:-$host} if [[ "$TMUX" == "" ]]; then local cmd="tmux new-session -As $name" if [[ "$host" != "" ]]; then cmd="ssh $host -t $cmd" fi eval $cmd else if [[ "$host" != "" ]]; then echo "$fg[red]error:$reset_color not allowed inside tmux session" return 1 fi tmux list-sessions | grep "$name:" &> /dev/null || \ tmux new-session -Ads $name -c $HOME tmux switch-client -t $name fi fi }