From 3b05bf691e35e0d093676b7704bd186a44dab6ff Mon Sep 17 00:00:00 2001 From: Tanguy Herbron Date: Sat, 11 Nov 2023 14:13:02 +0100 Subject: [PATCH] feat(tmux): Add project opener --- .bin/tmux-projectinator | 29 +++++++++++++++++++++++++++++ .tmux.conf | 3 +++ 2 files changed, 32 insertions(+) create mode 100755 .bin/tmux-projectinator diff --git a/.bin/tmux-projectinator b/.bin/tmux-projectinator new file mode 100755 index 0000000..6216d7b --- /dev/null +++ b/.bin/tmux-projectinator @@ -0,0 +1,29 @@ +#!/bin/sh + +# Quit on escape +set -o emacs +bind '"\e": "\C-W\c-d"' + +declare -A CUSTOM_SESSIONS +CUSTOM_SESSIONS[Projects]=Halia + +SELECTED=$(find . -name .git -type d -prune -not -path "./.*" 2>/dev/null | sed 's/\/.git//' | cut -d'/' -f2- | fzf) + +ROOT=$(echo $SELECTED | cut -d '/' -f1) +PROJECT=$(echo $SELECTED | rev | cut -d '/' -f1 | rev) +SESSION_NAME=$([ -z "${CUSTOM_SESSIONS[$ROOT]}" ] && echo "$ROOT" || echo "${CUSTOM_SESSIONS[$ROOT]}") + +tmux has-session -t $SESSION_NAME 2>/dev/null +if [ $? != 0 ]; then + tmux new-session -s $SESSION_NAME -d -n $PROJECT #2>/dev/null +fi + +tmux has-session -t $SESSION_NAME:$PROJECT 2>/dev/null +if [ $? != 0 ]; then + tmux new-window -t $SESSION_NAME: -n $PROJECT -d -c $SELECTED +fi + +tmux switch-client -t $SESSION_NAME:$PROJECT + +# Close tmux popup if any +tmux display-popup -C diff --git a/.tmux.conf b/.tmux.conf index 8b91963..398cf94 100644 --- a/.tmux.conf +++ b/.tmux.conf @@ -77,6 +77,9 @@ bind-key W command-prompt -p "Name of new window: " "new-window -n '%%'" bind-key S command-prompt -p "Name of new session: " "new-session -s '%%'" +unbind o +bind-key o display-popup "tmux-projectinator" + # Quick pane resizing using arrow keys bind-key -r H resize-pane -L 5 bind-key -r J resize-pane -D 5