Gnome (Ubuntu): how to bring a program window to the front using a command line from the terminal?

14

6

I have a certain work environment with dozens of open Windows. How can I bring to the front a window with a known name/title programatically or using the command line?

GJ.

Posted 2010-09-02T01:33:09.487

Reputation: 8 151

Answers

12

I used to use wmctrl -a <name>, which works fine, but recently switched to xdotool, e.g.:

xdotool search --name <name-or-regex-for-name> windowraise

It has many other features too.

To install:

sudo apt-get install xdotool

frabjous

Posted 2010-09-02T01:33:09.487

Reputation: 9 044

5xdotool windowraise brings the window to the front but does not give focus to the window or switch to the desktop with the window. instead, windowactivate will do all three. – jozxyqk – 2015-05-29T09:18:12.247

6

Well, after sudo apt-get install wmctrl-ing, you can play with this bash script:

#! /bin/bash

WINTITLE="Mail/News" # Main Thunderbird window has this in titlebar
PROGNAME="mozilla-thunderbird" # This is the name of the binary for t-bird

# Use wmctrl to list all windows, count how many contain WINTITLE,
# and test if that count is non-zero:

if [ `wmctrl -l | grep -c "$WINTITLE"` != 0 ]
then
wmctrl -a "$WINTITLE" # If it exists, bring t-bird window to front
else
$PROGNAME & # Otherwise, just launch t-bird
fi
exit 0

Which I found here

digitxp

Posted 2010-09-02T01:33:09.487

Reputation: 13 502

wmctrl has a -i option, which supports working with the window with its hex identifier. And so you can do this wmctrl -lp|grep 'whatever incomplete name'|cut -d' ' -f1|xargs wmctrl -ai - which would do something similar – vlad-ardelean – 2014-10-11T16:36:46.907

4No need for the brackets and backticks: if ! wmctrl -l | grep -q "$WINTITLE" – Paused until further notice. – 2010-09-02T02:09:17.160

0

When using xdotool, it seems difficult to bring to front all windows for a given application or class using only one command. I end up having better results by wrapping it in a for loop at the shell level. Using Bash:

for WINDOW in $(xdotool search --desktop 0 Firefox); do
   xdotool windowactivate ${WINDOW}
done

Few remarks:

  • By default, xdotool search will search the pattern (here Firefox) in window name, class, and classname. If you want to restrict your search space, use the relevant --class, --name or --classname options.
  • The --desktop 0 option limits the search to the first desktop. This seems to be a workaround to avoid the XGetWindowProperty[_NET_WM_DESKTOP] failed (code=1) mentioned in some comments.
  • At the time of this writing, the xdotool project is stalled since 2015. It still remains my tool of choice. For personal reasons, Jordan Sissel (the original author) is not as active as he was in the past, so don't hesitate to contribute to the project.


This is a copy of an answer I've posted on AskUbuntu, but I think it is Linux-flavor independent, so it may be useful here too.

Sylvain Leroux

Posted 2010-09-02T01:33:09.487

Reputation: 135