Estimated reading time: 4 minutes

Custom keybindings in the GTK+ file dialogs

In Fancy awesome theming I included a screenshot that, by accident more than design, spawned today’s tip.

gtkrc in vim screenshot

Laura Biddulph sent me a SMS message that read:

Thanks! I saw your gtkrc, and now I have keybound faves in the file dialog.

The gtkrc being referenced is my GTK+ configuration file that could be seen in the right hand vim window of the screenshot from that earlier post. It was actually open in that screenshot because I was changing my GTK+ theme, and not being a GNOME user the simplest way to change it is via my ~/.gtkrc-2.0. And, if you’re using KDE and wish to the settings for GTK+ applications you should edit ~/.gtkrc-2.0-kde.

So the question becomes, what were the options in that screenshot, and are there any other cool and useful things you can do there?

gtk-theme-name = "expose"
gtk-icon-theme-name = "hicolor"
gtk-key-theme-name = "Emacs"

binding "some-shortcuts" {
    bind "<Alt>T" {
        "location-popup" ("/home/jay/urls")
    }
    bind "<Alt>M" {
        "location-popup" ("/var/lib/repo/jnrowe-misc")
    }
}
class "GtkFileChooserDefault" binding "some-shortcuts"

Ignoring the vim modeline from the screenshot the first few settings are basic theme related options. gtk-theme-name and gtk-icon-theme-name set the style and icon groups for GTK+ apps respectively. gtk-key-theme-name sets the default keybindings for GTK+ apps. By setting it to Emacs we have access to the “normal” keybindings we expect if we use bash or other common Linux tools, such as C-w to delete the word under the cursor. You can read more about gtk-key-theme-name in Proper keyboard shortcuts in GTK+.

All of the settings above are configurable with a GUI if you use GNOME or Xfce, but for those of who don’t use those desktop environments editing the ~/.gtkrc-2.0 is a reasonable solution.

GTK file chooser screenshot

The “some-shortcuts” section is the interesting one for today, it is telling GTK+ applications that we want to have our own extra keybindings available when opening or saving files. GTKFileChooser is the modern GTK+ file dialog, and it already has a set of useful keybindings including:

Key

Purpose

M-<Home>

Jump to your home directory

M-D

Jump to your ~/Desktop directory

M-<Up>

Go to current directory’s parent

C-L

Display the text location entry box

We could actually use the file chooser’s bookmark feature, and access the bookmarks with M-1 through M-9 and M-0 for bookmark number ten from the dialog. However, I find it more practical to be able to use mnemonic names for favourite locations. You could even use both if you have an excellent memory and a lot of favourite locations!

If you wish to add your own bindings the format is hopefully quite self-explanatory, just don’t forget to link your bindings to the correct class or they will not work.

You can also change the default bindings by specifying them in the configuration file, for example to use M-<left> and M-<right> to skip backward and forward along the directory path:

bind "<Alt>Left" {
    "up-folder" ()
}
bind "<Alt>Right" {
    "down-folder" ()
}

Whether you find Up and Down or Left and Right easier to remember depends on how you visualise the path, I personally prefer the defaults in this instance but the choice is entirely yours.

If you decide to significantly modify the bindings you may even find it easier to split the ~/.gtkrc-2.0 in to chunks to make it easier to manage or share, for this you can use the include directive. An example would be: include "~/.gtk_bindings".


Authenticate this page by pasting this signature into Keybase.

Have a suggestion or see a typo? Edit this page