36

In 12.04 (Unity), I would like to assign a shortcut to the "always on top option" for a window. Probably something like Ctrl+Shift+Home to turn on and Ctrl+Shift+End to turn off, or whatever. Is there an option to do this somewhere in the Compiz Settings Manager?

This (old) post claimed that

For compiz, Enable Extra WM Actions. Check the properties, and insert ALT+A to Key field for Toggle Always-On-Top action.

But I couldn't find that.

Basharat Sialvi
  • 24,046
  • 8
  • 62
  • 82
Ian Hincks
  • 2,811

7 Answers7

50

It should work on every Ubuntu OS since version 9.04 without installing anything, modifying of creating any shortcuts.

Try Alt + Space, T

  • Alt + Space as the first shortcut brings up the right-click mouse menu

  • T selects the "Always On Top" function.

techraf
  • 3,316
user294298
  • 516
  • 5
  • 2
23

Since no one has mentioned this yet, I'll just leave a solution that worked for me on Ubuntu 12.04.

You could setup a regular keyboard shortcut and use wmctrl to toggle the "Always on Top" option.

Go to System Settings > Keyboard > Shortcuts > Custom Shortcuts. Hit the + and give your shortcut a name. Then enter the following command.

wmctrl -r :ACTIVE: -b toggle,above

If you don't have it already, you can install wmctrl from the repos using

sudo apt-get install wmctrl

You should have something that looks similar to this.

Screenshot showing a custom keyboard shortcut setup screen

Hit apply and you should be good to go!

This solution came from a discussion on Ubuntu forums.

Kasisnu
  • 530
7

I tried to use the wmctrl command toggle, but it didn't work on my Ubuntu set up. The toggle would toggle on, but not toggle off. (I think it might be because I'm using the gnome desktop environment, on which wmctrl is slightly broken AFAIK).

Anyway, after a lot of research and working out how to write proper code in bash, I created a single command that uses the wmctrl commands within a layer of logic to toggle the 'always on top' state effectively on the current GNOME desktop. I posted this answer on Ask Unix/Linux, but thought I'd post it here too in case anyone had the same issue.

Here is the command:

bash -c 'wmctrl -r :ACTIVE: -b $([[ $(xprop -id $(xprop -root -f _NET_ACTIVE_WINDOW 0x " \$0\\n" _NET_ACTIVE_WINDOW | awk "{print \$2}") _NET_WM_STATE) =~ "ABOVE" ]] && echo "remove" || echo "add"),above'

It checks the active window state property "_NET_WM_STATE" using xprops, and if it contains the text "ABOVE" that means the 'always on top' option is active. Then it just runs the wmctrl command with the parameter add or remove as appropriate.


Command breakdown (each command is inserted into the next, replacing the placeholder):

  • Get active window id:

     xprop -root -f _NET_ACTIVE_WINDOW 0x " \$0\\n" _NET_ACTIVE_WINDOW | awk "{print \$2}"
    
  • Get the window state from xprop using the id:

     xprop -id $(■) _NET_WM_STATE
    
  • Check if the state contains "ABOVE", indicating that the window is set to "always on top":

     [[ $(■) =~ "ABOVE" ]]
    
  • Return "remove" if true, otherwise return "add":

     ■ && echo "remove" || echo "add"
    
  • run wmctrl command using the returned value as a parameter:

     wmctrl -r :ACTIVE: -b $(■),above
    
  • Send the whole thing to bash so that you can use command substitution ${ ... }, bash boolean evaluation [[ ... ]] and the regex match operator =~:

     bash -c '■'
    

This last step in particular took me a very long time to figure out. Until I realised that the keyboard shortcuts weren't running in bash by default, I had no idea why the commands were working in the console as I was testing them but silently failing when run directly as a keyboard shortcut. It drove me up the wall for ages!

Note: because you need quotes around the command you're sending to bash, I had to be careful when writing the command that I never went more than one more level deep (using double quotes). Any further nesting of strings in quotes would have required lots of confusing backslashes to escape the quotes.

Further detailed notes: how to create the keyboard shortcut (thanks @rajesh_chaurasiya)

  1. Install wmctrl using sudo apt-get install wmctrl if you haven't already

  2. Go to system settings > keyboard shortcuts. Click the + button to add a custom shortcut.

  3. Choose a name such as 'Toggle always on top' ( it can be anything you like ).

  4. In the command field add the full command from the top of this answer.

  5. Record a keybinding to complete the shortcut.

