在Gnome-Terminal中有一个命令,或者任何可以打开新标签的tabbable shell?

我不是在寻找键盘快捷键,而是我想要一个命令:

  • 新窗户
  • 新标签
  • 关闭当前选项卡或窗口
  • 最大化壳窗口
  • 最小化Shell窗口
  • 将Shell移动到不同的工作空间
  • 切换标签

基本上都是这样的。 记得; 我不想要快捷方式,而是实际的命令。 这样做的原因是我可以使用别名function。

默认情况下,您无法在Gnome-Terminal中执行此操作,至少使用raw命令。

但是,您可以编写调用可执行此操作的键盘快捷方式的脚本。 请注意,您需要xdotoolsudo apt install xdotool

  • 新窗口 :使用nw启动新的终端窗口
    我们可以用gnome-terminal来做到这一点。
    添加到.bashrc

     echo "alias nw=gnome-terminal" >> ~/.bashrc 
  • 新标签 :使用nt启动新标签页
    我们可以用xdotool getactivewindow $(xdotool key ctrl+shift+t)来做到这一点
    添加到.bashrc

     echo "alias nt='xdotool getactivewindow $(xdotool key ctrl+shift+t)'" >> .bashrc 
  • 关闭选项卡 :使用ct关闭当前选项卡或窗口
    xdotool再次出现: xdotool getactivewindow $(xdotool key ctrl+shift+w)
    添加到.bashrc

     echo "alias ct='xdotool getactivewindow $(xdotool key ctrl+shift+w)'" >> .bashrc 
  • 最大化窗口 :使用maw最大化整个窗口
    我们可以在这里使用wmctrlwmctrl -r :ACTIVE: -b toggle,maximized_vert,maximized_horz
    添加到.bashrc

     echo "alias maw='wmctrl -r :ACTIVE: -b toggle,maximized_vert,maximized_horz'" >> .bashrc 
  • 最小化窗口 :使用miw最小化整个窗口
    我们可以再次使用xdotoolxdotool windowminimize $(xdotool getactivewindow)
    添加到.bashrc

     echo "alias miw='xdotool windowminimize $(xdotool getactivewindow)'" >> .bashrc 
  • 移至工作区 :使用mtw 将窗口移动到另一个工作区
    这在shell脚本编写中几乎是不可能的,并且超出了我的个人经验。 我建议使用Serg的脚本来实现这个目的,因为它实际上已经起作用了。 啊,Compiz的好处。

介绍

本回答中提供的脚本允许用户通过一个命令和选项列表控制其终端窗口。 它使用简单,并且与任何具有类似于gnome-terminal键绑定的终端仿真器兼容。 移动选项也可以与其他终端一起使用,但不保证这些终端的标签打开。

该脚本包括选项卡打开,窗口打开,向下移动工作区,右侧工作区,特定工作区按整数引用,最小化,最大化和取消最大化窗口。 脚本没有涉及的唯一事情是关闭选项卡/窗口只是因为每个shell /终端模拟器已经有一个命令 – exit或者通过Ctrl D快捷方式。

注意:您将需要xdotool进行工作区切换和选项卡打开。 通过sudo apt-get install xdotool安装它。 如果您不想安装额外的软件包,请记住,工作区和选项卡切换不起作用 ,但其他选项也会。

用法:

windowctrl.py所有参数都是可选的,因此它们可以单独使用,也可以一起使用。 如-h选项所示。

 $ ./windowctrl.py -h usage: windowctrl.py [-h] [-w] [-t] [-m] [-M] [-u] [-v VIEWPORT] [-r] [-d] Copyright 2016. Sergiy Kolodyazhnyy. Window control for terminal emulators. Originally written for gnome-terminal under Ubuntu with Unity desktop but can be used with any other terminal emulator that conforms to gnome-terminal keybindings. It can potentially be used for controlling other windows as well via binding this script to a keyboard shortcut. Note that --viewport and --tab options require xdotool to be installed on the system. If you don't have it installed, you can still use the other options. xdotool can be installed via sudo apt-get install xdotool. optional arguments: -h, --help show this help message and exit -w, --window spawns new window -t, --tab spawns new tab -m, --minimize minimizes current window -M, --maximize maximizes window -u, --unmaximize unmaximizes window -v VIEWPORT, --viewport VIEWPORT send window to workspace number -r, --right send window to workspace right -d, --down send window to workspace down 

