Emacs Keybindings - Firefox

From MozillaZine Knowledge Base
Jump to navigationJump to search

GNU/Linux with GTK

Changing Keybindings

Firefox uses the GTK setting to determine whether Emacs-like/Readline-like keybindings are active in text fields, regardless of what window manager you use. To enable these keybindings, add the line

gtk-key-theme-name = "Emacs"

to ~/.gtkrc-2.0 (create the file if it doesn't exist) and restart Firefox.

If you use Gnome, you can set this using Desktop Preferences > Keyboard Shortcuts instead of editing the configuration file.

Note: This doesn't work in the latest gnome (2.8). In this case, you need to run "gconf-editor", and directly change the key /desktop/gnome/interface/gtk_key_theme from "Default" to "Emacs". Then if you restart Firefox, all is good in the world again.

Note: Running Gnome 2.8 under FC3 I found that under gconf-editor the gtk_key_theme was already set to Emacs, yet Firefox wasn't using the Emacs bindings. I edited .gtkrc-2.0 as indicated and then it started working. On another FC3 system I found that setting .gtkrc-2.0 as above didn't do the trick, I had to use gconf-editor as well. YMMV.


The Keybindings

The shortcuts are often called Emacs keybindings, but they're actually more similar to keybindings in Readline. When these keybindings are enabled, they override the standard keybindings in Gnome and Firefox (when a text entry field has focus).

Ctrl+A Go to beginning of line
Ctrl+E Go to end of line
Ctrl+F Go right one character
Ctrl+B Go left one character
Ctrl+K Delete to end of line
Ctrl+U Delete entire line (different from readline, in which Ctrl+U deletes to the beginning of the line)
Ctrl+W Delete word, left
Ctrl+H Delete character, left (backspace)
Ctrl+D Delete character, right

Additional Readline keys not supported by Firefox:


Ctrl+T transpose characters
Ctrl+Y yank (like paste)
Ctrl+P previous line (like Readline's Up)
Ctrl+N next line (like Readline's Down)
Ctrl+S search
Ctrl+R reverse search
Ctrl+J return
Ctrl+M carriage return
Ctrl+L redraw

Windows

For Windows you can download XKeymacs, an OpenSource keybinder that works with most Windows apps. You can also follow the instructions below.

Any OS, without GTK

For both Windows and OS X, you can manually edit the platformHTMLBindings.xml file and add the handlers listed below to each <handlers/> section for which you desire the new bindings. (On OS X, you will need to open show the contents of the application package by choosing "Show package contents" from the contextual menu of the application in the Finder.) Locate the file as appropriate for your browser version, then add the entries below and restart the browser.

Pre 1.05

For versions before 1.0.5 this file is located at $MOZILLA_HOME/res/builtin/platformHTMLBindings.xml

Post 1.05

As of firefox 1.0.5 platformHTMLBindings.xml has moved inside the archive $MOZILLA_HOME/chrome/toolkit.jar (content/global/platformHTMLBindings.xml). You'll need to extract the existing file from the archive, modify it, then update the archive with the modified file.

To extract the existing file, navigate to the chrome directory in a terminal and type the following:

jar xf toolkit.jar content/global/platformHTMLBindings.xml

The file will be extracted to ./content/global/platformBindings.xml. After updating the file with the bindings listed below, update the jar file by invoking the following at the same location:

jar uf toolkit.jar content/global/platformHTMLBindings.xml

Entries to add to platformHTMLBindings.xml

<!-- Emacsish single-line motion and delete keys -->
<handler event="keypress" key="a" modifiers="control" command="cmd_beginLine"/>
<handler event="keypress" key="e" modifiers="control" command="cmd_endLine"/>
<handler event="keypress" key="b" modifiers="control" command="cmd_charPrevious"/>
<handler event="keypress" key="f" modifiers="control" command="cmd_charNext"/>
<handler event="keypress" key="h" modifiers="control" command="cmd_deleteCharBackward"/>
<handler event="keypress" key="d" modifiers="control" command="cmd_deleteCharForward"/>
<handler event="keypress" key="w" modifiers="control" command="cmd_deleteWordBackward"/>
<handler event="keypress" key="u" modifiers="control" command="cmd_deleteToBeginningOfLine"/>
<handler event="keypress" key="k" modifiers="control" command="cmd_deleteToEndOfLine"/>


<!-- Emacsish single-line motion and delete keys -->
<handler event="keypress" key="a" modifiers="control" command="cmd_beginLine"/>
<handler event="keypress" key="e" modifiers="control" command="cmd_endLine"/>
<handler event="keypress" id="key_left" key="b" modifiers="control" command="cmd_charPrevious"/>
<handler event="keypress" id="key_right" key="f" modifiers="control" command="cmd_charNext"/>
<handler event="keypress" id="key_delback" key="h" modifiers="control" command="cmd_deleteCharBackward"/>
<handler event="keypress" id="key_delforw" key="d" modifiers="control" command="cmd_deleteCharForward"/>
<handler event="keypress" id="key_delwback" key="w" modifiers="control" command="cmd_deleteWordBackward"/>
<handler event="keypress" id="key_del_bol" key="u" modifiers="control" command="cmd_deleteToBeginningOfLine"/>
<handler event="keypress" id="key_del_eol" key="k" modifiers="control" command="cmd_deleteToEndOfLine"/>

<!-- Emacsish multi-line motion and delete keys -->
<handler event="keypress" id="key_linedown" key="n" modifiers="control" command="cmd_lineNext"/>
<handler event="keypress" id="key_lineup" key="p" modifiers="control" command="cmd_linePrevious"/>
<pre>