Hotkeys for fast window switching

Hotkeys for fast window switching

One of the features I’ve really been missing in Gnome 3 (and Gnome 2, too) for a long time is the ability to directly jump to a given window by using a key combination. Some other desktop environments offer such a feature per default: Unity uses META+[NUMBERKEY] as it maintains a globally unique window order ([NUMBERKEY] signifies an application’s position on the panel), similarly to Windows 7, which uses the same key combination. KDE4, on the other hand side, does not maintain a strict order amongst all running applications on all workspaces etc, but it allows users to define a per application shortcut, enabling you to jump to the first launched window of that application (I like this feature very much 🙂 ).

Well, Gnome Shell, to my knowledge, does not offer any corresponding function. I think it would be possible to implement it the Unity way, given the fact that Gnome Shell’s Activities Dock does offer an application order, but I’m not that much into the code yet to implement it myself. Instead, I proceeded the KDE4 way and used a very nice and powerful tool, wmctrl. The following is a generic, desktop environment independent solution, as it is based upon executing a script which can be simply bound to a key combination in most DEs. The script checks whether a given application is running and switches to it; if it isn’t, it launches it. Proceed as follows.

  1. Install wmctrl
  2. Create a helper script: ~/.activate.sh
  3. Determine your applications’ WM_CLASSes
  4. Bind using your desktop environment’s keyboard settings

 

Install wmctrl

Fedora:

sudo yum install wmctrl

openSUSE:

sudo zypper install wmctrl

Ubuntu:

sudo apt-get install wmctrl

See http://tomas.styblo.name/wmctrl/ for more info.

 

Create a helper script: ~/.activate.sh

Save the following as ~/.activate.sh:

#!/bin/sh

if [[ -z "$1" || -z "$2" ]] ; then
echo "Usage: WM_CLASS COMMAND"
echo "WM_CLASS: the window manager window class to try to switch to"
echo "COMMAND: the command to execute on failure"
exit ; fi

WM_CLASS="$1"
COMMAND="$2"
EXISTS=$(wmctrl -l -x|grep $WM_CLASS)

if [[ -z "$EXISTS" ]]; then
$COMMAND
else
wmctrl -x -F -a $WM_CLASS
fi

Make it executable:

chmod +x ~/.activate.sh

 

Determine your applications’ WM_CLASSes

A WM_CLASS identifies the windows of a given application. You can now launch all applications you’d like to bind to key combinations and use

wmctrl -l -x

to view the appropriate WM_CLASSes (which are shown as the third column of the output). For example (KDE4):

                                           
0x02600304 -1 Plasma.Plasma         lrs plasma-desktop
0x0540014d  0 dolphin.Dolphin       lrs Download – Dolphin
0x05600166  1 ksysguard.Ksysguard   lrs System Monitor
0x05200150  0 konsole.Konsole       lrs lores : bash
0x01600147  3 amarok.Amarok         lrs Paused  ::  Amarok
0x05a000c0  0 Navigator.Firefox     lrs l0res - Mozilla Firefox
0x070000c0  0 Mail.Thunderbird      lrs Inbox - Mozilla Thunderbird

 

Bind using your desktop environment’s keyboard settings

You can focus/launch an application — with a given WM_CLASS, started with COMMAND — by running

~/.activate.sh WM_CLASS COMMAND

For instance, if I wanted to activate my KDE Konsole, I’d run

/home/lores/.activate.sh konsole.Konsole konsole

Knowing this, you’re now able to bind any application to any key combination your desktop environment supports. In order to do this, you need to know where your DE lets you change your hotkey settings, and in there, you need to create a new/custom entry and let it execute your new script with the two parameters. Now you bind some keys to this entry and you’re done!

 

I’ve tested this in Gnome Shell and KDE4, but as wmctrl is compatible with the X Window Manager, many other DEs, including Gnome 2, Mate and Unity, should do fine.

,

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.