26

I am running Ubuntu GNOME 15.10 with GNOME 3.18 and I am wondering if there is a way from Terminal to close, minimize, unminimize, maximize, and unmaximize specified windows?

Note - I've looked at this Q&A - I am asking how to do it from Terminal... Not how to add the buttons... The buttons are already there, I don't need to add them.

  • 2
  • @Anastasia: How is this a duplicate of that? That only asks about minimizing... I'm asking about all of the other stuff as well which is not answered there. –  Nov 28 '15 at 18:51
  • I assumed those actions are so closely related that answering one should lead to all the others, but you're right, that may be a broad sweep.

    Now I'm also wondering in subsets of questions can be considered duplicates.

    – Anastasia Nov 28 '15 at 19:03
  • @Anastasia: Well, because the answers to the other question don't actually mention how to achieve this, even if they would help me, it is still not a duplicate of that question. Plus even if the answers to the other question answer my question, my question still isn't asking the same thing as the other one is asking. –  Nov 28 '15 at 19:07
  • Hi Para, I hope the question is still "open". An overview... – Jacob Vlijm Feb 29 '16 at 20:30
  • What's your main objective ? perform any of the three actions on a window that you can select with cursor ? – Sergiy Kolodyazhnyy Feb 29 '16 at 20:42
  • Would it be accepteable to have 3 keyboard shortcuts for active (currently focused) window ? – Sergiy Kolodyazhnyy Feb 29 '16 at 21:05
  • @Serg: Preferably I would like to be able to execute 5 commands in Terminal (each would do one of these things for the target window close, minimize, unminimize, maximize, or unmaximize). I would be able to either specify the current window (the Terminal window), or I would be able to somehow specify another window to do the actions on. –  Feb 29 '16 at 21:20
  • OK. I'm gonna cook up a few things , will post later today or tomorrow. I've a few things put together already, but it will require time. – Sergiy Kolodyazhnyy Feb 29 '16 at 21:32
  • @SergiyKolodyazhnyy: How's the cooking going? Or did you eat the pie before thinking of us? ;) –  Sep 24 '17 at 12:20

4 Answers4

35

There are two important tools to manipulate windows from cli; xdotool and wmctrl. Both tools overlap each other in functionality, but the most important commands referring to your question:

In an (no doubt incomplete) overview, listing the commands I use most:

Closing a window

wmctrl:

wmctrl -ic <window_id>

Minimizing a window

xdotool:

xdotool windowminimize <window_id>

Un- minimizing a window

wmctrl:

effectively un- minimizing is done by:

wmctrl -ia <window_id>

I mention effectively, since the command moves to the corresponding desktop, unminimizes and raises the window, but the command also does that on windows which are not minimized.

Maximizing a window

xdotool:

xdotool windowsize <window_id> 100% 100%

wmctrl:

wmctrl -ir <window_id> -b add,maximized_vert,maximized_horz

Un- maximizing a window:

wmctrl -ir <window_id> -b remove,maximized_vert,maximized_horz

Notes

  • Both xdotool and wmctrl are not on your system by default:

    sudo apt-get install xdotool wmctrl
    
  • To run any of the commands on the currently active window:

    • for wmctrl commands, remove the -i option, replace <window_id> by :ACTIVE:

    • for xdotool commands: replace <window_id> by $(xdotool getactivewindow)

  • In many cases, commands can be run by using either the window id or the window name. The -i option in wmctrl tells wmctrl to use the window id. I'd suggest not using the window's name as an identifier, to prevent name clashes. It happens more easily then you'd expect.

  • From my own experience, using maximizing in a script; Using wmctrl to maximize / unmaximize can be a bit buggy on both Unity and Gnome, while the xdotool option works more robust in my experience. In most scripts, I end up in using a mix of both wmctrl and xdotool.

More info on man wmctrl and man xdotool (mainly the section: WINDOW COMMANDS).

Jacob Vlijm
  • 83,767
6

Adding to Jacob Vlijm's answer:

xdotool windowactivate $minwinid
xdotool windowraise $minwinid
xdotool windowfocus $minwinid

Also works for unminimizing a window. The animation is faster for me.

asdfdsa
  • 61
0

Adding to Jacob Vlijm's answer:

Minimizing a window w/ xdotool by window name:

xdotool search -name '<window_name>' windowminimize

To get list of windows names use:

wmctrl -l
-1

To hide the current window:

wmctrl -r :ACTIVE: -b toggle,hidden 
  • 1
    Does not do anything at my side. Ubuntu 22.04.2 LTS default install. – Tino Jul 13 '23 at 05:25
  • @Tino, I'm also running 22.04 LTS, but I use Xfce window manager. I don't think that makes a difference, but I did have to install wmctrl. 'sudo apt-get install wmctrl' – randyman99 Jul 20 '23 at 08:02