我并不是在寻找键盘快捷键,而是想要以下命令:
- 新窗户
- 新标签
- 关闭当前标签或窗口
- 最大化 Shell 窗口
- 最小化 Shell 窗口
- 将 Shell 移至其他工作空间
- 切换标签
基本上就是类似这样的操作。记住,我不需要快捷方式,而是需要实际的命令。这样做的原因是我可以利用别名功能。
答案1
默认情况下,您无法在 Gnome-Terminal 中执行此操作,至少使用原始命令不行。
但是,您可以编写脚本来调用可以执行此操作的键盘快捷键。请注意,您需要xdotool
:sudo 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
我们可以wmctrl
在这里使用:wmctrl -r :ACTIVE: -b toggle,maximized_vert,maximized_horz
添加.bashrc
:echo "alias maw='wmctrl -r :ACTIVE: -b toggle,maximized_vert,maximized_horz'" >> .bashrc
最小化窗口:最小化整个窗口
miw
我们可以再次使用xdotool
:xdotool windowminimize $(xdotool getactivewindow)
添加.bashrc
:echo "alias miw='xdotool windowminimize $(xdotool getactivewindow)'" >> .bashrc
移至工作区
mtw <id>
:使用以下命令将窗口移动到另一个工作区:仅仅在 shell 脚本中是不可能实现的,而且远远超出了我的个人经验。我建议使用 Serg 的脚本来实现这个目的,因为它现在确实有效。啊,Compiz 的好处。
答案2
介绍
本答案中提供的脚本允许用户通过一个命令和选项列表来控制其终端窗口。它使用简单,并且与任何具有类似键绑定的终端仿真器兼容gnome-terminal
。移动选项也可以用于其他终端,但不保证这些终端可以打开选项卡。
该脚本涵盖了标签页打开、窗口打开、向下移动到工作区、向右移动到工作区、通过整数引用的特定工作区、最小化、最大化和取消最大化窗口。该脚本唯一没有涵盖的是关闭标签页/窗口,因为每个 shell/终端仿真器已经有了相应的命令 -exit
或者通过CtrlD快捷方式。
!!!注意:您需要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 应用程序的功能。有终端多路复用器,如
screen
或 ,tmux
它们可以有“选项卡”或分割窗口,这有点接近“可选项卡的 shell”,但这不是您问的同一类型的行为。基本上,您的问题的答案是“否”。总是有替代方案,我的答案提供了其中之一。它根据终端窗口的性质来处理它 - X11 GUI 窗口。这个答案与别名有什么关系?嗯,首先别名可能有点混乱,特别是在引用和解析来自多个命令的多个输出时。此脚本为您提供了一个集中的命令,带有标志/开关,可在窗口上执行离散任务。它还使别名更简单。您可以这样做
alias nw='windowctrl.py --window'
。更短,更整洁。