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
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.Alternative option
You could simply do with the command:
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:
under a shortcut key (Alt+1 will work), given the fact that you'd need to have
wmctrl
installed then:How to add the command to a shortcut
Install
wmctrl
create a small script (open an empty file, paste the script below in it)
Save it as
raise_chrome.sh
Add the script to a shortcut; choose: System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add the command:
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:
the command:
lists all windows, including their pid and window- id. The result is piped to
which filters (lists) all occurrences of
Chrome
windows.Then,
will give us the last item in the list, which is the latest created window (since
wmctrl
lists windows chronologically).finally, will give us the first string in the line (from the output of
wmctrl
), which is the window- id.The command:
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
andChrome
, 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:
and run it subsequently with the commands under the shortcut keys:
and