Technology
 

Vim key bindings for Firefox

From Vim Tips Wiki

(Redirected from VimTip317)

Tip 317 Previous Next created 2002 · complexity basic · author Matthew J. Stott · version 6.0


Vim users find the h, j, k, l keys useful for navigation. This tip shows some techniques for using similar key bindings in the Mozilla Firefox web browser.

Contents

[edit] Vimperator

Vimperator is a free and supported add-on for Firefox, and is the best way to configure Firefox to behave like Vim.

[edit] Other information

Some related information follows:

  • Mozilla has a reference on how to configure key bindings, if you want to configure Firefox yourself.
  • In Firefox, press F7 to toggle the text cursor on/off ("caret browsing").
  • Calmar on Firefox, Vim more information.
  • Calmar on Opera can configure Vim key bindings in Opera.
  • Mouseless Browsing add-on numbers each link; can click on links without a mouse.

[edit] Old information

The following is from 2002, and may not be useful for recent versions of Firefox.

Finding the arrow keys on a laptop is annoying, when you simply want to scroll the web page. Here is how to customize Mozilla browsers to use the Vim h/j/k/l keys.

  • Locate your mozilla/res/builtin directory (varies by platform). You could search for htmlBindings.xml to find it. (ie. locate htmlBindings.xml.
  • On Mac OS X it's inside the Mozilla application bundle. /Applications/Mozilla/Mozilla.app/Contents/MacOS/res/builtin.
  • Create a new XML file called userHTMLBindings.xml, making it executable for all users and making sure the CRLF format is the same as htmlBindings.xml. i.e. on Mac OS X most files use \r but Vim writes \n instead. You can either save the file with Vim as a Mac formatted file or use tr '\n' '\r' < input.xml > output.xml to convert the file.
  • Place the following XML into the userHTMLBindings.xml file.
<?xml version="1.0"?>
 <bindings id="htmlBindings"
  xmlns="http://www.mozilla.org/xbl";
  xmlns:xul="[http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"> http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">];
  <binding id="browserUser">
   <handlers>
    <handler event="keypress" key="h" command="cmd_scrollLeft"/>
    <handler event="keypress" key="j" command="cmd_scrollLineDown"/>
    <handler event="keypress" key="k" command="cmd_scrollLineUp"/>
    <handler event="keypress" key="l" command="cmd_scrollRight"/>
   </handlers>
  </binding>
 </bindings>

[edit] Comments

I found this didn't work in userHTMLBindings.xml, but adding relevant lines in htmlBindings.xml works great (see bug 201011).


It's a bit tricky to get this working. Double-check your file format (CRLF, LF, CR) for the platform you are on. Also check that you put the file in the right path for your OS. I've gotten this to work on Linux, Mac OS X, and Windows equally well. In some cases (Unix based environments) I think the files may need to be executable as well (in other words, where the other binding files were executable, I made the user binding executable as well).


The tip worked on Netscape 7.0 on Windows. But it seemed like it worked only after removing the couple of ";' in the initial two lines which seemed unexpected.


Using Vim keys in Opera (tested with 7.23 and 7.50). Close Opera while performing following modifications, which allow keys j/k to scroll up/down and h/l to go to previous/next page in history. Keys J/K/H/L navigate links in page.

; In directoy ~/.opera/keyboard/ there is file unix_keyboard.ini or something like that
; Under section [Application] add following items:
k=Scroll up
k shift=Navigate up
j=Scroll down
j shift=Navigate down
l=Forward | Fast Forward
l shift=Navigate right
h=Back
h shift=Navigate left