From 99b5f577a9645d46a5514a0b7289224d3ff1ad37 Mon Sep 17 00:00:00 2001 From: TheSast <27977196+TheSast@users.noreply.github.com> Date: Fri, 14 Apr 2023 19:47:46 +0200 Subject: [PATCH] [+] prefix + ctrl + @ key binding for promoting panes to sessions --- README.md | 3 ++- scripts/promote_window.sh | 43 +++++++++++++++++++++++++++++++++++++++ sessionist.tmux | 13 ++++++++++++ 3 files changed, 58 insertions(+), 1 deletion(-) create mode 100755 scripts/promote_window.sh diff --git a/README.md b/README.md index e8bc052..25ede3d 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,8 @@ To change these, add to `.tmux.conf`: set -g @sessionist-goto 'C-f' set -g @sessionist-alternate 'P' set -g @sessionist-new 'C-c' - set -g @sessionist-promote-pane 'C-1' + set -g @sessionist-promote-pane 'b' + set -g @sessionist-promote-window 'B' set -g @sessionist-join-pane 'no-key' set -g @sessionist-kill-session 'C-x' diff --git a/scripts/promote_window.sh b/scripts/promote_window.sh new file mode 100755 index 0000000..2b0a9cb --- /dev/null +++ b/scripts/promote_window.sh @@ -0,0 +1,43 @@ +#!/usr/bin/env bash + +CURRENT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" + +source "$CURRENT_DIR/helpers.sh" + +# global vars passed to the script as arguments +CURRENT_SESSION_NAME="$1" +CURRENT_WINDOW_ID="$2" +CURRENT_WINDOW_NAME="$3" +WINDOW_CURRENT_PATH="$4" + +number_of_windows() { + tmux list-windows -t "$CURRENT_SESSION_NAME" | + wc -l | + tr -d ' ' +} + +create_new_session() { + TMUX="" tmux -S "$(tmux_socket)" new-session -c "$WINDOW_CURRENT_PATH" -s "se-$CURRENT_WINDOW_NAME" -d -P -F "#{session_name}" +} + +new_session_window_id() { + local session_name="$1" + tmux list-windows -t "$session_name" -F "#{window_id}" +} + +promote_window() { + local session_name="$(create_new_session)" + local new_session_window_id="$(new_session_window_id "$session_name")" + tmux swap-window -s "$CURRENT_WINDOW_ID" -t "$new_session_window_id" + tmux kill-window -t "$new_session_window_id" + switch_to_session "$session_name" +} + +main() { + if [ "$(number_of_windows)" -gt 1 ]; then + promote_window + else + display_message "Can't promote with only one window in session" + fi +} +main diff --git a/sessionist.tmux b/sessionist.tmux index 25aae7d..2b3ac90 100755 --- a/sessionist.tmux +++ b/sessionist.tmux @@ -14,6 +14,9 @@ tmux_option_new="@sessionist-new" default_key_bindings_promote_pane="@" tmux_option_promote_pane="@sessionist-promote-pane" +default_key_bindings_promote_window="C-@" +tmux_option_promote_window="@sessionist-promote-window" + default_key_bindings_join_pane="t" tmux_option_join_pane="@sessionist-join-pane" @@ -59,6 +62,15 @@ set_promote_pane_binding() { done } +# "Promote" the current window to a new session +set_promote_window_binding() { + local key_bindings=$(get_tmux_option "$tmux_option_promote_window" "$default_key_bindings_promote_window") + local key + for key in $key_bindings; do + tmux bind "$key" run "$CURRENT_DIR/scripts/promote_window.sh '#{session_name}' '#{window_id}' '#{window_name}' '#{pane_current_path}'" + done +} + set_join_pane_secondary_bindings() { local secondary_key_table="$1" local break_pane_flag="$2" @@ -103,6 +115,7 @@ main() { set_alternate_session_binding set_new_session_binding set_promote_pane_binding + set_promote_window_binding set_join_pane_binding set_kill_session_binding }