How to Create a Shortcut to Raise an Application’s Window

shortcut-keyswindow-managerwmctrl

I constantly switch between Sublime Text, Terminal and Chrome. I'd like to bind shortcut like "Alt + 1" so my current Terminal and current Google Chrome opens.

Right now, each time i launch Google Chrome or the terminal, it opens a new window. Any idea ?

Thanks !

Best Answer

The method in drizzle's answer will work, but has a few downsides

  • It has a delay; when pushing the Super key, you'd have to wait about a second to make the numbers appear.
  • The order of the items in the launcher might be changing, depending on the applications you run. The number of Chrome might be different then.

    enter image description here

Alternative option

You could simply do with the command:

wmctrl -a chrome

which will raise the first found window with "chrome" in its name. That would however also raise the window I am writing in now (a firefox window, named: "Shortcut to open current Google Chrome...")

The use of the window's pid will prevent this. In my opinion, it is therefore more elegant to put the command:

wmctrl -ia "$(wmctrl -lp | grep "$(pgrep chrome)" | tail -1 | awk '{ print $1 }')"

under a shortcut key (Alt+1 will work), given the fact that you'd need to have wmctrl installed then:

sudo apt-get install wmctrl

How to add the command to a shortcut

  1. Install wmctrl

    sudo apt-get install wmctrl
    
  2. create a small script (open an empty file, paste the script below in it)

    #!/bin/bash
    wmctrl -ia "$(wmctrl -lp | grep "$(pgrep chrome)" | tail -1 | awk '{ print $1 }')"
    

    Save it as raise_chrome.sh

  3. Add the script to a shortcut; choose: System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add the command:

    /bin/bash /path/to/raise_chrome.sh
    

    to (e.g.) the shortcut Alt+1.

    N.B. Use an absolute path, not e.g. ~

Explanation

The command is in fact a "conversion" of this one, and the explanation is similar:

In the section:

"$(wmctrl -lp | grep "$(pgrep chrome)" | tail -1 | awk '{ print $1 }')"

the command:

wmctrl -lp

lists all windows, including their pid and window- id. The result is piped to

grep "$(pgrep chrome)"

which filters (lists) all occurrences of Chrome windows.

Then,

tail -1

will give us the last item in the list, which is the latest created window (since wmctrl lists windows chronologically).

awk '{ print $1 }'

finally, will give us the first string in the line (from the output of wmctrl), which is the window- id.

The command:

wmctrl -ia <window_id>

Will then raise the last created window of Chrome

Note

No need to say that you can replace chrome in the command by any other application (process name), and place them under separate key combinations.


EDIT

use the application as an argument

Since you mention Sublime Text, Terminal and Chrome, you might want to use multiple shortcuts, and run the script with the application as an argument.

To do so

Use the script with slight difference:

#!/bin/bash
app=$1
wmctrl -ia "$(wmctrl -lp | grep "$(pgrep "$app")" | tail -1 | awk '{ print $1 }')"

and run it subsequently with the commands under the shortcut keys:

sh /home/jacob/Bureaublad/raise.sh sublime_text
sh /home/jacob/Bureaublad/raise.sh gnome-terminal

and

sh /home/jacob/Bureaublad/raise.sh chrome