KeyBinding madness

[Tweet : ADN : nvALT]

There’s a somewhat mysterious individual who is consistently leaving brilliant little pieces of code and information in my comments. Dr. Drang has been visited by this apparition as well. We’re both duly impressed with what he (or she?) does.

So, I’m digging through the code Lri’s posted and I find a DefaultKeyBinding.dict file. It’s the best one I’ve ever seen, and it inspires me to do some “fiddling” of my own. I figured I’d post my result, but with due credit to Lri (hi, Lri!).

The DefaultKeyBinding.dict file tells your computer at a fairly root level how to handle keyboard shortcuts in input fields. It doesn’t work everywhere, but sometimes it’s surprising where it does work. In those cases, you can save yourself a ton of time by committing a few custom keystrokes to memory.

You may be familiar with the basic Emacs keybindings that are default (Ctrl-a beginning of line, Ctrl-e end of line, etc.), but this allows you to go much further. For more information, check out Macworld hints, an older but very relevant article from Harvard and do a search for more information.

Basically, all you need to do is put the DefaultKeyBinding.dict file into ~/Library/KeyBindings and restart applications you want it to work in. Edit away, and change shortcuts to things you’re comfortable with. Several of mine are sequences which allow groupings, which I like, but you might not. Be aware that these can be a little unpredictable as to when they’ll override a system/application default and when they’ll be overridden (I know there’s a logic to it, I just don’t want to dive into that).

I’ve posted my KeyBindings on GitHub and included a cheatsheet in MultiMarkdown as well as a Readme with the tables already rendered. Direct download link below. If you’re looking for something to nerd out on this weekend, this should whet your appetite.

Killer DefaultKeyBinding.dict v1.0

A custom DefaultKeyBinding.dict file which adds shortcuts for document navigation, Markdown and much more. Inspired by Lri.

Updated Sat Aug 13 2011.

DonateMore info…

Just for reference, here’s the table of shortcuts in the KeyBindings file:

Grouped items begin with the groups shortcut, followed by the Keys specified. Items separated by commas are sequential, not concurrent.

All purpose key combos
Group Keys Function
Opt-z copy character before cursor
Opt-r repeat character before cursor
Opt-t transpose characters
Opt-+ uppercase word
Opt– lowercase word
Opt-. capitalize word
Ctrl-w delete word before cursor
Opt-w select word
Opt-f cut word (fold)
Opt-g copy word (grab)
Opt-h paste word (hit)
Opt-l select entire line/paragraph
Opt-s select from beginning of paragrah to last character
Opt-d delete line/paragraph
Opt-y copy paragraph
Opt-x cut paragraph
Opt-p paste paragraph below
Opt-Shift-P paste paragraph above
Ctrl-Shift-A select to beginning of paragraph and copy
Ctrl-Shift-E select to end of paragraph and copy
Opt-q cut to beginning of paragraph
Opt-k cut to end of paragraph
Opt-, move paragraph up a line, inserting blank space after
Opt- move paragraph to end of document
Opt-o blank line after current
Opt-Shift-O blank line before current
Cmd-Opt-/ comment with //
Cmd-Opt-3 comment with #
Markdown-specific shortcuts (Ctrl-Cmd-W)
Group Keys Function
Cmd-Opt-b bold selection
Cmd-Opt-i italicize selection
Ctrl-Cmd-w Return force Return
Ctrl-Cmd-w Tab force Tab
Ctrl-Cmd-w 14 # Headlines
Ctrl-Cmd-w l,t link text "[selected text](|)"
Ctrl-Cmd-w l,c clipboard link "[|selected text](clipboard contents)"
Ctrl-Cmd-w i,t image, selection as alt "![selected text](|)"
Ctrl-Cmd-w i,c clipboard image "![|selected text](clipboard contents)"
Ctrl-Cmd-w :,t create a reference from selected text
Ctrl-Cmd-w :,c create a reference from selected text, clipboard as url
Ctrl-Cmd-w [ insert reference link
Ctrl-Cmd-w +|-|* lists
Opt-Shift-L new list item after current
Ctrl-Cmd-w e entity
Ctrl-Cmd-w / http://
Surround (Ctrl-Cmd-s)
Group Keys Function
Ctrl-Cmd-s ( wrap () with spaces
Ctrl-Cmd-s ) wrap () no spaces
Ctrl-Cmd-s [ wrap [] with spaces
Ctrl-Cmd-s ] wrap [] no spaces
Ctrl-Cmd-s { wrap {} with spaces
Ctrl-Cmd-s } wrap {} no spaces
Ctrl-Cmd-s < wrap <> with spaces
Ctrl-Cmd-s > wrap <> no spaces
Ctrl-Cmd-s wrap single quotes
Ctrl-Cmd-s ` wrap backticks
Ctrl-Cmd-s wrap double quote
List/Paragraph motion
Group Keys Function
Ctrl-Cmd-k move line up
Ctrl-Cmd-j move line down
Ctrl-Cmd-l indent line
Ctrl-Cmd-h outdent line (all the way)
Ctrl-Opt-k copy line and paste above
Ctrl-Opt-j copy line and paste below
Cmd-Opt-Up modify selection up
Cmd-Opt-Down modify selection down
Cmd-Del Forward delete to end of paragraph
Document navigation
Group Keys Function
PgUp Page Up
PgDown Page Down
Opt-PgUp Scroll up
Opt-PgDown Scroll Down
Opt-1 bookmark
Opt-2 jump to bookmark