Ubuntu – Close active window from terminal

command linewindowxte

Do you know how to do it? I know how to do it from my keyboard (Alt+F4), I know how to do it with my mouse (click that X), and I also know that kill X should kill the process with the id X (and killall Y should kill the process named Y). But I want to know how to kill the active window from terminal.

Therefore, from what I stated above, a valid answer would also be to get the id of the active window.

Right now I'm implementing this function from xte (simulating Alt+F4), but I'd like to know if there's a way that doesn't imply installing another app. Thank you all.

EDIT. Here is the full script.
First you enable 'show mouse when Ctrl is pressed', then you add this script to Commands in CompizConfig and binding to top-right corner. When you touch the top-right corner, a small notification around the mouse is shown. If you do nothing, the active window will close after 1 second. However, if you touch the corner again within the second, the active window will not be closed.

xte 'key Control_R'; if [ -f ~/.fcont ]; then rm -f -r ~/.fcont; else touch ~/.fcont; sleep 1;  if [ -f ~/.fcont ]; then xkill -id `xprop -root _NET_ACTIVE_WINDOW | cut -d\# -f2`; rm -f -r ~/.fcont; fi fi

Note that I still use the xte app named before since this question I made some time ago was never answered.

Best Answer

And if you want to get really hairy and avoid applications that aren't installed by default:

xkill -id `xprop -root _NET_ACTIVE_WINDOW | cut -d\# -f2`

Again, this seems to work fairly well.

NOTE: In the case of some applications (see comments) this may mean that all windows for an application are closed. gnome-terminal is one such application but terminator and xterm are not affected. I would suggest this is more a bug (maybe by design) with those applications rather than one with my command.