如何从命令行在 GNOME 终端中打开一个新的选项卡?

我使用 Ubuntu 9.04 x64,当我写:

gnome-terminal --tab

在终端,我希望它在同一个终端窗口中打开一个新的选项卡。但却打开了一扇新的窗户。

我发现它的意图是在一个新窗口中打开一个新的标签页,也就是说,如果我写:

gnome-terminal --tab --tab

它将打开一个带有两个选项卡的新窗口。

因此,问题是,我如何使用 gnome-terminal中的命令在 目前窗口中打开一个新的选项卡?

150644 次浏览

我没有安装 gnome 终端,但是您应该能够通过使用 < strong > dbus-send 在命令行上使用 DBUS 调用来实现这一点。

您还可以让每个选项卡运行一个 set 命令。

gnome-terminal --tab -e "tail -f somefile" --tab -e "some_other_command"
#!/bin/sh


WID=$(xprop -root | grep "_NET_ACTIVE_WINDOW(WINDOW)"| awk '{print $5}')
xdotool windowfocus $WID
xdotool key ctrl+shift+t
wmctrl -i -a $WID

这将自动确定相应的终端,并相应地打开选项卡。

考虑改用 Roxterm。

roxterm --tab

在当前窗口中打开一个选项卡。

对于寻找不使用命令行的解决方案的任何人: ctrl + shift + t

更详细的版本(从另一个窗口使用) :

#!/bin/bash


DELAY=3


TERM_PID=$(echo `ps -C gnome-terminal -o pid= | head -1`) # get first gnome-terminal's PID
WID=$(wmctrl -lp | awk -v pid=$TERM_PID '$3==pid{print $1;exit;}') # get window id


xdotool windowfocus $WID
xdotool key alt+t # my key map
xdotool sleep $DELAY # it may take a while to start new shell :(
xdotool type --delay 1 --clearmodifiers "$@"
xdotool key Return


wmctrl -i -a $WID # go to that window (WID is numeric)


# vim:ai
# EOF #

对于在同一个终端窗口中打开多个选项卡,您可以使用以下解决方案。

示例脚本:

pwd='/Users/pallavi/Documents/containers/platform241/etisalatwallet/api-server-tomcat-7/bin'
pwdlog='/Users/pallavi/Documents/containers/platform241/etisalatwallet/api-server-tomcat-7/logs'
pwd1='/Users/pallavi/Documents/containers/platform241/etisalatwallet/core-server-jboss-7.2/bin'
logpwd1='/Users/pallavi/Documents/containers/platform241/etisalatwallet/core-server-jboss-7.2/standalone/log'


osascript -e "tell application \"Terminal\"" \


-e "tell application \"System Events\" to keystroke \"t\" using {command down}" \
-e "do script \"cd $pwd\" in front window" \
-e "do script \"./startup.sh\" in front window" \
-e "tell application \"System Events\" to keystroke \"t\" using {command down}" \
-e "do script \"cd $pwdlog\" in front window" \
-e "do script \"tail -f catalina.out \" in front window" \
-e "tell application \"System Events\" to keystroke \"t\" using {command down}" \
-e "do script \"cd $pwd1\" in front window" \
-e "do script \"./standalone.sh\" in front window" \
-e "tell application \"System Events\" to keystroke \"t\" using {command down}" \
-e "do script \"cd $logpwd1\" in front window" \
-e "do script \"tail -f core.log \" in front window" \
-e "end tell"
> /dev/null

我找到了最简单的方法:

gnome-terminal --tab -e 'command 1' --tab -e 'command 2'

我使用 tmux代替直接使用终端。所以我真正想要的是一个简单的命令/shell 文件来构建具有几个 tmux窗口的开发 env。Shell 代码如下:

#!/bin/bash
tabs="adb ana repo"
gen_params() {


local params=""
for tab in ${tabs}
do
params="${params} --tab -e 'tmux -u attach-session -t ${tab}'"
done
echo "${params}"
}
cmd="gnome-terminal $(gen_params)"
eval $cmd

为了将上面的几点结合起来,这里有一个脚本,它将运行传递给脚本 vim new_tab.sh的所有参数:

#!/bin/bash
#
# Dependencies:
#   sudo apt install xdotool


WID=$(xprop -root | grep "_NET_ACTIVE_WINDOW(WINDOW)"| awk '{print $5}')
xdotool windowfocus $WID
xdotool key ctrl+shift+t
wmctrl -i -a $WID
sleep 1; xdotool type --delay 1 --clearmodifiers "$@"; xdotool key Return;

下一步,让它可执行: chmod +x new_tab.sh

现在你可以使用它在一个新标签中运行任何你想要的东西: ./new_tab.sh "watch ls -l"

以防万一,你想打开

  • 一扇新的窗户
  • 有两个标签
  • 执行命令
  • 让他们继续营业。

给你:

gnome-terminal --geometry=73x16+0+0 --window \
--working-directory=/depot --title='A' --command="bash -c ls;bash" \
--tab --working-directory=/depot/kn --title='B' --command="bash -c ls;bash"

(对了,mate-terminal也一样)