8

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.

4
  • 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.
    – martin
    Nov 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.
    – Mood
    Nov 25, 2011 at 0:46
  • Do you use Ctrl+Alt+T as a shortcut?
    – int_ua
    Dec 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.
    – Mood
    Dec 10, 2011 at 17:18

6 Answers 6

9

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...

3
  • 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.
    – Mood
    Dec 12, 2011 at 19:21
  • anyway to get this working with wayland?
    – Brad
    Apr 28, 2022 at 6:07
2

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
2
  • No, I don't want a new tab. I want the terminal to move to the foreground in the state I last left it.
    – Mood
    Dec 12, 2011 at 11:28
  • 1
    This just opens new windows for me on Ubuntu 16.04
    – beruic
    Jul 26, 2016 at 8:45
1

Method 1 - Remap gnome dock shortcut

  1. 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.
  2. Disable terminal shortcut in Settings/Kebyaord/Keyboard Shortcuts
  3. 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

  1. 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
  1. Enable execution permission
chmod +x ~/bin/single_gnome_terminal.sh`
  1. Install wmctrl
sudo apt-get install wmctrl
  1. Change default terminal
gsettings set org.gnome.desktop.default-applications.terminal exec single_gnome_terminal.sh

References:

  1. https://askubuntu.com/a/1194659
  2. https://askubuntu.com/a/87109
  3. https://unix.stackexchange.com/a/510376
0

It is the default behavior if you use Mod4+# (Mod4 = Super = "Windows" key, # is number in launcher) shortcut to open any application.

1
  • 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.
    – Mood
    Dec 10, 2011 at 13:04
0

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
0

I'd like to add my own script, since I find it extremely useful:

  1. If there is a gnome-terminal in the current workspace, then it brings it to the front and gives it focus
  2. 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
  3. 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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .