Shell command to bring a program window in front of another?

Does a shell command exist to bring a already started program in gnome in the front of another.

i.e.:

Gedit and Nautilus are started. Nautilus is in the background and Gedit in the foreground.

How to i bring Nautilus in the front with a shell command?


Solution 1:

  • Install wmctrl Install wmctrl

    wmctrl -a <name>

  • You can also use xdotool Install xdotool

Note:

  • More information about xdotool can be found here.

Solution 2:

Another option is xdotool:

xdotool search --class Nautilus windowactivate

Solution 3:

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 though. For personal reasons, Jordan Sissel (the original author) is not as active as he was, so don't hesitate to contribute to the project.