http://github.com/einars/dotfiles/blob/master/bin/focus-or-run
KDE, both the good old 3.5 and new clunky 4, has vast options for customizing the window behaviour. For example, you can ask the chat windows to automatically open in one workspace, gimp — in other, and so on.
When using Gnome, the feature I’m missing most is a possibility to assign window shortcuts: it is incredibly convenient to get rid of the alt-tab and use shortcuts like Alt-A to switch to firefox from anywhere.
I’ll show how to emulate this using a custom script in Gnome, and even make it better, opening the application in question if it’s not running.
You’ll need:
a folder where you store all your own scripts. If you’re using linux and working from console, you’ve probably already figured out that you need a place to drop all your scripts and helpers and whatnot. Usually ~/bin is used — just make a folder “bin” in your home folder, if you don’t have any.
wmctrl. Wmctrl is a small script which allows other scripts to communicate with windows on screen and control them. It is available in stock ubuntu repository (universe) as “wmctrl”, the same in Arch Linux community repository, and generally you shouldn’t have problems finding and installing it.
zsh. Zsh is an interpreter which will run our script. The default shell interpreter in most of the linux distributions is called bash, yet zsh is easier to use and offers prettier scripting capabilities. There is no reason why the script couldn’t be written in bash or other interpreter of choice: I just use zsh for everything myself.
The script
You can view and dowload the complete script here: http://github.com/einars/dotfiles/blob/master/bin/focus-or-run
It is called “focus-or-run” and it takes two parameters: the first is window class or a fragment of the title. Script will try to find the matching window and bring it to foreground.
The second is an optional program name you wish to run in case the window was not found.
So, this command:
focus-or-run firefox /usr/bin/firefox
will attempt to find a window which belongs to “firefox” and, if found, focus that. If the window was not found, it will run /usr/bin/firefox and then focus the window again.
focus-or-run firefox
Will actually work just fine, too.
Assigning the hotkeys
This is where the System-Preferences-Keyboard Shortcuts applet comes in handy. Some time ago it was reworked so that adding custom shortcuts now is a breeze.
First, you have to create a new action by clicking Add, and filling in the information like this:

You’ll find the new command in the list below; you can now assign any keyboard shortcut to it:

That’s all. Now you can press Alt-A anytime you want to get to that firefox window, and it’ll pop up.
I’ll leave assigning other most frequently used software to shortcuts as a home exercise :)
(view comments)