Tuesday, December 18, 2007

making ctrl-u work in firefox

I try to avoid the mouse when I'm using my computer. In Firefox, I commonly want to clear the text in the location bar and type in a new URL. Focusing the location bar is easy (Ctrl-L), but GTK doesn't appear to have a default keybinding to clear a text entry. End, Shift-Home, Delete works but has the unpleasant side effect of replacing the contents of the clipboard with the previous location — I'm often clearing the location bar so I can visit the URL that was previously in the clipboard.

You can tell GTK to use an Emacs/readline()-like keyboard theme by adding gtk-key-theme-name = "Emacs" to ~/.gtkrc-2.0, which allows you to use Ctrl-U to clear text entries, but it also adds a binding for Ctrl-W, leaving you with no Firefox keyboard shortcut to close a window. GTK added an "unbind" directive that presumably would allow one to remove the Ctrl-W binding, but it's only present in 2.12 or later.

The solution is to just add the Ctrl-U binding itself by putting the following into ~/.gtkrc-2.0:

binding "gtk-clear-text-entry" {
bind "<ctrl>k" {
"delete-from-cursor" (paragraph-ends, 1)
}
bind "<ctrl>u" {
"move-cursor" (paragraph-ends, -1, 0)
"delete-from-cursor" (paragraph-ends, 1)
}
}
class "GtkEntry" binding "gtk-clear-text-entry"
class "GtkTextView" binding "gtk-clear-text-entry"


You can test your bindings with zenity --entry.

(Most of this was cribbed from MozillaZine. I don't have the issues that the author describes with Firefox ignoring user-defined bindings, but I suspect that may be because he/she is not unbinding the old Ctrl-U binding before defining a new one.)

1 comments:

daisy stanton said...

dude! i can't tell you how much this under-the-surface pisses me off when i'm using linux. i say "under the surface" because i've always been too lazy to do anything about it. and yet now a solution has crawled into my lap.

LGTM++