You just got Vim’ed!
June 27th, 2010 | by emontero |An additional pet peeve of mine resulted in yet another simple hack. I’ve found myself using my mouse too much while reading lengthy articles online. This fact became acutely evident when I was going through Google’s Javascript Pacman implementation. Pressing the space bar in Firefox scrolls down, so that’s great (I don’t need to move my hands off of a typing position). But what if I wanted to scroll up with my keyboard too? Wouldn’t it be cool if I could start moving around a page as soon as I land on it using my keyboard and some good old Vim-like key presses? Yes, it’d be pretty awesome.
Vim has the following default keyboard mappings for moving around a document:

So if you want to scroll up, you press k. If you want to scroll down, you must press j. The Javascript code that gives Firefox the ability to scroll up and down using these key mappings is this:
function captureKeyPress(event){
if(!event) event = window.event; var unicode = event.charCode ? event.charCode : event.keyCode; var actualkey = String.fromCharCode(unicode); if (actualkey == "j") window.scrollByLines(20); else if (actualkey == "k") window.scrollByLines(-20); } window.addEventListener("keypress", captureKeyPress, true);
Now you just need to get Greasemonkey, install the script, and you’ll be all set!
Vim’s key mappings image found at swaroopch.com.
2 Responses to “You just got Vim’ed!”
By Scott on Jun 28, 2010 | Reply
Or, you could give yourself much more vi/Vim-like features in Firefox by installing the Vimperator add-on and mapping (in ~/.vimperatorrc) shift+space to scroll up, which is more convenient than ctrl+b, with: ‘map ‘
By emontero on Jun 28, 2010 | Reply
Scott,
Thanks for the tip! A friend of mine brought Vimperator up to my attention on FB too. I’ll definitely check it out. However, where’s the fun in just using Vimperator? An excuse for hacking some Javascript code together is orders of magnitude cooler. Wouldn’t you agree?