April 9, 2007

Hide and show panels with a keyboard shortcut in Ubuntu

I usually keep the default top and bottom panels in gnome, but especially on my laptop, place great value on the screen estate that I can get by hiding them. The usual way I do this is by using autohide and then setting the hidden size to 0 or 1. Mostly I dont need the panels because I can launch applications with Alt-F2 or with shortcuts. But when I need to access something from the panel, I have to mouse over the hidden panel to bring it up. The other minor irritation is that the panel may spring out when you dont really want it if your mouse wanders close to it.

In a recent post in the Ubuntu forums, it was suggested that it would be nice to set up a keyboard shortcut to show the panels when needed. Since it is easy to access the gconf-editor from the command line, it was easy to write a script to toggle the hide status of the panel. Here is a short how-to if someone is interested.

Copy this script, paste it a file and save it as toggle.sh. A good way to place it will be /home/username/.toggle.sh.


#find the current state of the panels
state=`gconftool-2 --get "/apps/panel/toplevels/top_panel_screen0/auto_hide"`

#if autohide on, turn it off
if [ $state = "true" ]; then
gconftool-2 --set "/apps/panel/toplevels/top_panel_screen0/unhide_delay" --type integer "0"
gconftool-2 --set "/apps/panel/toplevels/top_panel_screen0/auto_hide" --type bool "false"
gconftool-2 --set "/apps/panel/toplevels/bottom_panel_screen0/unhide_delay" --type integer "0"
gconftool-2 --set "/apps/panel/toplevels/bottom_panel_screen0/auto_hide" --type bool "false"

#if autohide off, turn it on
if [ $state = "false" ]; then
gconftool-2 --set "/apps/panel/toplevels/top_panel_screen0/unhide_delay" --type integer "100000"
gconftool-2 --set "/apps/panel/toplevels/top_panel_screen0/auto_hide" --type bool "true"
gconftool-2 --set "/apps/panel/toplevels/bottom_panel_screen0/unhide_delay" --type integer "100000"
gconftool-2 --set "/apps/panel/toplevels/bottom_panel_screen0/auto_hide" --type bool "true"

Make the script executable:

chmod +x ~/.toggle.sh

Open gconf-editor and under /apps/metacity/keybinding_commands, change the value of command_1 (or any other unused command) to /home/username/.toggle.sh.

Then under /apps/metacity/global_keybindings, change the value of run_command_1 (if you used command_1 for the script) to controlF12 or any other key combination that you want. There, you are done! Now pressing the key combination will toggle the panels to hide or show. I have tested this with metacity and beryl on gnome and it works perfectly.

Edit: 10 April 2007 - Thanks to dzv (in the Ubuntu forums) for comments and the suggestion to change the unhide delay so that the panel stays hidden.

Powered by ScribeFire.


Anonymous said...

Hi, I would like to enable the same shortcut (hide/show), and tried your script. Using Ubuntu Feisty with the latest beryl release under XGL, it seems the gconf-editor keys the script edit have no effect on Gnome.

The script works nonetheless, since key values are edited in gconf. Do you know what the problem could be? Thanks :)

Raja said...

I have the script working in Beryl. But you have to create the keybindings for the command in Beryl, not gnome.
So, open Beryl-manager and go to General Options -> Commands. Select command 0 and type the path to your script. Then go to General Options -> shortcuts -> Commands. Now you can bind command 0 to the key that you want to use.
Thanks for trying this out and hope this helps!

Anonymous said...

Thank you. In fact I made a mistake in my statement, due to a confusion between Beryl and compiz. I've been running Beryl for a long time but someday it failed to launch and I returned to Metacity. Then, Feisty came out with a "Click here and you'll get the 3D thing" option.

I did not realized it is using compiz and not all my beryl packages. Well, Beryl is going to merge/has merged (?) with compiz anyway, but it could explain why the script fails.

If I run beryl-manager, it basically crashes and Xgl complains. The gnome-compiz-manager has no shortcut/command section, but it could be gconf-editor then?

'Should clean my installation :)
Thank you for your help.

Matt M said...

Thanks for the instructions and script.

Using gconf-editor was the final step for me --- setting the autohide size to zero.

Offbrand said...

Ok, I got this working, but what are the chances we could get this to make the panels slide to the right like when using the panel hide buttons?

Anonymous said...

Thank you for sharing this script. Works very well for me using Ubuntu Hardy.

smoenux said...

Nice one.

I made a python version of the script,
just for fun ^_^

Selim said...

Installed and it works like a charm.
Thanks for the script.
I like using shortcuts.
In case if someone has difficulties with assigning shortcuts, use ubuntu-tweak.

Selim said...

By the way, is there a way to make shortcut for setting focus on panel? That would be very useful, I think.

Anonymous said...

screen_0 is to be suppressed for fedora 12.

Anonymous said...

gconftool-2 --toggle /apps/panel/toplevels/bottom_panel_screen0/auto_hide

Beothorn said...

--toogle didn't work

I have made changes on the script so it doesn't depend anymore on the hardcoded panel names:


panelList=$(gconftool-2 --all-dirs "/apps/panel/toplevels")

for panel in $panelList
state=$(gconftool-2 --get "$panel/auto_hide")
if [ $state = "true" ]; then
gconftool-2 --set "$panel/unhide_delay" --type integer "0"
gconftool-2 --set "$panel/auto_hide" --type bool "false"
gconftool-2 --set "$panel/unhide_delay" --type integer "100000"
gconftool-2 --set "$panel/auto_hide" --type bool "true"

Dorian Scholz said...

for gnome3 with old-style panels you need to use gsettings instead of gconf.
also i changed it to auto-hide only one of my panels, when i'm in single monitor mode:


NUM_MONITORS=$(/usr/bin/xrandr -q | /bin/grep " connected [0-9]\+x[0-9]\+" | /usr/bin/wc -l)

if [ "$NUM_MONITORS" = "1" ]; then
gsettings set org.gnome.gnome-panel.toplevel:/org/gnome/gnome-panel/layout/toplevels/toplevel-1/ unhide-delay 10000
gsettings set org.gnome.gnome-panel.toplevel:/org/gnome/gnome-panel/layout/toplevels/toplevel-1/ auto-hide true
gsettings set org.gnome.gnome-panel.toplevel:/org/gnome/gnome-panel/layout/toplevels/toplevel-1/ unhide-delay 0
gsettings set org.gnome.gnome-panel.toplevel:/org/gnome/gnome-panel/layout/toplevels/toplevel-1/ auto-hide false