【问题标题】:How to close all windows to the right in tmux如何在 tmux 中关闭右侧的所有窗口
【发布时间】:2019-02-13 16:03:48
【问题描述】:

有没有办法发出命令关闭所有 tmux 窗口,除非该窗口中打开了某些东西?例如,打开的文件、正在运行的进程等?

我希望有一个可以用作网络浏览器的东西,您可以在其中右键单击并选择close all other tabs to the right. 我想在 tmux 中发布这个,类似于网络浏览器示例,有“忙碌”的窗口或窗格提示我关闭它们或默默地关闭它们。

我见过this question,但我不一定要向所有窗口发出命令。

【问题讨论】:

  • 有一种方法,但它不是内置的。您必须编写脚本。您可以使用#{window_panes} 获取当前窗格的数量并遍历它们,并确保被测窗格编号大于当前窗格(即当前窗格的右侧)。然后只需为该窗格发送kill pane
  • 谢谢,杰里米!

标签: tmux tmuxinator


【解决方案1】:

这是一个 shell 替代方案:

for win_id in $(tmux list-windows -F '#{window_active} #{window_id}' | awk '/^1/ { active=1; next } active { print $2 }'); do tmux kill-window -t "$win_id"; done

这是相同的(可读版本):

for win_id in $(tmux list-windows -F '#{window_active} #{window_id}' | \
                awk '/^1/ { active=1; next } active { print $2 }')
do 
  tmux kill-window -t "$win_id"
done

编辑:我用这个做了一个插件! https://github.com/pschmitt/tmux-forsaken

【讨论】:

    【解决方案2】:

    我刚刚构建了一个脚本来执行此操作,这里是:

    #!/usr/bin/env python3
    import subprocess
    import os
    import re
    
    result = subprocess.run(['tmux', 'list-windows'], stdout=subprocess.PIPE)
    
    result = result.stdout.decode('utf-8')
    
    lines = result.splitlines()
    should_close_next = False
    for line in lines:
    
        if should_close_next:
            window = line.split(':')[0]
            os.system(f'tmux kill-window -t {window}')
            continue
    
        match = re.search("active", line)
        if match:
            should_close_next = True
    

    并将其与您的 tmux 添加到您的 tmux.conf 中

    bind-key "k" run-shell "kill_panes_to_right.py\n"
    

    最好的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-01
      • 1970-01-01
      • 2022-01-23
      • 2021-04-08
      • 2014-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多