JP Vossen on 26 Jan 2010 15:13:18 -0800


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] Hotkey to bring a window into the forground?


> Date: Fri, 22 Jan 2010 19:19:11 -0500
> From: jim fisher<jedijf@myfisher.org>
>
> On 1/22/10, JP Vossen<jp@jpsdomain.org>  wrote:
>> Way back when, in Windows you could assign a hot-key to bring a window
>> into the foreground. [...]
>>
>> I know that System>  Preferences>  Keyboard Shortcut has "Run a
>> terminal" but I don't want to run a new one, I want to bring my existing
>> one into the foreground.  How do I do that in Ubuntu/Gnome?
>
> wmctrl and a script.
>
> http://superuser.com/questions/16647/custom-hotkey-shortcut-to-open-bring-to-front-an-app
>
> See #2

Sorry about the latency, life happens.

This is cool!  And it worked.  Eventually.

Short version:
System > Preferences > Keyboard Shortcuts
	[Add]
		Name: Roxterm to Foreground
		Command: bash -c "wmctrl -a \"$(wmctrl -lp | grep $(pgrep roxterm) | 
awk '{print $5}')\""
	Shortcut: Alt+1

I *love* Linux/GNU/Ubuntu (and Gnome, kinda).

The above needs some work and is probably better implemented as a short 
shell script, but it works!


Long version:

It didn't work originally because my window titles keep changing.  Since 
I'm talking about a terminal window, the "title" of the window is the 
current tab, which in turn is "user@host:/path" which varies a lot.  It 
looks like wmctrl can do case insensitive sub-strings, but that didn't 
help as I don't have a least common denominator.

I tried creating one by changing the Roxterm title to "rxtm: %s" but 
that didn't seem to take.  I also hate to waste the space.

roxterm doesn't record an obvious PID in /var/run and wmctrl doesn't 
list the process name, only the window title (which changes).  But 
wmctrl *can* also list the PID (-lp).  So if I can find the PID, I can 
then find the window title, and then active it!

To break down the nested command:

$() is a more readable and easily nestable version of `` command 
substitution.

$(pgrep roxterm)	= Print out the PID of "roxterm"
grep $(pgrep roxterm)	= Grep for that PID from --\/
wmctrl -lp | grep...	= Print list of window PIDs and titles
awk '{print $5}')	= Print just the title from --/\
wmctrl -a \" ... \"	= Activate the given title from --/\
			# Quotes escaped due to below
bash -c ""   		= Run this command in a shell

This will fail silently if there is no roxterm or more than 1 roxterm 
already running, but adding anything else to the one-liner will get 
(more) ugly.  A couple line shell script will fix that, but stay readable.

Also, some folks might notice that 'grep foo | awk ...' can be reduced 
to "awk '/foo/ {print $5}'" which is faster and more efficient.  The 
problem with that is handling all the quotes in the one-liner gets 
out-of-hand quickly.  I'll probably do it in awk in the script version, 
which I'll try to remember to post if/when I get it working.

Thanks,
JP
----------------------------|:::======|-------------------------------
JP Vossen, CISSP            |:::======|      http://bashcookbook.com/
My Account, My Opinions     |=========|      http://www.jpsdomain.org/
----------------------------|=========|-------------------------------
"Microsoft Tax" = the additional hardware & yearly fees for the add-on
software required to protect Windows from its own poorly designed and
implemented self, while the overhead incidentally flattens Moore's Law.
___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug