8

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.

3 Answers 3

8

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.

3
  • Perfect! actually I needed an app (of course), I didn't realize I actually wanted a pre-installed one. Thank you so much! Sep 4, 2012 at 15:18
  • @FrankPresenciaFandos I think your edit is specific to Gnome-Terminal because I have tested this with Terminator and XTerm and it doesn't happen to them.
    – Oli
    Sep 7, 2012 at 17:16
  • Hmm. Well it also cascades for Nautilus (kills all listing windows plus the desktop). I don't know if there's a way to limit this effect to the Window. This seems to be something internal to xkill. Edit: You can confirm this by running xkill on its own - it nukes everything it can.
    – Oli
    Sep 7, 2012 at 17:22
6

A high-level "close" operation using wmctrl

The command below also closes the currently active window using EWMH specification, though it does require installing another program, which you seemed to prefer avoiding.

Specifics

  • This is a high-level "close" operation. In other words, it sends to the application the very same message asking it to close the window that would be send via mouse clic on "close" gadget or key shortcut.
  • It does not emulated a key shortcut, so works whatever your key shortcuts are or even if you have no shortcut configured (or probably even not keyboard configured).

As a result, it has expected behavior:

  • It only closes the very window which is targeted and not the whole app (tested with xfce4-terminal, Chromium, Thunderbird).
  • Also, it gives a chance to the application to react to the close event, like asking confirmation (confirmed with Thunderbird with a window where a mail is being written, and Firefox asking confirmation before closing a window with several open tabs) or possibly even ignore the event.

The command

wmctrl -c :ACTIVE:

Reference

From project home page wmctrl - A command line tool to interact with an EWMH/NetWM compatible X Window Manager.

-c Close the window gracefully.

and

The special string ":ACTIVE:" (without the quotes) may be used to instruct wmctrl to use the currently active window for the action.

2
  • 1
    Indeed, wmctrl uses _NET_CLOSE_WINDOW whereas xdotool windowclose uses XDestroyWindow and xdotool windowkill uses XKillClient Sep 25, 2021 at 20:20
  • Awesome! Just what I was looking for!
    – NoOne
    Nov 15, 2021 at 19:40
3

I'd use xdotool:

xdotool windowkill `xdotool getactivewindow`

Seems to do the job quickly and quietly.

2
  • Thank you for your answer, I'll look into it, but from what I stated above, I'd like to know if there's a way that doesn't imply installing another app. Sep 4, 2012 at 13:54
  • 1
    or even simpler: xdotool getactivewindow windowkill
    – Shnatsel
    Dec 30, 2014 at 1:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .