Update tmux-sessionizer and binds for session management

This commit is contained in:
david 2025-04-04 16:55:39 -05:00
parent 595394f83a
commit 6baf822f69
2 changed files with 23 additions and 22 deletions

View File

@ -21,10 +21,13 @@ bind -r l select-pane -R
bind r source-file "~/.config/tmux/tmux.conf" \; display-message "tmux.conf reloaded"
# kill the current session
bind Q rename-session zzzz-temp-kill\; switch-client -p\; kill-session -t zzzz-temp-kill
bind-key Q rename-session zzzz-temp-kill\; switch-client -p\; kill-session -t zzzz-temp-kill
bind-key W kill-session
# use tmux-sessionizer in place of f find
bind-key -r f run-shell "tmux neww ~/.local/bin/tmux-sessionizer"
# custom find using tmux-sessionizer
unbind-key f
bind-key f run-shell "tmux neww ~/.local/bin/tmux-sessionizer existing"
bind-key F run-shell "tmux neww ~/.local/bin/tmux-sessionizer"
# bind -r D neww -c "#{pane_current_path}" "[[ -e TODO.md ]] && $EDITOR TODO.md || $EDITOR ~/personal/dev/todo.md"

View File

@ -4,35 +4,33 @@ switch_to() {
[[ -z $TMUX ]] && tmux attach-session -t $1 || tmux switch-client -t $1
}
tmux_session_exists() {
[[ -n $(pgrep tmux) ]] && tmux list-sessions | grep -q "^$1:"
}
hydrate() {
local tmux_hydrate_path="$HOME/.local/bin/.tmux-session-hydrate"
[[ -f $2/.tmux-session-hydrate ]] && tmux_hydrate_path="$2/.tmux-session-hydrate"
[[ -f $tmux_hydrate_path ]] && tmux send-keys -t $1 "source $tmux_hydrate_path" c-M
}
local name_regex="^\([-_A-Za-z0-9]*\):.*$"
local existing_sessions=$([[ -n $(pgrep tmux) ]] && tmux list-sessions | sed "s/$name_regex/\1/" || echo '')
local search_dirs=(
$HOMEBOX
$DEVDIR
$DEVDIR/git/*
)
local selected_path=''
[[ $# -eq 1 ]] && selected_path=$1 ||
selected_path=$(
find $search_dirs -mindepth 1 -maxdepth 1 -type d |
(echo 'hub' && cat) |
fzf
)
[[ -z $selected_path ]] && exit 0
local target_name=''
local target_path=''
local selected_name=''
[[ "$selected_path" = "hub" ]] && selected_name="hub" && selected_path=$HOME ||
selected_name=$(basename "$selected_path" | tr . _)
[[ $# -eq 1 ]] && target_path=$1 ||
target_path=$(find $search_dirs -mindepth 1 -maxdepth 1 -type d | fzf)
! (tmux_session_exists $selected_name) &&
tmux new-session -d -s $selected_name -c $selected_path &&
hydrate $selected_name $selected_path
switch_to $selected_name
if [[ $target_path = "existing" ]]; then target_name=$(echo $existing_sessions | fzf);
elif [[ $target_path = "hub" ]]; then target_name="hub" && target_path="$HOME";
elif [[ -n $target_path ]]; then target_name=$(basename "$target_path" | tr . _);
fi
[[ -z $target_name ]] && exit 0
! (echo $existing_sessions | grep -q "$target_name") &&
tmux new-session -d -s $target_name -c $target_path &&
hydrate $target_name $target_path
switch_to $target_name