Electricmonk

Ferry Boender

Programmer, DevOpper, Open Source enthusiast.

Blog

Gnome/Metacity keybindings and other customizations

Thursday, August 2nd, 2007

I never really liked Gnome because I couldn’t make Metacity act the way I like it.

I’m used to WindowMaker, and it has very configurable keybindings for managing windows. For instance, it’s quite easy to map a keybinding like <Control><Alt>-; to an action that maximizes the currently focussed window horizontally. Another keybinding maximizes the window vertically. These are very useful options for me, since I use terminal windows a lot.

Another annoying thing about Gnome/Metacity was that you could have a keybinding which maximized the currently focussed window, but pressing that keybinding again wouldn’t bring the window back to the original size. This is another feature is use all the time, because I often need to have more screen real estate for my terminal window for a short while (so that I can see more of the terminal output buffer).

Something else I missed from WindowMaker is binding keybindings to programs in the menu so that, say, <Win>-e opened my editor. This didn’t seem possible in Gnome/Metacity, and I didn’t want to use a third-party application for it, because that doesn’t integrate nicely with Gnome (and will therefore cause strange behaviour, such as conflicting keybindings in applications).

In the past, I’ve given Gnome a couple of tries, but everytime I switched back to WindowMaker after a little while because of the lack of proper keybindings. A couple of days ago I decided to give Gnome another try, because I’m planning on installing Ubuntu (currently running Debian GNU/Linux) somewhere in the near future. Quite by accident, I stumbled upon the fact that Gnome/Metacity does support all the keybindings I’m used to. All you have to do is dive into the GConf registry a bit.

Window manipulations

Here are the various locations for Window manipulations in the Gnome registry and their purposes. To edit these, you’ll have to have the gconf-editor installed.

  • /apps/metacity/window_keybindings/maximize
    Maximize the window. Disable this setting because it doesn’t “toggle” the maximization state, it only maximizes the window. Just edit the value and enter ‘disabled‘ as the value.
  • /apps/metacity/window_keybindings/maximize_horizontally
    Maximize the window horizontally. (make it as wide as the screen while keeping the same height)
  • /apps/metacity/window_keybindings/maximize_vertically
    Maximize the window vertically. (make it as high as the screen while keeping the same width)
  • /apps/metacity/window_keybindings/toggle_maximized
    Maximize the window on the first keypress. Unmaximize it on the next keypress.
  • /apps/metacity/window_keybindings/toggle_shaded
    Roll up the window so that only the title bar is visible. This is handy for quickly peaking under the currently focussed window.

You can edit these bindings by right-clicking on the key and selecting ‘Edit key’. For the value, you’ll have to type out the full keybinding name. For instance: “<Control><Alt>r” (without the quotes). Take a look at the already defined keybindings for hints.

Global command-running keybindings

After finding out about the above configuration options for Metacity, I took a closer look at other options, and it turns out you can also bind global keybindings to programs so you can have shortcuts. Just press the keybinding and the program starts. I’m using this mostly for the top-10 applications I use: browser, editor, terminal emulator and some other stuff. Here’s how to add keybindings to programs:

  1. In gconf-editor, find the key /apps/metacity/keybinding_commands/. Here, you’ll see a list of ‘command_NR’ key/value pairs.
  2. Edit the value for one of the command_NR keys, and enter the name of the program you want to run when pressing some keybinding. For instance: /usr/bin/gvim
  3. Now, go to /apps/metacity/global_keybindings/. Here, you’ll see a list of keys with names similar to run_command_NR.
  4. Edit the value for the key with the name corresponding to the key you edited in step 2. Enter your keybinding in the same way as mentioned above.

Using the Windows key for bindings

