65

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.

Oliver Salzburg
  • 86,445
  • 63
  • 260
  • 306
Malabarba
  • 8,578
  • 21
  • 69
  • 105

5 Answers5

96

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
Malabarba
  • 8,578
  • 21
  • 69
  • 105
  • 8
    Nice to see someone is reading and I'm not just rambling to myself. =) – Malabarba May 20 '10 at 14:59
  • 1
    Also try [xdotool](http://stackoverflow.com/a/4228019/237285). – Andres Riofrio Apr 25 '12 at 07:04
  • 1
    This is awesome for setting focus back to gdb (debugger) when it launches a debugger target with a window that steals focus, like kvm. Use gdb command `shell wmctrl -a something`, where *something* is something in your debugger terminal title. – doug65536 Oct 25 '16 at 03:46
  • Thanks so much, this is pure gold, I was afraid I lost all of my pending work in some Chrome window that just disappeared in the background somehow, it worked! – Osmar Jan 15 '19 at 21:45
  • > _I'm not sure what happens if more than one window meets that condition_ — It seems to just select the first one in the `-l` list, regardless of which one most recently had focus or which desktop is currently visible. You can use `-i` to specify the window by its numeric ID, which you could get from the list, but you'd need some way to determine which window you're interested in. – Michael Scheper Feb 23 '21 at 01:43
  • 2
    You could also use `xdotool` for this: `xdotool search --class "class name" windowfocus`, or target the window using its name. Find the window info with `xev`. – idontknow Aug 08 '21 at 18:02
  • This technique [doesn't work with pcmanfm](https://unix.stackexchange.com/questions/686826/), because the application's name isn't in the title bar. – LonnieBest Jan 18 '22 at 12:08
18

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
Christopher
  • 348
  • 1
  • 3
  • 10
7

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

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

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

cat focus_win.sh

#! /bin/sh

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

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;
fi
wmctrl -i -a $WIN_ID
swj
  • 71
  • 1
  • 1
5

Update

Sadly, this no longer works on Gnome 41 for security reasons

Running global.context.unsafe_mode = true in Looking Glass re-enables the functionality, but only temporarily.

Original answer

On Wayland, sadly wmctrl and xdotool do not function. Instead we can talk to the window manager.

For Gnome, we can run gdbus to send a DBUS message to execute some GJS (JavaScript bindings for the GNOME C APIs).

To focus a window:

$ gdbus call \
  --session \
  --dest org.gnome.Shell \
  --object-path /org/gnome/Shell \
  --method org.gnome.Shell.Eval "
var mw =
  global.get_window_actors()
    .map(w=>w.meta_window)
    .find(mw=>mw.get_title().includes('Firefox'));
mw && mw.activate(0)"

See also:

You can play around with what's possible in GJS using Gnome's 'Looking Glass' debugger: Alt+F2, and run lg

ZimbiX
  • 181
  • 1
  • 5
  • I'm seeing this all over the place. But where exactly do I put gdbus calls? It looks like it's supposed to just evaluate the GJS. But I'm running it in a shell, from a file, and get no output (error or otherwise) on any of the scripts linked above. I've tried to evaluate them through d-feet (org.gnome.Shell.Eval) with: 'invalid syntax (, line 1)' error outputs. Separate GJS files evaluate to "global is not defined". Any help? Thank you for being the first person across dozens of threads to consider Wayland, though. Since it runs just fine on looking glass, it's obviously correct gjs. – Nephilim Oct 25 '21 at 08:14
  • in kde, it's qdbus call.... eg: qdbus org.kde.klipper /klipper getClipboardCotents to get clipboard contents... install qdbusviewer and play around.... – Vaisakh K M Jun 04 '23 at 12:23
1

As another answer already mentioned, wmctrl is a good choice for this task and wmctrl -a WINDOWTITLE allows one to activate the window whose title contains WINDOWTITLE.

However, in many cases the window title does not contain the name of the program used. For this purpose wmctrl also has the -x option that can be used to focus windows by class name.

You can type wmctrl -lx to print all currently open windows, together with their class name. You can use wmctrl -xa NAME to activate a Window whose class name includes NAME.

For example,I had the issue that I wanted to activate the window of the Evince PDF viewer if it was open, and I couldn't do that from the window name alone as it always only contains the name of the PDF that is viewed. So I used wmctrl -lx and found the line

0x06000007  0 evince.Evince         sejmou-desktop 2. User-User Collaborative Filtering

Where evince.Evince is the window class name mentioned above.

Then, I was able to activate the Evince window programmatically wmctrl -xa Evince. Of course this only works reliably as long as Evince is a unique class name among the windows that are currently open.

Sejmou
  • 371
  • 2
  • 5