From 64dd619da8d03b64be60621c0e1861ab5af2bcd0 Mon Sep 17 00:00:00 2001 From: Tanguy Herbron Date: Mon, 13 Nov 2023 00:14:45 +0100 Subject: [PATCH] feat(tmux): Add local project opener shortcut --- .bin/tmux-projectinator | 40 +++++++++++++++++++++++++++++----------- .tmux.conf | 1 + 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/.bin/tmux-projectinator b/.bin/tmux-projectinator index 6216d7b..f6f7dfb 100755 --- a/.bin/tmux-projectinator +++ b/.bin/tmux-projectinator @@ -4,6 +4,23 @@ set -o emacs bind '"\e": "\C-W\c-d"' +move_pane () { + 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 +} + declare -A CUSTOM_SESSIONS CUSTOM_SESSIONS[Projects]=Halia @@ -13,17 +30,18 @@ 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 +while getopts ":l" option; do + case $option in + l) + tmux send-keys 'cd $SELECTED' Enter -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 + # Close tmux popup if any + tmux display-popup -C + ;; + *) + move_pane + ;; + esac +done -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 398cf94..b167007 100644 --- a/.tmux.conf +++ b/.tmux.conf @@ -79,6 +79,7 @@ bind-key S command-prompt -p "Name of new session: " "new-session -s '%%'" unbind o bind-key o display-popup "tmux-projectinator" +bind-key O display-popup "tmux-projectinator -l" # Quick pane resizing using arrow keys bind-key -r H resize-pane -L 5