Perhaps it’s not the same on every system, but I couldn’t use the Windows key (left of the left Alt key) for keybindings. When I tried binding commands to it in Gnome’s Keyboard Shortcuts manager, all I got was an ‘Super_L’ entry, and I couldn’t enter any extra modifiers (such as <Super>e, to run my Editor). Turns out you have to change the behaviour of the Win key so that it registers as a normal modifier key (similar to Shift, Control, etc) in X.org. Here’s how to do that:

  1. Open the Keyboard manager. On my system, it’s the System → Preferences → Keyboard menu-option.
  2. Open the Layout options tab.
  3. Open the Alt/Win key behavior fold-out.
  4. Choose the Meta is mapped to the left Win-key option.
  5. Now, Meta is a code-name for one of the modifier-keys. Each modifier key (Shift, Alt, Control, etc) has a codename. But internally, X.org doesn't use these codenames. Instead, it used different names: Mod1 through Mod5. And apparently it differs from system to system to which Mod-X internal name the Meta modifier is mapped. So in order to find out which Mod-X you'll need to use when specifying keybindings, you can use the Gnome Keyboard Shortcuts manager.
  6. Open the Gnome Keyboard Shortcuts manager. (System → Preferences → Keyboard shortcuts)
  7. Click on of the options, and press the Win key with some other key. This will appear in the manager as: l, for instance. You now know that you need to use Mod4 if you want to bind commands or other things to the Win key when using the GConf-editor tool.

Move/resize Windows with a modifier key

WindowMaker's got this option that allows you to move and resize windows by simply holding the Alt key and then drag-and-drop anywhere in the window to resize or move it. So, if I hold the Alt key, and press and drag anywhere in the window with the left mouse-button, I can move the window. If I hold the Alt key and press and drag with the right button, I can resize the window. This is so much more comfortable than having to go al the way to the edge of the window in order to drag it.

I don't know how long Gnome/Metacity's been supporting this, but as far as I know, it didn't the last time I tried it. It does now. I'm not sure if you need to turn it on explicitly, but here's how it's set up on my system:

In the menu, choose System → Preferences → Windows. Here, under 'Movement key', choose the 'Alt' key. You can now move windows by holding Alt and then use the left mouse-button to move the window. Holding Alt and dragging with the middle mouse-button in the window resizes the window. It's a little annoying that you can't resize windows when it's maximized (either fully, horizontally or vertically), but I guess I'll get used to that. Or maybe I overlooked an option in the GConf-editor again... lemme check.. Nope, doesn't look like it.

Standard window options

Something that's completely unsupported by Metacity is automatically setting window options on windows when they start. WindowMaker has a facility that allows you to set options for windows as soon as they are opened. This way, you can force a window to be maximized or minimized as soon as it opens, even if the program itself doesn't support this. Metacity can't do that, but there's a third-party application that takes care of that: Devil's Pie.

Application run dialog

By default, Gnome has an Application Run dialog, but it's not very good. It doesn't do a good job at completing the name of programs I want to run. I want tab-completion. Fortunately, I wrote GExec a while ago. I've hooked it up to the <Win>r keybinding as described in the 'Global command-running keybindings' section in this post.

Result

I now have the following keybindings set for Gnome/Metacity:

Control-Alt-h Switch to the workspace on the left. Vim-style.
Control-Alt-l Switch to the workspace on the right. Vim-style.
Control-Alt-j Minimize the current window.
Control-Alt-k Maximize/Unmaximize the current window.
Control-Alt-- Shade/Unshade the current window.
Control-Alt-; Horizontally maximize/unmaximize the current window.
Control-Alt-' Vertically maximize/unmaximize the current window.
Control-Alt-1 Jump to workspace 1.
Control-Alt-9 Jump to workspace 9.
Win-e Start my editor (gvim).
Win-b Start my browser (Iceweasel / Firefox).
Win-x Start my terminal emulator. (gnome-terminal)
Win-r Start my application launcher (gexec).

With the customizations described above, I feel Gnome is probably much more usable for me. I hope to switch to Ubuntu shortly. For the curious among you, this is how my desktop looks now:

gnome.png.

And this is what it used to look like:

dt_blood.jpg

Update: I forgot to mention. If you want to bind keybindings to stuff like <Control><Alt>-; , you need to enter <Control><Alt>Semicolon. Use the Gnome keyboard shortcut manager to find out the proper keybinding to enter in gconf.

Update II: These customizations work for Metacity 2.14.5. Your mileage may vary for other versions.

Update III: Also check out the Emacs keybindings 'theme'. It lets you use Emacs-like keybindings in Gnome so that you can press Ctrl-u to erase everything in front of the cursor on the current line and/or use Ctrl-y to insert back what you just erased ('yanking'). Edit the GConf key /desktop/gnome/interface/gtk_key_theme and set it to the 'Emacs' value. If you're only using GTK and not Gnome, you can add a line with:

gtk-key-theme-name = "Emacs"

to a .gtkrc-2.0 file in your home directory to get the same effect.

The text of all posts on this blog, unless specificly mentioned otherwise, are licensed under this license.