我怎样才能优雅地关闭gnome-terminal的所有实例

我试图找到如何关闭终端的所有窗口(退出所有gnome-terminal实例),干净利落。 通过“干净地”,我的意思是它不仅仅是简单地一次性杀死所有实例,这就是我已经拥有的这个别名:

alias poof='/usr/bin/killall gnome-terminal' 

我想要做的是让它的行为与终端应用程序在Mac OS X中的行为一样。在该平台上,当我在终端中点击“command-Q”(a / k / a“Apple-Q”)时,所有的窗口是关闭的,但如果在任何特定的终端窗口中有进程运行,我会收到一个警告我的对话框,并询问我是否仍要关闭窗口。 这样我可以确保我不会杀死我忘记的过程(例如用vim编辑文件)。 换句话说,它就像我点击了每个窗口上的关闭按钮一样。

这个问题之前已经以这种或那种forms提出,但没有得到令人满意的答复(除非我误解了答案)。 当然有一种方法可以在Ubuntu 14.04中做到这一点? 在命令行中还是使用键盘快捷键(或两者)?

(如果我没有正确地遵循任何风格格式,请原谅我,因为我是新手。)

即使是“最友好的”kill-command也会在没有询问的情况下关闭终端。 另外man gnome-terminal没有提供任何解决方案来关闭窗口,就像在GUI中一样。

但是,您可以创建一个脚本(全部) gnome-terminal窗口并模拟Ctrl + Shift + Q.

复杂性是当窗口分布在不同的工作空间时,这将不起作用。 因此,下面的脚本在当前工作空间中查找gnome-terminal窗口,并按照上面的说明处理它们。

剧本

 #!/usr/bin/env python3 import subprocess import time def get_res(): # get resolution xr = subprocess.check_output(["xrandr"]).decode("utf-8").split() pos = xr.index("current") return [int(xr[pos+1]), int(xr[pos+3].replace(",", "") )] try: pid = subprocess.check_output(["pidof", "gnome-terminal"]).decode("utf-8").strip() except: pass else: res = get_res() ws = subprocess.check_output(["wmctrl", "-lpG"]).decode("utf-8").splitlines() for t in [w for w in ws if pid in w]: window = t.split() if all([0 < int(window[3]) < res[0], 0 < int(window[4]) < res[1]]) : w_id = window[0] subprocess.Popen(["wmctrl", "-ia", w_id]) subprocess.call(["xdotool", "key", "Ctrl+Shift+Q"]) time.sleep(0.2) 

如何使用

  1. 该脚本需要wmctrlxdotool

     sudo apt-get install xdotool sudo apt-get install wmctrl 
  2. 将脚本复制到空文件中,将其另存为close_allterminals.py

  3. 通过命令测试运行它:

     python3 /path/to/close_allterminals.py 

    示例:打开四个gnome-terminal窗口,左上角是运行的进程:

    运行该命令后,三个自动关闭,运行进程的一个提示:

  4. 如果一切正常,请将其添加到快捷键组合中:选择系统设置>键盘>快捷方式>自定义快捷方式 。 单击“+”并添加命令:

      python3 /path/to/close_allterminals.py 

编辑

下面的版本还负责其他工作区上的gnome-terminal窗口:所有窗口在安全关闭之前都会移动到当前工作区。

一个例子:
我总共有六个gnome-terminal窗口在四个不同的工作区上打开,其中许多都有进程在其中运行:

如果我运行脚本,所有gnome-terminal窗口都会有序地移动到当前工作区并引发。 空闲窗口自动关闭,提示具有正在运行的进程:

剧本

像第一个版本一样设置它。

 #!/usr/bin/env python3 import subprocess import time def get_res(): # get resolution xr = subprocess.check_output(["xrandr"]).decode("utf-8").split() pos = xr.index("current") return [int(xr[pos+1]), int(xr[pos+3].replace(",", "") )] try: pid = subprocess.check_output(["pidof", "gnome-terminal"]).decode("utf-8").strip() except: pass else: res = get_res() ws = subprocess.check_output(["wmctrl", "-lpG"]).decode("utf-8").splitlines() matches = [t.split() for t in [w for w in ws if pid in w]] pos = 100 for match in matches: w_id = match[0] subprocess.call(["xdotool", "windowmove", "--sync", match[0], str(pos), str(pos/2) ]) subprocess.call(["wmctrl", "-ia", w_id]) subprocess.call(["xdotool", "key", "Ctrl+Shift+Q"]) pos = pos+100 

您可以使用这个简单的shell脚本close_terminals

 #!/bin/bash xdotool search --class "terminal" | while read id do xdotool windowactivate "$id" &>/dev/null xdotool key ctrl+shift+q sleep 0.2 done 

然后创建一个新的快捷方式并调用此脚本。