What is the command to close a window?

I want to set a keyboard shortcut to close the current window in Ubuntu 16.04. I am aware that alt+f4 already does this and that I can redefine this shortcut if I want to, but what I really want is to add an additional shortcut to perform the same function.

The reason being; I have a keyboard without designated function keys (a 60%). Instead the function keys are activated via a keyboard shortcut that toggles the row of number keys between number and function keys (for example 4 to f4 and vice versa).

I am deep in the habit of quickly closing windows using alt+f4 and want it to work regardless of whether I am in function key or number key mode, but can't seem to find what I should enter in the command field when setting a new keyboard shortcut.

I have found a good workaround to my particular problem and posted it in the answers below. Still open to more elegant solutions.

Edit: To clarify, the machine is running X11.


Solution 1:

wmctrl -c <win>

   wmctrl - interact with a EWMH/NetWM compatible X Window Manager.

   -c <WIN>
          Close the window <WIN> gracefully.

(from man wmctrl. But note: I use XFCE so haven't tested this on Unity as found in default Ubuntu 16.04, and I wouldn't expect it to work if using Wayland on 17.10 upwards)

Solution 2:

the other work around i found along with @FinleyGibson answer is

open dconf-editor (if dconf-editor is not installed, install with below command
sudo apt install dconf-editor)

search key word "close"

enter image description here

then edit the custom value to ['<Alt>F4', '<Alt>4']

enter image description here

Solution 3:

Had you try to use CTRL+Q

I was use to press these keys to close windows, never added a keyboard shortcut, seems to be the default at least in the distros I used(always debian based)

but I see the point of your question

Solution 4:

I found a good solution to my problem, though it does not exactly answer the question I originally asked, I'm posting it here as a workaround for anyone that encounters a similar issue.

The solution I found was to use xdotool to execute the alt+f4 shortcut as a command, and to set this as the command for alt+4. To do this first isntall xdotool if you don't have it already:

sudo apt-get install xdotool

Then go to keyboard shortcuts in the settings menu, and add a custom shortcut. In the command field enter:

xdotool key Alt+F4

And set the shortcut to Alt+4.

This works well, but I'm leaving the question as is, in case someone has a more elegant solution, using the exact command called by the close window shortcut by default.

Solution 5:

Same problem was solved after 5 minute search: You can use

$ sh -c "xkill -id $(xprop -root -notype | sed -n '/^_NET_ACTIVE_WINDOW/ s/^.*# *\|\,.*$//g p')"

It kills current active window. I bind this to "Super+q".