I'd like to know or it is possible to set up the GNOME Terminal in a way that when I open up the terminal and there is already an instance open it'll show that instance and not open up a brand new one.
-
what version of ubuntu are you using, and which environment? I ask, because what you are describing is the default setup in unity and gnome-shell.– martinNov 24, 2011 at 12:57
-
I’m using Ubuntu 11.10 with Unity-2D. I haven’t done a clean install but an upgrade from 11.04.– MoodNov 25, 2011 at 0:46
-
Do you use Ctrl+Alt+T as a shortcut?– int_uaDec 9, 2011 at 12:54
-
Yes I do.That or I start up a terminal typing “ter » enter” in Kupfer. Those are the the 2 methods I use. Because I don't look on the Unity laucher all too often I end up with 3, 4 different terminals.– MoodDec 10, 2011 at 17:18
6 Answers
I wrote a simple shell script using wmctrl to give the Terminal focus. It does exactly what I want.
First, install wmctrl sudo apt-get install wmctrl
.
Next, fire up a text-editor, copy the following lines.
#!/bin/sh
SERVICE='gnome-terminal'
if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
wmctrl -xa $SERVICE
else
$SERVICE
fi
Save the file somewhere where you usually keep your shell scripts. In my case I did: ~/bin/single_instance.sh
cd to the directory (cd ~/bin
) and make the file executable chmod +x single_instance.sh
Now, open up Main Menu select Accessories » Terminal press properties and replace command with the script: ~/bin/single_instance.sh
Open up System Settings » Keyboard » Shortcuts. Disable Launch Terminal shortcut by giving it a backspace. Make a custom shortcut. Again fill in the path and name to the script and give it the key combination ctrl + alt + t.
You’re done, single instance terminal...
-
Why not just make your script the default terminal launcher? Why all the steps to add shortcuts and all? +1 for the script, -1 for the implementation. Dec 12, 2011 at 16:18
-
In all honestly… I don't know how to do that. I’ve looked at default applications and didn't see terminal. Also I'm convinced that the previous version of Ubuntu had text-fields where commands could be entered. This version of default apps is all drop down menus.– MoodDec 12, 2011 at 19:21
-
Add --tab to the launcher command. Then you end up with only one app. open.
~$ gnome-terminal --help-all
:
--tab Open a new tab in the last-opened window with the default profile
-
No, I don't want a new tab. I want the terminal to move to the foreground in the state I last left it.– MoodDec 12, 2011 at 11:28
-
1
Method 1 - Remap gnome dock shortcut
- Add gnome-terminal into dock (favortie), I move it to the third application, please change the number 3 in following article to the number you used.
- Disable terminal shortcut in Settings/Kebyaord/Keyboard Shortcuts
- Reset the shortcut to for start dock applications
gsettings set org.gnome.shell.keybindings switch-to-application-3 "['<Ctrl><Alt>T']"
Method 2 (Xorg only) - Reset gnome default terminal
- Create a script at
~/bin/single_gnome_terminal.sh
#!/bin/sh
SERVICE='gnome-terminal'
if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
wmctrl -xa $SERVICE
else
$SERVICE
fi
- Enable execution permission
chmod +x ~/bin/single_gnome_terminal.sh`
- Install
wmctrl
sudo apt-get install wmctrl
- Change default terminal
gsettings set org.gnome.desktop.default-applications.terminal exec single_gnome_terminal.sh
References:
It is the default behavior if you use Mod4+#
(Mod4
= Super = "Windows" key, #
is number in launcher) shortcut to open any application.
-
Yes I’m aware of that. Still, I have to have a terminal open to begin with. I meant when I press control + alt + t or open it up in Kupfer. I’m looking for the same behavior as – for example – Chrome: If an instance is open show me that instance, if there no instance open a new instance.– MoodDec 10, 2011 at 13:04
I generalized and simplfied @Mood 's script a bit. Now it takes a command line parameter and use pgrep instead.
sudo apt-get install procps
The script:
#!/usr/bin/env bash
if pgrep $1
then
wmctrl -xa $1
else
$1
fi
I'd like to add my own script, since I find it extremely useful:
- If there is a gnome-terminal in the current workspace, then it brings it to the front and gives it focus
- If there isn't any gnome-terminal in the current workspace, it switches the workspace and brings the first existing gnome-terminal it finds to front and gives it focus
- If there are no gnome-terminal instances running, it opens a new instance, brings it to front and gives it focus
I configured Ctrl+Alt+T shortcut to run this script and I find it even more comfortable than Guake or Tilda.
#!/usr/bin/env sh
# APP is the binary name you see when you do ps
APP="gnome-terminal"
# WIN_CLASS is the window class you see when you do wmctrl -lx
WIN_CLASS="gnome-terminal-server.Gnome-terminal"
# Use the app from current workpace first, or any if not available
WORKSPACE=$(wmctrl -d | grep '\*' | cut -d ' ' -f1)
TPID=$(ps aux | pgrep $APP)
if [ "$TPID" ]
then
QTERM_WIN=$(wmctrl -lx | grep $WIN_CLASS | grep $WORKSPACE | awk 'NR == 1 {print $1}')
if [ "$QTERM_WIN" ]
then
wmctrl -ia $QTERM_WIN
else
# No valid app found in the current workspace, switch to any
QTERM_WIN=$(wmctrl -lx | grep $WIN_CLASS | awk 'NR == 1 {print $1}')
wmctrl -ia $QTERM_WIN
fi
else
$APP &
fi
I use it with QTerminal but I modified to fit your needs. Good luck