Custom Key to Instantly Switch to a Window in Ubuntu

F8 keyLately, I began developing my projects in Emacs. One of the productivity tips that is recommended is to define a system wide keyboard key shortcut that takes you from any window that is currently in focus back to Emacs. I was looking into how to implement this behavior. Once I found out, I decided  to share it with you here as it can be quite handy for all sorts of applications.





We will use a program called wmctrl. Install it into your system:

sudo apt-get install wmctrl

Once installed, see the list of open/existing windows using this command:

wmctrl -l

In my case, I get something like this:

0x02200110  1 petr-linux plasma-desktop
0x01a0001a  0 petr-linux Krusader
0x03e0001a  0 petr-linux petr : vim – Konsole
0x016000e2  0 petr-linux emacs24@petr-linux
0x03e0005d  0 petr-linux petr : bash – Konsole

In my case, the goal is to set a system wide key binding to the F8 key such that pressing the key automatically switches focus to my Emacs window. To do that, having Emacs open, it shows in the list above as emacs24@petr-linux. Now if you go to your terminal and issue:

wmctrl -a emacs24@petr-linux

the Emacs window will instantly become front and focused. To bind this action to the F8 key (I am on Kubuntu, Ubuntu’s menu will be a bit different), go to System Settings -> Shortcuts and Gestures -> Custom Shortcuts and create a new global shortcut for a command/URL. Then switch to the Trigger tab and set the key to F8. Finally, in the Action tab, specify the command: wmctrl -a emacs24@petr-linux (you’ll have to, of course, replace emacs24@petr-linux with what your list of active windows shows). Hit Apply and you’re set. Pressing F8 at any point should instantly take you to Emacs.

Bookmark the permalink.