如何选择一些窗格并将它们与 TMUX 同步?

如何选择一些窗格并将它们与 TMUX 同步?

我正在寻找一种方法来同步 TMUX 中选定的某些窗格。

我正在寻找的第二个选项是如何创建几组窗格,然后同步他们。

在 TMUX 中可以实现吗?

我知道终结者多路复用器中有这个功能

是否有任何选项可以在窗格中添加有关此信息...例如组名和同步信息?

-------------------------------
|            |                |  First group of panes + sync
|            |                |  With specific group name  GROUP-1
|            |                |
|            |                |
-------------------------------
|            |        |       |
|            |        |       |  Second group of panes + sync
|            |        |       |  With specific group name  GROUP-2
|            |        |       |
-------------------------------
|            |                |
|            |                |  Free panes - no sync
|            |                |
|            |                |
-------------------------------

答案1

您可以通过使用 +Pane -d 禁用窗格来排除同步Select(使用Select+Pane -e 再次启用它)。

答案2

我有同样的需求,但找不到任何不需要过度手动的东西。使用Nicholas Marriott 描述的方法,我创建了一个小型 bash 脚本,可以通过一个命令禁用/启用选定窗格上的输入。也许有人会觉得它很有用:

https://github.com/rollerd/paneblocker

~~没有进行分组,但如果有的话就更好了~~。

添加了分组并重构了一些内容。

Bash 脚本的代码如下:

#!/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="${@:$OPTIND}" 
    # Group mode branch
    if [ $group_name != "None" ];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 "-e or -d (enabled/disabled) flags required"
            exit 1
        fi
    # Regular modes branch
    else
        target_panes=${option_index[@]}
        if [ $only_mode = true ];then
            echo "-o mode can only be used with groups (-g)"
            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 "-e -d or -a (enabled/disabled/enable_all) flags required"
            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 = "+" ];then
        echo "group_name cannot be set to '+', did you forget to add a group_name?"
        exit 1
    fi

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

    # If '+' add pane to group_name
    if [ ${user_targets[0]} = "+" ];then
        if group_exists $group_name;then
            for pane in ${user_targets[@]:1};do
                echo "Adding pane $pane to group $group_name"
                tmux set-option -p -t $pane @groupid $group_name
            done
        else 
            echo "Group: $group_name not found. Cannot add pane to non-existant group"
            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

相关内容