Close active window from terminal
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.
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.
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.
I'd use xdotool
:
xdotool windowkill `xdotool getactivewindow`
Seems to do the job quickly and quietly.