16

I tend to use the terminal a lot,

So I wondering if there is a way I can make Ctrl+Alt+T focus the existing terminal if there is one, otherwise create a new terminal?

24

Create a small script which will raise the GNOME Terminal:

echo 'xdotool windowactivate $(xdotool search --onlyvisible --class gnome-terminal)'> ~/raiseterminal.sh && chmod +x ~/raiseterminal.sh

or if you want to check if Terminal is already running, use:

echo -e $'if ps aux | grep "[g]nome-terminal" > /dev/null\n then xdotool windowactivate $(xdotool search --onlyvisible --class gnome-terminal)\n else gnome-terminal &\nfi' > ~/raiseterminal.sh && chmod +x ~/raiseterminal.sh

This will create the script ~/raiseterminal.sh with this content:

if ps aux | grep "[g]nome-terminal" > /dev/null                                                                                                                                                                                
 then xdotool windowactivate $(xdotool search --onlyvisible --class gnome-terminal)                                                                                                                                            
 else gnome-terminal&                                                                                                                                                                                                          
fi          

Open the preferences to set up a custom keyboard shortcut and set the command to /home/$USER/raiseterminal.sh, but make sure to change $USER to your actual username.

If you only want to raise the terminal on a specific screen or desktop, see xdotool search --help for more information on how to do this.

There are also various other methods which work better with other window managers.

  • You could add a | head -n1 after searching gnome-terminal windows to prevent an error if more than one windows are found – ggalmazor May 12 '17 at 7:17
8

why don't you try tilda or guake, both available in ubuntu repositories. Although they don't specifically do what you're after, I'm sure they are that thing that you were looking for but did not know it existed. ;)

EDIT: ok, I was a bit vague, more information follows:

from wikipedia:

Tilda is a GTK+ terminal emulator. Its design was inspired from consoles in computer games such as Quake which slide down from the top of the screen when a key is pressed, typically the tilde, and slide back up when the key is pressed again.

Running Tilda can be faster than launching a new terminal with a keyboard shortcut because the program is already loaded into memory; it can be useful to people who frequently find themselves opening and closing terminals for odd tasks.

guake is really the same thing, the only difference I noticed is that I couldn't make it open http links by ctrl+click which I found annoying

1

My version (=

Script to run/raise any app:

PID=$$
xdotool search --class $1 | while read line
do
  echo "$line"
  if [ `xdotool windowactivate $line 2> /dev/stdout | grep -c fail` -eq 0 ]
    then
    kill $PID
    exit
  fi
done
## Launch the program if we reach here
$1 & disown

e.g.

sh ~/raise.sh chromium
0

try

sudo apt-get install wmctrl
wmctrl -xa 'gnome-terminal-server.Gnome-terminal'

go to system settings - Keyborad, add a custom shortcut, and paste the wmctrl command there. It works.

where the gnome-terminal-* string is from

wmctrl -xl
0

Yet another option: launch or switch. The script relies on wmctrl to check whether a window is already open. If it is, the script switches to an existing one, giving priority to an existing window on the current desktop. Otherwise, a new window is lauched. This script is published by Vaughn Dickson.

#!/bin/sh

terminal_wm_class="gnome-terminal"
terminal_exec="gnome-terminal"

# no terminal started, so start one
if [ -z "`wmctrl -lx | grep gnome-terminal`" ]; then
    $terminal_exec &
else
    # search for existing terminals on current desktop
    current_desk=`wmctrl -d | grep '*' | cut -d ' ' -f 1`
    term_on_this_desk=`wmctrl -lx | grep "$current_desk[ ]*$terminal_wm_class" | cut -d ' ' -f 1`
    if [ -n "$term_on_this_desk" ]; then
        wmctrl -i -a $term_on_this_desk
    else
        # no terminals on current desktop, so just open the first one we find
        wmctrl -x -a $terminal_wm_class
    fi;
fi;

Place this script in the bin folder in your home folder and make it executable. Then under Keyboard Shortcuts (Settings - Keyboard), disable the existing hotkey for "Launch terminal" under the section "Launchers": click on it, then press Backspace to disable the current assignment. Then, in the section "Custom Shortcuts", create a new custom shortcut by clicking the + icon. Fill out the name of your script as the "command" and assign it the Ctrl+Alt+t shortcut.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.