Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

samtregar (2699)

samtregar
  (email not shown publicly)
http://sam.tregar.com/

Journal of samtregar (2699)

Thursday March 24, 2005
02:57 PM

Binding keys to raise windows by name

[ #23828 ]
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

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • 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
  • 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...