6

I'm using Gnome. In vim, I want to map a key to switch to Firefox. I know that I should use bash commands (a command in form of !...). Is it possible to switch to an application using its PID?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Omid
  • 3,311
  • 8
  • 34
  • 40
  • So, in X terms, you want to raise and focus a window from a shell? – bahamat Aug 09 '12 at 16:56
  • 1
    You probably need to get the [x window id](http://stackoverflow.com/questions/151407/how-to-get-an-x11-window-from-a-process-id) from the pid and send a [_NET_ACTIVE_WINDOW](http://standards.freedesktop.org/wm-spec/wm-spec-latest.html#id2577229) message afterwards – Ulrich Dangel Aug 09 '12 at 17:33

1 Answers1

15

The first two examples will activate the first listed Firefox window, based on its Title. Firefox always ends its Title-bar with "Mozilla Firefox"... "listed" means: as listed by wmctrl querying X. The first example uses a fuzzy match for the title.

wmctrl -a "Mozilla Firefox"

Or, to get the title more specifically:

wmctrl -Fa "$(wmctrl -l | sed -rn 's/^([^ ]+ +){3}(.*Mozilla Firefox)$/\2/p')"

If you know the PID of the process behind a window, you can use this command:

wmctrl -ia $(wmctrl -lp | awk -vpid=$PID '$3==pid {print $1; exit}') 
Peter.O
  • 32,426
  • 28
  • 115
  • 163