sponsor Vim development Vim logo Vim Book Ad

basic Tip #317: Mozilla Vim Keybindings

 tip karma   Rating 113/40, Viewed by 10591 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   August 21, 2002 21:23      complexity:   basic
author:   Matthew J. Stott      as of Vim:   5.7

If you use VIM you like the h, j, k, and l movement keys.  I found myself annoyed at not having these movement keys available when browsing web pages.  Moving to the arrow keys on a laptop is annoying to just scroll the page.  

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">;

  <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>


There are many more bindings one could configure to get Vim like keybindings.  You can read http://www.mozilla.org/unix/customizing.html#keys for more information.

PS.  I love the keymaster/gatekeeper xul reference in the xul URL above.  It's from the original GhostBusters movie.  ;-)




  

 rate this tip  Life Changing Helpful Unfulfilling 

<< Using /pattern/ search in a script | Extended Bracket and Parenthesis + extras for perl >>

Additional Notes

Anonymous, September 7, 2002 19:53
I found this didn't work in userHTMLBindings.xml, but adding relevant lines in htmlBindings.xml works great - thank you, this is one of those things I've always wanted!
Mohit Kalra, September 8, 2002 1:35
This comment is not exactly related to vim.  Anyways, checkout
http://labs.google.com/keys/index.html
Google for the keyboard aficionados.
-mohit
Anonymous, September 17, 2002 22:45
I admit 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 (*nix 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).  

Good Luck
Anonymous, September 17, 2002 22:48
Didn't know about the Google Keys, that's pretty slick.  
[email protected], September 29, 2002 9:17
Maybe the _greatest_ tip I found here! Thank you!
It is a real live saver for me as I have been used to using lynx for years and I always used the vi motion in lynx.
I don't use Mozilla but Netscape 7. However, it worked all well for me (under Linux mdk 8.2)
Absolutely fabulous would be to be able to mimic all lynx keystrokes:
e.g.
"/" to search for a word (crtl-F in Netscape 7)
"g" to open a new URL
"v" to open the bookmarks page (alt-B in Netscape 7)
"a" to add a bookmark (crtl-D in Netscape 7)
etc.
I couldn't figure out how to do it yet.
Any idea?
Anonymous, October 15, 2002 9:45
Probably not, at least not without hacking the Mozilla source.  Most of what you listed would include a corded keypress.  i.e. ALT+Key, CTRL+Key.  All the Mozilla docs state for legal commands would be mostly navigation keystrokes like pageup/down, etc.  Also some editing and clipboard stuff.  See the link in the Tip for the Mozilla.org documentation.  Of course the modifier keys (CTRL+ALT, etc.) are customizable as it is done for each platform.  i.e. Mac's have a CMD key (Apple Key) that is used instead of CTRL.  A Mac has CTRL, Option(ALT), and Command(apple logo).  Sun has a CTRL, ALT, and a Meta key.  It may still be somewhat possible, you would have to replace the modifier key cording with a single key press.  However, this may produce undesirable effects that could impact normal Mozilla operation, etc.

Netscape 7 is really Mozilla under the hood.  Netscape took Mozilla as the base and added some stuff that isn't open source.  i.e. AIM client and some other things.  I find Netscape 7 to be bloated and full of useless fluff, but that's my opinion.  

