Mozilla Vim Keybindings
From Vim Tips Wiki
Tip 317 Previous Tip • Next Tip
Created: August 21, 2002 Complexity: basic Author: Matthew J. Stott Minimum version: 5.7 Karma: 112/39 Imported from: Tip#317
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"> 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.
[edit] Comments
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!
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).
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.
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"/>
This works in Mozilla Firebird 0.7 on Windows (and presumably 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 :)
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.
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.
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.
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.
or even better to use the keyconfig tool: http://www.calmar.ws/firefox/index.php#keyconfig
Anyone who reads this might be interested in karat browsing in mozilla/firefox. Just press F7 and you have a blinking cursor that you can move through the text to navigate the page and select text.
fortunately, it's not hard to setup vi vim (single letters) keybindings on Opera (free/gratis).
And it actually works (includings searching for links / text (and while typing there, the letters are taken literary, what is the bug in firefox))
At the moment you can modify the file platformHTMLBindings.xml in /chrome/toolkit.jar to get the vim keybindings enabled.
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/
Check out the Vimperator extension at http://vimperator.mozdev.org/
