Skip to content

Commit

Permalink
[+] prefix + ctrl + @ key binding for promoting panes to sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSast committed Apr 14, 2023
1 parent b7cd932 commit 99b5f57
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down
43 changes: 43 additions & 0 deletions scripts/promote_window.sh
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions sessionist.tmux
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 99b5f57

Please sign in to comment.