Gecko is the rendering engine under the Mozilla hood.  XUL is a way to create a browser interface and can be heavily skinned.  XUL is literally rendered by Gecko.  So your widgets are all drawn by the render engine.  Neat concept but it can get a bit bloated (it's gotten much better with the 1.x releases).  There are many projects that take Gecko and wrap it up in a particular widget kit so it doesn't use XUL for the interface.  This eliminates the skinning but it makes for a lighter smaller non-cross platform browser, which apparently many people want badly.  Gnome, KDE, Win32, Cocoa/Aqua, etc., etc., etc.  They seem to want a native widget kit that matches their platform better.

Ultimately, I would want a lightweight browser that is really fast and good at one thing, rendering pages and loading plugins.  I could care less about email, chat, etc.  I have other programs for that stuff.  


hari_vim at yahoo dot com, March 4, 2003 17:30
Thanks for the tip, it 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 like unexpected.

Hari
[email protected], January 28, 2004 5:58
Great tip!  I got it working under Moz 1.6 and added these to make it even more Vimlike:

<handler event="keypress" key="g" command="cmd_scrollTop"/>
<handler event="keypress" modifiers="shift" key="g" command="cmd_scrollBottom"/>
<handler event="keypress" key="b" command="cmd_scrollPageUp"/>

Bob
Mark Rushakoff, February 28, 2004 3:14
This works in Mozilla Firebird 0.7 on Windows (and assumably Firefox), too.  I added the code just as the original poster had it, to res/builtin/htmlBindings.xml underneath my MozillaFirebird folder.  However, I was having trouble with "find as you type" because when it was enabled, hjkl would look for text, but when it was disabled through tools->options I couldn't even search after pressing slash.  However, typing about:config into the location bar takes you to a config page where you can set accessibility.typeaheadfind to true, and  accessibility.typeaheadfind.autostart to false, thereby solving the problem.  It's great to be that much more free from arrow keys :)
FullyIonized, March 23, 2004 10:36
Great tip!  The latest versions of Mozilla have the search key "/" and search links "'" working correctly which is also very useful.  After finding text, I often highlight it using shift-Arrow keys and the CTRL-c to copy it.  Does anyone know how to use SHIFT-movement keys to work?  For example, I would like this:
      <handler event="keypress" key="h" modifier="shift" command="cmd_selectCharPrevious"/>
      <handler event="keypress" key="j" modifier="shift" command="cmd_selectLineNext"/>
      <handler event="keypress" key="k" modifier="shift" command="cmd_selectLinePrevious"/>
      <handler event="keypress" key="l" modifier="shift" command="cmd_selectLineNext"/>
But I find these don't work.  I even tried variations of the modifier key to no avail.

[email protected], April 16, 2004 18:51
It worked for me for mozilla 1.6.  I wrote the key bindings in userHTMLBindings.xml. When I tried to do the same thing in firefox. It worked but I needed to disable the "Find as you type" feature which I find it very useful so I can't use both vim setting as well as Find as you type feature in Firefox. Though that was working great in Mozilla. I probably need to dig more. Any comments.
Sven Suursoho, May 21, 2004 5:08
Using vim keys in Opera (tested with 7.23 and 7.50):

; 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

During modification Opera should be shut down.
After that you can use vi keys 'j' & 'k' to scroll up and down and 'h' & 'l' to go to previous or next page in history.
Pressing shift and vi movement keys allows to navigate through links found in page.
[email protected], June 3, 2004 16:00
Well, I put some things together from here and other places.
The result really a 'vim' like firefox.

http://www.calmar.ws/firefox

Cheers
[email protected], July 26, 2004 17:31
An alternative to hacking your own bindings is to use the superscroll extension (http://update.mozilla.org/extensions/moreinfo.php?id=53), you can easily get `j' and `k' to work.
[email protected], November 29, 2004 19:30
or even better to use the keyconfig tool:

http://www.calmar.ws/firefox/index.php#keyconfig

Cheers
Anonymous, December 3, 2004 20:04
Anyone who reads this might be interested in karat browsing in mozilla/firefox. Just press F7 and you have a blinking curser that you can move through the text to navigate the page and select text.
[email protected], September 4, 2005 0:02
Does anybody know how to use this tip with new Mozilla/Seamonkey builds?

There is no [...]/res/builtin/ directory any more where I could put the described file.

Thanks for your suggestions!
[email protected], November 18, 2005 18:58
Well,

fortunately, it's not hard to setup vi vim (single letters) keybindings on Opera (free/gratis).

http://www.calmar.ws/opera

And it actually works (includings searching for links / text (and while typing there, the letters are taken literary, what is the bug in firefox))

(I still don't know why the firefox developers obviously don't care for keybindings / shortcuts)

cheers
calmar
Anonymous, November 28, 2005 23:57
At the moment you can modify the file platformHTMLBindings.xml in /chrome/toolkit.jar to get the vim keybindings enabled.
Anonymous, August 10, 2006 4:35
I've done this before: i'm using h-j-k-l keys to navigate (j-k: up-down, h-l: history back-forward); however, i haven't reconfigured firefox, but used a little extension that can do this. This extension is called "Mouseless Browsing", and it's amazingly useful: it adds numbers after each link, allowing you to click on links, but without a mouse.

It can also be configured to bind a few keys to some actions: this is what i've done for h-j-k-l.

Give it a try, and don't get your hands away from your keyboard anymore:
https://addons.mozilla.org/firefox/879/

rancid
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
Sponsored by Web Concept Group Inc. SourceForge.net Logo