Martin Ström
my-domain.se

AutoPair functionality using Cocoa KeyBindings

I’ve been using the application “AutoPairs” since it was mentioned on 43folders some time ago. Yesterday (via the Macromates TextMate blog) I found an article on the Cocoa Text System and realised I could recreate AutoPairs’ functionality by just using native Cococa Keybindings found in OSX.

Add these lines to your ~/Library/KeyBindings/DefaultKeyBinding.dict (create the file if it isn’t already there) and the commands will be available in every application started afterwords.

/* Recreate Auto pairs functionality */
"\\\"" = ( "insertText:", "\"\"", "moveBackward:" );
"("    = ( "insertText:", "()", "moveBackward:" );
"~("   = ( "insertText:", "{}", "moveBackward:" );
"~8"   = ( "insertText:", "[]", "moveBackward:" );
"<"    = ( "insertText:", "<>", "moveBackward:" );

When you type e.g ⇧2 for a double quote (as in swedish keyboard layout), it’ll automatically insert the ending quote as well and place the cursor between them. This will also work for “([{<”. Remember the shortcuts are for a swedish keymap so you might want to change that.

A real time saver when writing outside an editor without this functionality built-in (like TextMate).

Update 2006-08-07 Simplfied the commands based on Jacob Rus’ comment.

CSS is in progress