Geoff Davids
  • 243
  • 2
  • 6
  • This looks like a question and not really an answer. Anyway, on my Ubuntu 19.10 your command works. Probably, as you wrote yourself, something is slightly broken in your system. Next time I suggest to invest your time into finding the root cause, instead of elaborating clumsy solutions. – user1182474 Apr 12 '20 at 10:18
  • @user1182474 I did invest time into finding a root cause, but came up with nothing. Aside from looking into the actual code for wmctrl myself, I don't see what else I could have done to fix it directly. And while long, I think my command cleanly automates using the add/remove 'above' (separate commands) approach workaround that was mentioned on other posts. don't understand your issue with it? – Geoff Davids Apr 25 '20 at 18:37
  • 1
    I had the same problem, wmctrl would not toggle off. This solution tackles the problem brilliantly and gives me a method I can reuse for other purposes. Thank you! – kalligator Oct 10 '20 at 10:29
  • This is not isolated to the poster's system. It's this way for me as well, and I found a post by someone else with the same problem. I was going to look into doing exactly what this solution does when I saw it, so many thanks for sharing it! – Tomer Godinger Mar 08 '22 at 08:53
  • This example actually works way better than @Kasisnu answer. For some reason kasisnu's answer only toggles Always on Top on but doesn't take it off - even though toggle is in the command. This answer by Geoff Davids seems to work every time. – hazrpg Jun 28 '23 at 13:15
7

Unity instead of GNOME (Ubuntu before 17.04)

If not install CCSM and extra compiz plugins via:

sudo apt-get install compizconfig-settings-manager compiz-plugins-extra

Be aware please. CCSM can kill Unity3D on some systems.

If you have not had compiz-plugins-extra already installed, you need to restart Compiz to load them (even though they display in CCSM just fine before the restart) with Alt+F2 and

compiz --replace

You can see Extra WM Actions under Windows Management section on CCSM as explained in the post you found. You can set the desired action there easily.

Pablo Bianchi
  • 15,657
heartsmagic
  • 5,350
  • Actually, restarting compiz is only needed so that it is aware of the new plugins. Afterwards, you can change the shortcuts on the fly (i.e. you do not need to restart Compiz after every change of a shortcut) – sup Jan 16 '13 at 11:45
  • Thank you for the info @sup. I just explained it using the given post. Actually I did not tried it myself. – heartsmagic Jan 17 '13 at 18:59
  • Well, I reworded it to make it even clearer:-). – sup Jan 19 '13 at 13:27
2

This one is quite (very) old, but still, here is what did it for me:

dconf write /org/gnome/desktop/wm/keybindings/always-on-top  "['<Control><Super>t']"

(that's the same shortcut than default Windows PowerToys)

brisssou
  • 191
  • 1
    Works in 20.04. It might work in any version I guess as long as the environment is GNOME & keybinding path exist. This should be the accepted answer now. Other solutions on this page did not work for me. – ProPlayerMaxUltra Mar 28 '23 at 10:30
  • 2
    dconf-editor application can be used to do this via GUI. Just install it with: sudo apt install dconf-editor. – ProPlayerMaxUltra Mar 28 '23 at 10:44
  • 1
    An analog for MATE: dconf write /org/mate/marco/window-keybindings/toggle-above "'<Control><Super>t'". – Des Nerger Apr 30 '23 at 01:53
2

Another, better I think, solution is to add Alt+A as a shortcut to toggle the "Always on Top" property of a window. This can be done via gconf-editor. Just follow the simple instructions given in this video.

EDIT : Copied from the video

  1. if you are using 12.04 && 12.10 you have to install gconf-editor

    sudo apt-get install gconf-editor

  2. Type gconf-editor in terminal.

  3. Click on apps

  4. Go to metacity

  5. window_keybindings

  6. right click inside the right box and click on new key

  7. Choose string from the dropdown list.

  8. Add the word toggle_above

  9. Write any command you want. i.e : A and press OK

Done :-)

Suhaib
  • 4,110
0

It may be too late but if anyone search for it again and come here, there is, as for me, a more simple solution:

On Ubuntu and on many other distributions using GTK (they mostly share the setting panel), you can go to Setting -> keyboard > shortcut. In the "list" of shortcut, you will find the entry unassigned to keep a windows on top. Not sure of the name in English but in French it's "Activer/Désactiver la mise au premier plan de la fenêtre".

In English, it is "Rise window above other windows".

screenshot

Set it and that's it :)

Pablo Bianchi
  • 15,657