0

there are answers to parts of this question dotted around, but I can't figure out how to put them together. I want to, for example, synchronize panes 0, 1, 2, and 3, but not 4, 5, 6, and 7; and then undo that same action with the same key. possibly relavant facts that I can't assimilate into an answer:

select-pane -dt 4 and select-pane -et 4 will disable and enable plane 4 context
set-window-option synchronize-panes will toggle syncronize-panes on and off with the same key, and
#{?pane_synchronized,on,off} will tell you if the panes are currently synchronized context
bind-key n command-1 \; command-2 will bind key n to run command-1 and command-2 context
this question comes close but it only runs one command, while I want to bind a key to synchronize panes 0, 1, 2, and 3 until I hit that key again.

guest4308
  • 189

1 Answers1

0

You might be able to use/modify the script here: https://github.com/rollerd/paneblocker

You could probably setup a tmux bind to call the script with your requirements.

#!/usr/bin/env bash

num_re='^[0-9]+$' enabled_mode=false enable_all_mode=false disabled_mode=false invert_mode=false only_mode=false group_name="None" group_action=false

Gather all existing pane indexes into pane_array

IFS=": " read -r -a pane_array <<< $(tmux list-panes | awk '{print $1}' | tr ':\n' ' ')

Help() {

Display Help

echo "paneblock tmux help" echo echo "Syntax: paneblock [-h|d|e|i|a|o|g:] [group_name] <pane index(es)>" echo "options:" echo "h Print this help." echo "d disable input on <pane index>" echo "e enable input on <pane index>" echo "i invert selection (requires -e or -d flag)" echo "a enable input on all panes" echo "g create/modify group" echo echo "Examples:" echo "Disable input on panes 0,4,6: `paneblock -d 0 4 6`" echo "Enable input on pane 4: `paneblock -e 4`" echo "Re-enable input on all panes: `paneblock -a`" echo "Disable input on all panes EXCEPT 0,4,6: `paneblock -id 0 4 6`" echo "Create group and add panes: `paneblock -g <group_name> 0 3 4`" echo "Add pane(s) to existing group: `paneblock -g <group_name> + 5 6`" echo "Enable panes in group: `paneblock -eg <group_name>`" echo "Disable panes in group: `paneblock -dg <group_name>`" echo "Enable panes only in group (disable others): `paneblock -og <group_name>`" echo exit 0 }

showopts() { while getopts 'hg:iedao' opt;do case "$opt" in h) Help ;; g) group_name="$OPTARG" ;; i) invert_mode=true ;; e) enabled_mode=true ;; d) disabled_mode=true ;; a) enable_all_mode=true ;; o) only_mode=true ;; esac done return $OPTIND }

main() { # Set pane title bars tmux set -g pane-border-status top tmux set -g pane-border-format "#[fg=black, bg=green] #{pane_index} #{@groupid} #{@custom_pane_title}"

showopts $@
option_index=&quot;${@:$OPTIND}&quot; 
# Group mode branch
if [ $group_name != &quot;None&quot; ];then
    if [ $enabled_mode = true ];then
        enable_group $group_name
    elif [ $disabled_mode = true ];then
        disable_group $group_name
    elif [ $only_mode = true ];then
        enable_only_group $group_name
    else
        group_panes $option_index
    fi
# Invert mode branch
elif [ $invert_mode = true ];then
    target_panes=$(echo ${pane_array[@]} ${option_index[@]} | tr ' ' '\n' | sort | uniq -u)
    if [ $enabled_mode = true ];then
        enable_panes $target_panes
    elif [ $disabled_mode = true ];then
        disable_panes $target_panes
    else
        echo &quot;-e or -d (enabled/disabled) flags required&quot;
        exit 1
    fi
# Regular modes branch
else
    target_panes=${option_index[@]}
    if [ $only_mode = true ];then
        echo &quot;-o mode can only be used with groups (-g)&quot;
        exit 1
    elif [ $enabled_mode = true ];then
        enable_panes $target_panes
    elif [ $disabled_mode = true ];then
        disable_panes $target_panes
    elif [ $enable_all_mode = true ];then
        enable_panes ${pane_array[@]}
    else
        echo &quot;-e -d or -a (enabled/disabled/enable_all) flags required&quot;
        exit 1
    fi
fi

}

