Arrow keys, schmarrow keys—some of the best programs out there let you move around from the home row just like vim intended it.
Without diving headfirst into an ancient Linux holy war, let me set the record straight. I am a card-carrying, home-row hugging, Esc-key hammering, vim user. If you love Emacs, JOE, ed, Kate, gedit or the magnifying-glass-and-magnet approach to text-file editing, that's fine, and I'm not here to judge. It's just that for me, once I got over the initial vi(m) learning curve, I started looking for other tools that take the same approach to key bindings. Specifically, I am talking about the h, j, k and l keys and how you can use them to move left, down, up and right, respectively, in a document. What I found was that most of my favorite tools either already had vi-style key bindings or there was a simple way to enable them. Some programs even offered advanced bindings that closely mimic vim in a number of ways. In this column, I highlight some programs that either have vi key bindings or can be made to have them with a few simple steps.
Before I start talking about specific programs, I probably should explain why navigation with hjkl is better than with the arrow keys. It's a dirty secret among vim users that many people just use the arrow keys and backspace to edit their documents. The main reason hjkl navigation is great is that all of those keys are on the home row. In case you never took formal typing, the home row is the asdfghkl row of keys on a qwerty keyboard. If you learn to touch type, you are taught to rest your fingers on this row by default. This means the hjkl keys are within easy reach, but every time you reach for the arrow keys you have to move your right hand off the home row. Now, if you aren't a touch typist, that isn't a big deal. But if you are, it is almost as disruptive as reaching for the mouse. Granted, I know it is awkward at first, but if you are a vim user and touch type at all, it's worth it to force yourself to use hjkl for navigation until it becomes second nature.
In case you are new to vi key bindings, here are some of the main keys that you'll find work similarly in other programs:
h — move left
j — move down
k — move up
l — move right
^ — move to the beginning of a line
$ — move to the end of a line
G — move to the bottom of the document
g — move to the top of the document (gg in vim)
w — move the cursor ahead one word
b — move the cursor behind one word
/ — enter search mode
n — go to the next search result
N — go to the previous search result
A number of standard command-line programs use vi-style navigation out of the box, and the first I want to mention is less. The less program allows you to page through a text file, and if you needed yet another reason to use less instead of more, use it because j and k will move down and up a document. In addition, you can type G to scroll to the very bottom of a document and g (gg in vim) to move to the very top. As with vim, you also can press / to type a search term, and press Enter, then press n and N to find the next and previous matches, respectively. Like with less, by default, you can scroll through man page output with the same keys.
Screen also can use vi key bindings to navigate through its copy mode. Screen is an amazing shell program that allows you to open multiple shell sessions and detach and re-attach to them. If you've started using screen after being used to a regular terminal session, you likely ran into the strange behavior screen exhibits when you press Shift and PgUp and PgDn (or use the scroll bar) to scroll up and down through the output. In screen, if you want to view output that has scrolled past the top of the terminal, simply press Ctrl-A Esc to enter copy mode. Within copy mode, now you can use the arrow keys (shame on you) or hjkl to scroll around the output. As with less, you also can use g and G to scroll to the top and bottom of the output. When you are done scrolling, simply press the q key to exit copy mode.
Even bash itself can be set so that you can navigate the command line in true vi style. In your bash shell, just type set -o vi. Now, keep in mind that once you enable this option, you will have to enter insert mode (press the i key) to insert text just like in vi. And, if you want to use h or l to move the cursor left or right, or w or b to move forward or back a word, you will have to press Esc to leave insert mode. For those of you who tried this and want to undo it, simply press i to enter insert mode, and then type set -o emacs.
Here's yet another opportunity for me to add one more reason I love mutt as an e-mail program—it's practically vim's key-binding cousin. In fact, when you first start using mutt, you'll notice that when in doubt, you often can just press the same keys you'd use in vi to do something similar in mutt. The only place you might become confused initially is once you open an e-mail message and read it. By default, the j and k keys switch to the next and previous e-mail message in your folder, even when an e-mail is open, so you do have to teach yourself to use Enter and backspace to scroll through the body of an e-mail message.
Netris is a great command-line Tetris clone available on most major Linux distributions. One thing that always bugs me about Netris is that although it uses much of the home row to rotate and move shapes in the game, the keys are just slightly off from what you'd expect them to be in vi. Luckily, you can change the key bindings when you start Netris, so for true vi keys execute:
netris -k "hkl j"
Doing the above causes h to move pieces left, l to move them right, k to rotate them, j to make a piece drop faster and the spacebar to drop a piece to the bottom immediately. My Netris score was much improved once I could play it like vi.
Unfortunately, Firefox doesn't use vi key bindings by default (although Google Reader does), but it's not surprising that this can be fixed with a Firefox plugin. The Vimperator plugin (vimperator.org/trac/wiki/Vimperator) is extensive enough to deserve a column of its own (in fact, send me an e-mail at email@example.com, if you'd be interested in that). Essentially, once the plugin is installed, your entire Firefox session turns into a modal vi-style session. Not only can you use hjkl, g, G and so forth to navigate pages, but also when you are in a text field, Vimperator actually moves into insert mode! You even can record and play back macros just like in vim. Vimperator adds a bunch of other features to make keyboard-only Web browsing not only possible, but also preferable to the mouse. If you are a vim lover and haven't installed Vimperator yet, I highly recommend it.
As you dig around both command-line and GUI programs, you'll find that a surprising number of them at least support hjkl, if not more-extensive vi key bindings. I've listed only some of my favorites here, but the next time you open a program, press j a few times—you just might be surprised when the program scrolls down.