Wednesday, December 3, 2008

libedit and vi key bindings
OS X unix commands sometimes provide a interactive prompt for users to enter text. An example would be the Python or Ruby interactive interpreters. These interactive prompts require ways for users to move their cursors around quickly. The Mac has had standard keyboard shortcuts for text fields since the beginning of time (pretty sure MPW, but don't quote me)... but, it's Unix folks! Pick an editor: vi or emacs.

In the NeXT days, those engineers picked emacs - which is why you can use, say Control-A to jump to the beginning of the line in any Cocoa text field.

But I like vi. What to do? Turns out you can turn on vi bindings for console programs (that link to libedit....) You can even add your own commands!!

On OS X 10.5 most programs are use the same underlaying code to handle interactive text input: libedit. This is a pretty obscure library, compared to the more common (on linux systems) readline.

man editrc is the man page, but to do our task, here is the short of it is:

  1. Create ~/.editrc
  2. Add the line: bind -v
  3. There is no step three! Fire up your interactive console program and try it out!

Mac OS X Hints has more things you can do with .editrc