我经常使用终端来执行快速命令,然后将其留在后台,以便最终在工作时打开20多个终端会话。这是因为仅使用快捷键并键入命令是非常快速的。
有没有一种方法可以设置快捷键,以便我调出最后一个终端窗口而不是创建一个新的窗口?
我经常使用终端来执行快速命令,然后将其留在后台,以便最终在工作时打开20多个终端会话。这是因为仅使用快捷键并键入命令是非常快速的。
有没有一种方法可以设置快捷键,以便我调出最后一个终端窗口而不是创建一个新的窗口?
Answers:
我使用guake,对此感到非常满意。按F12,出现一个终端窗口,再次按F12,它消失,但继续在后台运行。另外:看起来真的很酷。
您可以将脚本放在下面的组合键下。如果按组合键,终端窗口将消失(完全)。再按一次,它们将再次完全按照您的状态弹出。
您唯一需要做的就是在终端的窗口名称中添加标识字符串(大多数情况下,终端窗口具有相同的名称)
同时安装xdotool
和wmctrl
:
sudo apt-get install xdotool
sudo apt-get install wmctrl
hide_terminal.py
在组合键下运行它:
python3 /path/to/hide_terminal.py
#!/usr/bin/env python3
import subprocess
import os
home = os.environ["HOME"]
hidden_windowid = home+"/.window_id.txt"
get = lambda cmd: subprocess.check_output(cmd).decode("utf-8")
# --- set the identifying string in the terminal window's name below (you mentioned "Terminal"
window_idstring = "Special_window"
# ---
def execute(cmd):
subprocess.check_call(cmd)
w_id = [l.split()[0] for l in get(["wmctrl", "-l"]).splitlines() if window_idstring in l]
if len(w_id) !=0:
for w in w_id:
execute(["xdotool", "windowunmap", w])
with open(hidden_windowid, "a") as out:
out.write(w+"\n")
else:
try:
with open(hidden_windowid) as read:
for w in [w.strip() for w in read.readlines()]:
try:
execute(["xdotool", "windowmap", w])
except subprocess.CalledProcessError:
pass
with open(hidden_windowid, "wt") as clear:
clear.write("")
except FileNotFoundError:
pass
gnome-terminal
。另外,如果您有多个打开的终端,则会中断。在我的系统上,第一个运行隐藏活动的,第二个运行隐藏第二个,第三个运行仅返回第二个终端。第一永远失去。
这与用bash编写的Jacob Vlijm的答案相同:
#!/usr/bin/env bash
## window_name will be the first argument passed or, if no
## argument was given, "Terminal"
window_name=${1:-"Terminal"}
## Get the list of open terminals
terms=( $(wmctrl -l | grep "$window_name" | cut -d ' ' -f 1) )
## If all terminals are hidden
if [ -z "${terms[0]}" ]
then
## Read the IDs of hidden windows from .hidden_window_id
while read termid
do
xdotool windowmap "$termid"
done < ~/.hidden_window_id
## If there are visible terminals
else
## Clear the .hidden_window_id file
> ~/.hidden_window_id
## For each open terminal
for i in "${terms[@]}"
do
## Save the current ID into the file
printf "%s\n" "$i" >> ~/.hidden_window_id
## Hide the window
xdotool windowunmap "$i"
done
fi
如果将其另存为~/bin/show_hide.sh
,则可以通过提供要隐藏的任何窗口的标识字符串来运行它。如果没有给出字符串,它将在Terminal
:
show_hide.sh Terminal
我使用的gnome-shell
是'Drop Down Terminal'扩展名,默认快捷方式是TAB
但很容易更改。
以下方法对我有用:
#!/usr/bin/env bash
# Written by Eric Zhiqiang Ma (http://www.ericzma.com)
# Last update: Jul. 9, 2014
# Read the tutorials at
# http://www.systutorials.com/5475/turning-gnome-terminal-to-a-pop-up-terminal/
# Required tools: xdotool
terminal="gnome-terminal"
stat_file="/dev/shm/actiavte-termianl.term.$USER"
termtype="Terminal"
wait_sec=1
max_wait_cnt=4
# parse options first
if [ "$1" != "" ]; then
terminal="$1"
fi
term_exists () {
allterms=`xdotool search --class "$termtype"`
for e in $allterms; do [[ "$e" == "$1" ]] && return 0; done
return 1
}
create_terminal () {
echo "Create new terminal"
$terminal --maximize &
exists=1
wait_cnt=0
until [ "$exists" == "0" ]; do
# sleep a while
sleep $wait_sec
# Note that the focus window may be from a terminal that
# already exists; the makes create_terminal choose the existing terminal
# Making the wait_sec large enough so that the terminal can be created and
# displayed can make the false choosing more unlikely.
term=$(xdotool getwindowfocus)
term_exists "$term"
exists=$?
# avoid infinite wait
let wait_cnt=wait_cnt+1
if [ $wait_cnt -gt $max_wait_cnt ]; then
echo "Wait for too long. Give up."
term=""
exists=0
fi
done
echo "Created terminal window $term"
# save the state
echo "$term" >$stat_file
}
# read the state
if [ -f $stat_file ]; then
term=$(cat $stat_file)
fi
# check whether it exists
term_exists "$term"
exists=$?
if [[ "$exists" != "0" ]]; then
create_terminal
exit 0
fi
# check whether it is already activated
curwin=$(xdotool getwindowfocus)
if [ "$term" == "$curwin" ]; then
# deactivate (minimize) the terminal if it is currently activated
xdotool windowminimize $curwin
else
# activate the terminal if it is not currently activated
xdotool windowactivate $term
fi
exit 0
然后只需为其授予执行权限,并将脚本绑定到设置中的某个键即可。
资源:
http://www.systutorials.com/5475/turning-gnome-terminal-to-a-pop-up-terminal/