如何在 tmux 中关闭右边的所有 windows

How to close all windows to the right in tmux

有没有一种方法可以发出关闭所有 tmux windows 的命令,除非 window 中有东西打开?比如一个打开的文件,一个运行进程等等?

我希望有一个可以用作网络浏览器的东西,您可以在其中右键单击 select close all other tabs to the right. 我想在 tmux 中发布它,类似于网络浏览器示例,有 "busy" windows 或窗格提示我关闭它们或静默无法关闭。

我看到了this question,但我不一定要向所有windows发出命令。

我刚刚为此构建了一个脚本,这里是:

#!/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"

最佳

这是一个 shell 替代方案:

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

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