可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
When I write
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 current window using a command in gnome-terminal
?
I'm using Ubuntu 9.04 x64.
回答1:
#!/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
This will auto determine the corresponding terminal and opens the tab accordingly.
回答2:
You can also have each tab run a set command.
gnome-terminal --tab -e "tail -f somefile" --tab -e "some_other_command"
回答3:
I found the simplest way:
gnome-terminal --tab -e 'command 1' --tab -e 'command 2'
I use tmux
instead of using terminal directly. So what I want is really a single and simple command/shell file to build the development env
with several tmux
windows. The shell code is as below:
#!/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
回答4:
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 '$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 #
回答5:
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
:
#!/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;
Next make it executable:
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"
回答6:
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.
回答7:
Consider using Roxterm instead.
roxterm --tab
opens a tab in the current window.
回答8:
For anyone seeking a solution that does not use the command line: ctrl+shift+t
回答9:
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-terminal
btw.)
回答10:
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