Toggling between two applications can be done, as you know, with Alt+Tab. But it's designed go through all of the open windows by Z-order; so every time a third application is activated, the Z-order changes and it's usually annoying to get the two apps you're mainly interested back in sync for switching.
I read your requirement to be that you have a main application Chromium and a secondary application, let's go with
gnome-calculator, that you switch to frequently and would like a foolproof toggle between them using a single keybinding.
You can, however, create this behavior with a script and a custom keybinding. To create the script, I wanted something simple (for me) to understand and that could be easily modified without compiling; so I used python.
#windows are found through the titlebar, not the app name
#edit the titles to change the apps
titlePrimaryApp = re.compile(".*Chromium*")
titleSecondaryApp = re.compile("Calculator")
screen = wnck.screen_get_default()
#needed for the code that gets the windows list to work
active_window = screen.get_active_window()
open_windows = screen.get_windows()
#if the primary app is already activated, find the secondary app, and activate it
for w in open_windows:
#otherwise, activate the primary app
for w in open_windows:
To use this script:
- Save it in a file such as
myswitcher.py. A good place for it is
- Make it executable:
chmod +x myswitcher.py
- You will need:
sudo apt-get install python-wnck. This is the library that works with Gtk windows. It is not installed by default.
Link the script to a keybinding.
The easiest way to do this is to create a custom shortcut in
System Settings >> Keyboard >> Shortcuts >> Custom Shortcuts.
After highlighting Custom Shortcuts in the list on the left, click the plus sign to add a shortcut.
In the popup window, choose a name for the shortcut and enter the command
The new shortcut will be added to the list of shortcuts in the window. To the right of the name will be the word disabled. Set the keybinding by clicking the word disabled, then typing the key combination that you want.
Make sure you choose a keybinding that doesn't conflict with other programs. Those bindings are set at a lower level and will preempt the script. I chose Ctrl+ \ (backslash) since it is not used by anything else and is more or less the mirror image of Alt+Tab (at least on my keyboard layout).
You need to log out and log back in before the custom keybinding will work.
Credit for help with the script:
I had most of the script logic worked out before looking around for examples (and there's no shortage of
wnck examples out there); but I need to credit this answer on StackOverflow that solved some problems I was having with the regular expressions usage, the syntax for passing in the gtk timestamp, and most importantly providing the key lines about the Gtk events. Without those 2 lines, get_windows returns an empty list.