脚本源代码:

脚本源代码可以在GitHub以及此处获得。 最新的更改可能会进入GitHub,而不是在这里,所以我强烈建议在那里检查最新版本。 还建议在那里发布错误报告。

 #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Program name: windowctrl.py Author: Sergiy Kolodyazhnyy Date: Sept 18, 2016 Written for: http://askubuntu.com/q/826310/295286 Tested on Ubuntu 16.04 LTS """ from __future__ import print_function import gi gi.require_version('Gdk', '3.0') from gi.repository import Gio,Gdk import sys import dbus import subprocess import argparse def gsettings_get(schema,path,key): """Get value of gsettings schema""" if path is None: gsettings = Gio.Settings.new(schema) else: gsettings = Gio.Settings.new_with_path(schema,path) return gsettings.get_value(key) def run_cmd(cmdlist): """ Reusable function for running shell commands""" try: stdout = subprocess.check_output(cmdlist) except subprocess.CalledProcessError: print(">>> subprocess:",cmdlist) sys.exit(1) else: if stdout: return stdout def get_dbus(bus_type,obj,path,interface,method,arg): # Reusable function for accessing dbus # This basically works the same as # dbus-send or qdbus. Just give it # all the info, and it will spit out output if bus_type == "session": bus = dbus.SessionBus() if bus_type == "system": bus = dbus.SystemBus() proxy = bus.get_object(obj,path) method = proxy.get_dbus_method(method,interface) if arg: return method(arg) else: return method() def new_window(): screen = Gdk.Screen.get_default() active_xid = int(screen.get_active_window().get_xid()) app_path = get_dbus( 'session', 'org.ayatana.bamf', '/org/ayatana/bamf/matcher', 'org.ayatana.bamf.matcher', 'ApplicationForXid', active_xid ) desk_file = get_dbus('session', 'org.ayatana.bamf', str(app_path), 'org.ayatana.bamf.application', 'DesktopFile', None ) # Big credit to Six: http://askubuntu.com/a/664272/295286 Gio.DesktopAppInfo.new_from_filename(desk_file).launch_uris(None) def enumerate_viewports(): """ generates enumerated dictionary of viewports and their indexes, counting left to right """ schema="org.compiz.core" path="/org/compiz/profiles/unity/plugins/core/" keys=['hsize','vsize'] screen = Gdk.Screen.get_default() screen_size=[ screen.get_width(),screen.get_height()] grid=[ int(str(gsettings_get(schema,path,key))) for key in keys] x_vals=[ screen_size[0]*x for x in range(0,grid[0]) ] y_vals=[screen_size[1]*x for x in range(0,grid[1]) ] viewports=[(x,y) for y in y_vals for x in x_vals ] return {vp:ix for ix,vp in enumerate(viewports,1)} def get_current_viewport(): """returns tuple representing current viewport, in format (width,height)""" vp_string = run_cmd(['xprop', '-root', '-notype', '_NET_DESKTOP_VIEWPORT']) vp_list=vp_string.decode().strip().split('=')[1].split(',') return tuple( int(i) for i in vp_list ) def maximize(): screen = Gdk.Screen.get_default() window = screen.get_active_window() window.maximize() screen.get_active_window() window.process_all_updates() def unmaximize(): screen = Gdk.Screen.get_default() window = screen.get_active_window() window.unmaximize() screen.get_active_window() window.process_all_updates() def minimize(): screen = Gdk.Screen.get_default() window = screen.get_active_window() window.iconify() window.process_all_updates() def window_move(viewport): # 1. grab window object # 2. jump viewport 0 0 so we can move only # in positive plane # 3. move the window. # 4. set viewport back to what it was # Step 1 screen = Gdk.Screen.get_default() screen_size=[ screen.get_width(),screen.get_height()] window = screen.get_active_window() viewports = enumerate_viewports() current = get_current_viewport() current_num = viewports[current] destination = [ key for key,val in viewports.items() if val == int(viewport) ][0] # Step 2. run_cmd([ 'xdotool', 'set_desktop_viewport', '0','0' ]) # Step 3. window.move(destination[0],destination[1]) window.process_all_updates() run_cmd([ 'xdotool', 'set_desktop_viewport', str(current[0]), str(current[1]) ]) def move_right(): sc = Gdk.Screen.get_default() width = sc.get_width() win = sc.get_active_window() pos = win.get_origin() win.move(width,pos.y) win.process_all_updates() def move_down(): sc = Gdk.Screen.get_default() height = sc.get_height() win = sc.get_active_window() pos = win.get_origin() win.move(pos.x,height) win.process_all_updates() def new_tab(): run_cmd(['xdotool','key','ctrl+shift+t']) def parse_args(): """ Parse command line arguments""" info="""Copyright 2016. Sergiy Kolodyazhnyy. Window control for terminal emulators. Originally written for gnome-terminal under Ubuntu with Unity desktop but can be used with any other terminal emulator that conforms to gnome-terminal keybindings. It can potentially be used for controlling other windows as well via binding this script to a keyboard shortcut. Note that --viewport and --tab options require xdotool to be installed on the system. If you don't have it installed, you can still use the other options. xdotool can be installed via sudo apt-get install xdotool. """ arg_parser = argparse.ArgumentParser( description=info, formatter_class=argparse.RawTextHelpFormatter) arg_parser.add_argument( '-w','--window', action='store_true', help='spawns new window', required=False) arg_parser.add_argument( '-t','--tab',action='store_true', help='spawns new tab', required=False) arg_parser.add_argument( '-m','--minimize',action='store_true', help='minimizes current window', required=False) arg_parser.add_argument( '-M','--maximize',action='store_true', help='maximizes window', required=False) arg_parser.add_argument( '-u','--unmaximize',action='store_true', help='unmaximizes window', required=False) arg_parser.add_argument( '-v','--viewport',action='store', type=int, help='send window to workspace number', required=False) arg_parser.add_argument( '-r','--right',action='store_true', help='send window to workspace right', required=False) arg_parser.add_argument( '-d','--down',action='store_true', help='send window to workspace down', required=False) return arg_parser.parse_args() def main(): args = parse_args() if args.window: new_window() if args.tab: new_tab() if args.down: move_down() if args.right: move_right() if args.viewport: window_move(args.viewport) if args.minimize: minimize() if args.maximize: maximize() if args.unmaximize: unmaximize() if __name__ == '__main__': main() 

旁注

  • 您询问“Gnome-Terminal中是否有命令,或任何可触发的shell都打开一个新选项卡?” Gnome Terminal手册没有列出这样的选项。 shell是命令行实用程序。 选项卡是GUI应用程序的function。 有像screentmux这样的终端多路复用器,它们可以有“标签”或分割窗口,这种类型接近“tabbable shell”,但这与你提出的行为类型不同。 基本上,回答你的问题是“不”。 总有其他选择,我的回答提供了其中一个。 它根据其性质处理终端窗口 – X11 GUI窗口。

  • 这个答案与别名有什么关系? 好吧,首先别名可能有点乱,特别是在引用和解析多个命令的多个输出时。 这个脚本为您提供了一个集中命令,带有标志/开关,可以在窗口上执行离散任务。 它还使别名更简单。 你可以做alias nw='windowctrl.py --window' 。 更短,更整洁。