Is there a keyboard shortcut for minimizing all windows except the active one?
When opening programs like GIMP, I find having background windows open distracting because GIMP has three separate windows associated with it.
It's a burden to have to go to every other non-Gimp window manually to minimize it. What I need is a keyboard shortcut in Ubuntu that matches Windows' Super + Home shortcut. One that minimizes all windows except the active one.
Is it possible to achieve this behavior in Ubuntu?
Solution 1:
It is possible to achieve this with a python script. The script requires python-wnck
and python-gtk
to be installed in order to work, although I think these are installed by default anyway.
Copy and paste this into a text editor and save in a sensible place (eg. as minimise.py in your home folder):
#!/usr/bin/env python
import wnck
import gtk
screen = wnck.screen_get_default()
while gtk.events_pending():
gtk.main_iteration()
windows = screen.get_windows()
active = screen.get_active_window()
for w in windows:
if not w == active:
w.minimize()
You can then set up the keyboard shortcut by opening Keyboard Shortcuts.
Click on Add to create a new shortcut.
Use the command bash -c 'python ~/minimise.py'
(this is assuming you saved it as minimise.py in your home folder).
You can then assign your preferred keyboard combination to this action.
The script will minimise all non-active windows. I don't think this is very useful for your use case because you will want to have all of the Gimp windows open. You can use a slightly different script to minimise all windows that aren't from the current application instead:
#!/usr/bin/env python
import wnck
import gtk
screen = wnck.screen_get_default()
while gtk.events_pending():
gtk.main_iteration()
windows = screen.get_windows()
active_app = screen.get_active_window().get_application()
for w in windows:
if not w.get_application() == active_app:
w.minimize()
Solution 2:
Here's a pretty simple approach using wmctrl
wmctrl -k on; wmctrl -R :ACTIVE:
The first command shows the desktop (i.e. minimizes all windows) and the second command raises the "active" window which is whatever was active before the minimizing. I put this in a one-line bash script and then set a keyboard shortcut to that script.
edit: restore all windows using:
wmctrl -l | cut -d' ' -f 1 | xargs -n1 wmctrl -i -a
Solution 3:
Since python-wnck is no longer in the apt repository (Kubuntu 18.04 Bionic), below is the modified python code (from the answer above by @Aditya and @dv3500ea).
From python3 onwards wnck is part of the GObject Introspection API (source). So, syntax for importing wnck (and Gtk objects) has changed.
#!/usr/bin/env python
# import necessary objects
import gi
gi.require_version('Wnck', '3.0') # specify Wnck version
from gi.repository import Wnck
from gi.repository import Gtk
# the script itself
screen = Wnck.Screen.get_default()
while Gtk.events_pending():
Gtk.main_iteration()
windows = screen.get_windows()
active = screen.get_active_window()
for w in windows:
if not w == active:
w.minimize()
then assign the shortcut to the python script:
(in Kubuntu) kmenueditor -> create a new item -> script bash -c 'python path_to_the_python_script.py'
-> assign a desired shortcut
UPDATE (May'19):
At Kubuntu 19.04 I needed to install gir1.2-wnck-3.0 module to make the script above work.
$ python -V
Python 2.7.16
$ sudo apt-get install python3-gi gir1.2-wnck-3.0
Solution 4:
bash script using xdotool:
currentwindowid=$(xdotool getactivewindow)
currentdesktopid=$(xdotool get_desktop)
for w in $(xdotool search --all --maxdepth 3 --desktop $currentdesktopid --name ".*"); do
if [ $w -ne $currentwindowid ] ; then
xdotool windowminimize "$w"
fi
done
it minimizes only windows on the current desktop.
To minimize windows on all desktops:
currentwindowid=$(xdotool getactivewindow)
for w in $(xdotool search --all --maxdepth 3 --name ".*"); do
if [ $w -ne $currentwindowid ] ; then
xdotool windowminimize "$w"
fi
done