group_exists() { declare -A groupMap for pane in ${pane_array[@]};do groupName=$(tmux show-options -p -t $pane -q -v @groupid) if [ -z $groupName ];then groupName="_" fi groupMap[$groupName]+="$pane|" done

eval '[ ${'groupMap[$1]'+test} ]'

}

group_panes() { local user_targets=("$@")

# Check for missing group name
if [ $group_name = &quot;+&quot; ];then
    echo &quot;group_name cannot be set to '+', did you forget to add a group_name?&quot;
    exit 1
fi

# Check that group_name doesn't start with a number
if [[ $group_name =~ ^[0-9]*$ ]];then
    echo &quot;group_name cannot start with a number&quot;
    exit 1
fi

# If '+' add pane to group_name
if [ ${user_targets[0]} = &quot;+&quot; ];then
    if group_exists $group_name;then
        for pane in ${user_targets[@]:1};do
            echo &quot;Adding pane $pane to group $group_name&quot;
            tmux set-option -p -t $pane @groupid $group_name
        done
    else 
        echo &quot;Group: $group_name not found. Cannot add pane to non-existant group&quot;
        exit 1
    fi
# Otherwise, add all target panes to group_name
else
    for pane in ${user_targets[@]};do
        tmux set-option -p -t $pane @groupid $group_name
    done
fi

}

enable_all() { for pane in ${pane_array[@]};do tmux select-pane -t $pane -e # Update tmux pane title (if enabled in tmux.conf) tmux set-option -p -t $pane @custom_pane_title "E " done }

disable_panes() { arr=("$@") for pane in ${arr[@]};do if ! [[ $pane =~ $num_re ]] ; then echo "Args must be pane indexes not names: ($pane)" exit 1 fi tmux select-pane -t $pane -d # Update tmux pane title (if enabled in tmux.conf) echo "disabling pane: $pane" tmux set-option -p -t $pane @custom_pane_title "D " done }

enable_panes() { arr=("$@") for pane in ${arr[@]};do if ! [[ $pane =~ $num_re ]] ; then echo "Args must be pane indexes not names: ($pane)" exit 1 fi tmux select-pane -t $pane -e # Update tmux pane title (if enabled in tmux.conf) echo "enabling pane: $pane" tmux set-option -p -t $pane @custom_pane_title "E " done }

enable_only_group() { if group_exists $1;then declare -A groupMap for pane in ${pane_array[@]};do groupName=$(tmux show-options -p -t $pane -q -v @groupid) if [ -z $groupName ];then groupName="_" fi groupMap[$groupName]+="$pane|" done enable_all for key in ${!groupMap[@]};do if [ $1 != $key ];then for p in ${groupMap[$key]};do IFS='|'; read -r -a pane_list <<< $p disable_panes ${pane_list[@]} done fi done else echo "Cannot enable non-existant group: $1" exit 1 fi }

enable_group() { if group_exists $1;then declare -A groupMap for pane in ${pane_array[@]};do groupName=$(tmux show-options -p -t $pane -q -v @groupid) if [ -z $groupName ];then groupName="_" fi groupMap[$groupName]+="$pane|" done for p in ${groupMap[$1]};do IFS='|'; read -r -a pane_list <<< $p enable_panes ${pane_list[@]} done else echo "Cannot enable non-existant group: $1" exit 1 fi }

disable_group() { if group_exists $1;then declare -A groupMap for pane in ${pane_array[@]};do groupName=$(tmux show-options -p -t $pane -q -v @groupid) if [ -z $groupName ];then groupName="_" fi groupMap[$groupName]+="$pane|" done for p in ${groupMap[$1]};do IFS='|'; read -r -a pane_list <<< $p disable_panes ${pane_list[@]} done else echo "Cannot disable non-existant group: $1" exit 1 fi }

main "$@"; exit