Binding keys to raise windows by name

samtregar on 2005-03-24T19:57:16

I generally program with three windows open - Emacs, Firefox and Gnome-Terminal. I also like huge fonts so there's no way I can see the contents of all three windows simultaneously. I've gotten used to using alt-tab to cycle through them, but it's a pain. I know which one I want but I always have to look for it.

To solve this problem I wrote a script which uses wmctrl to raise a window on the current desktop by name. Here it is:

#!/usr/bin/perl
use strict;
use warnings;

my $what = shift;
die "Missing what arg." unless $what;

my $desktops = `/usr/local/bin/wmctrl -d`;
my ($active_desktop) = $desktops =~ /^(\d+)\s+\*/m;
die "Unable to determine active desktop!" unless defined $active_desktop;

my $windows = `/usr/local/bin/wmctrl -l`;
my ($id) = $windows =~ /^(0x\w+)\s+\Q$active_desktop\E\s+\w+.*\Q$what\E/m;
die "Unable to find $what on $active_desktop!" unless $id;

system('/usr/local/bin/wmctrl', '-i', '-a', $id);

I called it raise.pl and call it like:

$ raise.pl emacs

Then to finish the job I edited the Metacity configuration to bind Alt-e to raise Emacs, Alt-f to raise Firefox and Alt-s to raise the terminal (shell). Mission accomplished!

(Of course, all this would be much easier if Metacity supported a scripting language like Sawfish did. I miss Sawfish...)

-sam


Thank You!

Smylers on 2005-03-28T17:02:30

Hey, thank you so much for this. It's something I've hankered after for a while but never got round to trying to fix (I didn't know about wmctrl).

Smylers

more screen real estate

mw487 on 2005-04-05T13:47:43

Have you ever considered a multi-head set up? I come from the world of Macintosh, and it is hard to go back to just one monitor.

What are the pros and cons of multihead versus switching? Is it a *nix thing? With my background, I never grokked emacs. jEdit has replaced BBEdit for me...