Key Bindings

Key Bindings is a feature of Mac OS X's Cocoa Text Input system that lets you bind text editing commands to keystrokes. The Key Bindings mechanism is responsible for causing the text behind the insertion point to be deleted when you hit the "delete" key, for example. However, there are tons of other functions that, if bound to reasonable keystrokes, can turn any Cocoa application's Text Input fields into full-on, keyboard-friendly text editors.

Take a look at Apple's hard-to-find introductory coverage of Text System Defaults and Key Bindings to get the general idea about Key Binding. Then see the NSResponder reference page, under the section "Action methods", for a list of all of the standard commands that can be bound to keystrokes.

Since switching from UNIX to Mac OS X, I've found myself automatically trying to use vi-like editing commands when entering text in text fields. Often, I'm more than slightly annoyed that they cause a beep, leave command turds in my text, or simply don't work. Key Bindings have helped me make OS X text fields feel a little more like vi, though.

Using the standard Input Manager, Text Input fields don't have modes like vi does, so you can't exactly duplicate vi to the degree I'd really like. For example, there's no Command Mode in which unmodified keys can move the cursor, delete text, et cetera; effectively, you are always in Insert Mode.

You can download my DefaultKeyBinding.dict file and install it in your ~/Library/KeyBindings directory to get an idea of some of the things you can do with Key Bindings. Once you've installed this file (creating the KeyBindings directory inside your Library directory if it doesn't exist already), you'll need to restart any Cocoa applications that you'd like to have take advantage of the new bindings. The DefaultKeyBinding.dict file is only read when an application starts. Also, remember that only Cocoa applications use this file; Carbon, Classic, and Plain Old Unix applications do not.

Here are the important entries from my DefaultKeyBinding.dict file (I recommend using /Developer/Applications/PropertyListEditor.app, with "Show Strings in ASCII" selected, to create/edit your DefaultKeyBinding.dict file):

Control-0
Extend selection to beginning of line.

Control-shift-0
Move to beginning of line.

Control-4
Exten selection to end of line.

Control-$ (Control-shift-4)
Move to end of line.

Control-vi-Movement-Keys
Control-h, -j, -k, -l extend the selection left, down, up, and right, respectively.

Shift-Control-vi-Movement-Keys
Control-Shift-H, -J, -K, -L move the insertion point left, down, up, and right, respectively.

Control-u, Control-d
Move the insertion point up or down one page.

Control-w
Extend the selection one word backwards.

Control-b
Move the insertion point one word backwards.

Control-Shift-W
Extend the selection one word forwards.

Control-c, Shift-Control-C
Extend the selection to the end or beginning of the line, respectively.

Control-{ (Control-left-curly-brace)
Move to the beginning of the paragraph.

Control-} (Control-right-curly-brace)
Move to the end of the paragraph.

I'm still figuring out "what feels more like vi" (or, at least, "what makes Text Input fields into more efficient text editors"), so I'm still changing this file regularly. Check back from time to time for revisions. Also, let me know if you come up with better bindings.


Aldebaran > Mike > Mac OS X > Key Bindings