Linux 使用命令行在 gnome-terminal 中打开一个新选项卡

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1188959/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 17:34:09  来源:igfitidea点击:

Open a new tab in gnome-terminal using command line

linuxubuntuconsoleterminal

提问by Vikrant Chaudhary

I'm using Ubuntu 9.04 x64 and when I write:

我正在使用 Ubuntu 9.04 x64,当我写:

gnome-terminal --tab

At the terminal, I expect it to open a new tab in the same terminal window. But it opens a new window instead.

在终端,我希望它在同一个终端窗口中打开一个新选项卡。但它会打开一个新窗口。

I found out that its intention is to open a new tab in a new window, i.e., if I write:

我发现它的意图是在新窗口中打开一个新选项卡,即,如果我写:

gnome-terminal --tab --tab

It will open a new window with two tabs.

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

So, the question is, how can I open a new tab in the currentwindow using a command in gnome-terminal?

所以,问题是,如何使用 in 中的命令在当前窗口中打开一个新选项卡gnome-terminal

采纳答案by utkarsh dubey

#!/bin/sh

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

This will auto determine the corresponding terminal and opens the tab accordingly.

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

回答by Mike McQuaid

I don't have gnome-terminal installed but you should be able to do this by using a DBUS call on the command-line using dbus-send.

我没有安装 gnome-terminal,但您应该可以通过在命令行上使用 DBUS -send调用 DBUS 来完成此操作。

回答by Chase Seibert

You can also have each tab run a set command.

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

gnome-terminal --tab -e "tail -f somefile" --tab -e "some_other_command"

回答by user25643

Consider using Roxterm instead.

考虑改用 Roxterm。

roxterm --tab

opens a tab in the current window.

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

回答by Jemini

For anyone seeking a solution that does not use the command line: ctrl+shift+t

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

回答by jno

A bit more elaborate version (to use from another window):

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

#!/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 '==pid{print ;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 #

回答by Pallavi Varne

For open multiple tabs in same terminal window you can go with following solution.

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

Example script:

示例脚本:

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 

回答by jaogoy

I found the simplest way:

我找到了最简单的方法:

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

I use tmuxinstead of using terminal directly. So what I want is really a single and simple command/shell file to build the development envwith several tmuxwindows. The shell code is as below:

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

#!/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

回答by NeoGeek

To bring together a number of different points above, here's a script that will run any arguments passed to the script vim new_tab.sh:

为了将上面的许多不同点结合在一起,这里有一个脚本,它将运行传递给脚本的任何参数vim new_tab.sh

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

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

Next make it executable: chmod +x new_tab.sh

接下来使其可执行: chmod +x new_tab.sh

Now you can use it to run whatever you'd like in a new tab: ./new_tab.sh "watch ls -l"

现在您可以使用它在新选项卡中运行您想要的任何内容: ./new_tab.sh "watch ls -l"

回答by Frank Nocke

Just in case, you want to open

以防万一,你想打开

  • a new window
  • with two tabs
  • and executing command in there
  • and having them stay open...
  • 一个新窗口
  • 有两个标签
  • 并在那里执行命令
  • 并让它们保持开放...

here you go:

干得好:

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"

(same for mate-terminalbtw.)

mate-terminal顺便说一下。)