Keybindings in Cocoa

      Comments Off on Keybindings in Cocoa

SeeYesterday a new version of SubEthaEdit was released (2.3) and with it came a change in their licensing. The latest version requires registration (no longer free to educational sites) but there is still an “edu” discount so the registration fee is an affordable $19.95. I really rely on this editor for a couple of specialized tasks, so there was no real option other than registering a copy and grabbing the latest version. It’s still great and sports a number of new features, changes and bug fixes. But that’s not the point of this posting.

Now that I had made an actual “investment” in the program, I decided to move beyond using it just for collaborative document creation and try relying on it for various editing tasks (I seem to spend most of my editing time in a terminal window using the JOE editor). I pulled in a couple of PHP scripts from the library’s website and began a little code cleanup—figuring I’d begin getting more familiar with the program in the process. Syntax highlighting and code completion are really cool but I quickly discovered that all the keyboard commands I use for moving around in a file were missing—and pressing Option-this and Command-that to move around was killing my productivity.

A bit of background: I like the Joe editor because it mimics the old WordStar/Borland editor key commands (and yes, I still run remapping software to put the CTRL key back where it was in 1986 before IBM introduced the 101-Key “Enhanced” keyboard). Who cares, right? I agree. Anyway, I decided to send a note to CodingMonkeys (creators of SubEthaEdit) to ask if there were a way to remap keys. I didn’t expect much but I got a really great answer back from Martin Pittenauer, one of the original “coding monkeys.”

Not only is there a way to modify keys for SubEthaEdit, it extends to any application built with the Cocoa AppKit text edit objects. Here’s how you do it.

The Cocoa default key bindings are stored in:

/System/Library/Frameworks/AppKit.framework/Resources/StandardKeyBinding.dict

This is an XML file and should not be touched. Instead, you create a new file under your home directory:

~/Library/KeyBindings/DefaultKeyBinding.dict

This will overwrite any settings in the default file so only modify the keys you want to change. It is a simple text file that follows this format.

/* ~/Library/KeyBindings/DefaultKeyBinding.dict */
{
“KEY1” = “ACTION1”; /* Bind KEY1 to ACTION1 */
“KEY2” = “ACTION2”; /* Bind KEY2 to ACTION2 */

}

So, to bring a bit of WordStar goodness to SubEthaEdit (and several other Cocoa Apps I use), I created this file:

/* ~/Library/KeyBindings/DefaultKeyBinding.dict */
{
“^f” = “moveWordForward:”;
“^a” = “moveWordBackward:”;
“^x” = “moveDown:”;
“^e” = “moveUp:”;
“^t” = “deleteWordForward:”;
“^d” = “moveForward:”;
“^s” = “moveBackward:”;
“^g” = “deleteForward:”;
“^r” = “moveToBeginningOfDocument:”;
“^c” = “moveToEndOfDocument:”;
“^y” = (“moveToBeginningOfLine:”,”deleteToEndOfLine:”);
}

Your may not want to go back to the future with your keymappings but for even a small change you’ll need some documentation.  Here’s a source of information collected and posted by Llew Mason (of the Rabbit Radio Dashboard Widget fame):