Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a way, in bash command line, to give focus to a specific window of a running process. Assume I know the process' name, number, and anything else I need.

For instance, if I have a single instance of Firefox running, but it's minimized (or there's some other window on top of it). I need a bash command that brings up and gives focus to the Firefox window, by making it the active window.

share|improve this question
up vote 42 down vote accepted

The wmctrl command seems to do the job. It was already installed for me, but it's available in the repositories in case anyone needs it.

wmctrl -l 

Lists currently open windows (including the gnome panels).

wmctrl -a STRING

Gives focus to a window containing STRING in its title. I'm not sure what happens if more than one window meets that condition.
In my case the command was:

wmctrl -a Firefox
share|improve this answer
+1, works for me too. – Peter Jaric May 19 '10 at 15:56
Nice to see someone is reading and I'm not just rambling to myself. =) – Malabarba May 20 '10 at 14:59
Also try xdotool. – Andres Riofrio Apr 25 '12 at 7:04

Using wmctrl in combination with xdotool you can switch focus to Firefox and then perform keyboard or mouse actions.

In this example:

wmctrl -R firefox && \
  xdotool key --clearmodifiers ctrl+t ctrl+l && \
  xdotool type --delay=250 google && \
  xdotool key --clearmodifiers Tab Return

The following steps are executed:

  1. Give focus to the first matching Firefox window
  2. Open a new browser tab
  3. Puts focus in the address bar
  4. Type "google"
  5. Tab to the first browser auto-complete result
  6. Press the Return (or Enter) key
share|improve this answer

How's the below script that I use in my ubuntu pc? use case is like this.

   $ ./ 1            # focus on a application window that executed at first
   $ ./ 2            # second executed application window

I'm using it after assigning it in keyboard custom shortcut. ctrl+1, ctrl+2, ...


#! /bin/sh

if [ "" = "$1" ] ; then
    echo "usage $0 <win index>"
    exit 1;

WIN_ID=`wmctrl -l | cut -d ' ' -f1 | head -n $1 | tail -n 1`

if [ "" = "$WIN_ID" ] ; then
    echo "fail to get win id of index $1"
    exit 1;
wmctrl -i -a $WIN_ID
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .