2

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

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

我见过这个问题,但我不一定想向所有窗口发出命令。

4

2 回答 2

2

这是一个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

于 2020-11-21T18:14:00.003 回答
1

我刚刚构建了一个脚本来做到这一点,这里是:

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

最好的

于 2019-10-11T11:45:47.723 回答