9

I am configuring Terminator for my desktop. I use the Ctrl + Alt + T shortcut to launch it. Since I want only a single instance running, I use run-one terminator as the launcher command.

Now if an Terminator instance is running, a new instance isn`t launched, but the existing instance does not get focused.

Any ideas as to how I could get the Terminator window to get focus?

Mr Hyde
  • 483

3 Answers3

8

Here is a method that works without run-one assuming wmctrl is available:

wmctrl -xa terminator.Terminator || terminator

This assumes the default window class name is being used. You can get creative if you want and use a custom class name when launching terminator. This will allow you to keep your terminator shortcut separate from a normal terminator instance:

wmctrl -xa MyCustomTerminator.Terminator || terminator -c MyCustomTerminator

This just builds on Denis' answer without the need for run-one.

loleary
  • 81
  • Any idea how to make it toggle the focus? – Joe Eifert Jun 01 '17 at 11:01
  • Great solution. I had to use bash -c 'wmctrl -xa terminator.Terminator || terminator' on Ubuntu 18.10 using Gnome 3.30.1. Otherwise the shortcut would only work once terminator was running already. – vice_spirit Jan 09 '19 at 10:53
4

Try to install wmctrl : sudo apt-get install wmctrl
now the command wmctrl -a STRING gives focus to a window containing STRING in its title
so your final command will be:

run-one terminator; wmctrl -a Terminator
Denis
  • 1,013
1

Here is the hack I have settled upon using run-one and xdotool

In /home/(user)/my_scripts/single_terminator_instance.sh

#!/usr/bin/bash

# run a single instance of terminator
/usr/bin/run-one /usr/bin/terminator

# search for the terminator window and focus!!!
/usr/bin/xdotool search --onlyvisible --class terminator windowactivate

Then

  1. Change Terminator shortcut command in Main Menu to point to the above script
  2. In Keyboard shortcuts, Ctrl + Alt + T activates the script

While searching across the i'net, I also chanced upon this

Since I am new to bash scripts, I welcome any refinements to the hack!

Mr Hyde
  • 483
  • Easier: /usr/bin/wmctrl -xa terminator :-) instead of xdotool! Thanks Denis. – Mr Hyde Dec 18 '11 at 19:55
  • you are right! right way is wmctrl -xa terminator – Denis Dec 23 '11 at 22:16
  • I tried your solutions, but run-one doesn't work on my computer with Terminator. It always launches a new instance. Do you have any idea why? Thanks. – Tamás Barta Feb 22 '14 at 19:38
  • ps aux | grep terminator returns nothing. The process runs as x-terminal-emulator for me, so that might be the cause. I did manual check like this:

    RUNNING=$(ps aux | grep x-terminal-emulator | grep -v grep)

    if [[ -z $RUNNING ]]; then x-terminal-emulator & fi

    – Tamás Barta Feb 22 '14 at 19:55
  • @BartaTamás Did you solve your problem. My script is just two lines of bash attached to keyboard shortcut. I now use it with gnome-terminal and tmux, and am very happy with the solution. But it also works reliably with terminator. Maybe the same keyboard shortcut is assigned twice (Ctrl+Alt+T). Just guessing! – Mr Hyde Feb 22 '14 at 21:57