Ok, this one will probably wrap up this little run of key binding tricks. It’s been fun, though, and I’d love to answer any questions I can in the forum.
If you’ve ever examined my master DefaultKeyBinding.dict
(or even looked at the readme), you’ll see that I make use of a lot of multi-stroke key combos. There are only so many key combinations available, and you’re bound to run out of ones that don’t conflict with other shortcuts on your system. But with the magic of multi-stroke bindings, you can take one available shortcut and add secondary “child” shortcuts with the whole keyboard available for assignment again.
For example, the SearchLink bindings I use start with ⌃⌘G. So I press that, then I can type a single letter to determine what type of SearchLink syntax gets inserted, e.g. ⌃⌘G followed by M inserts an IMDB search.
To create a mutli-stroke binding, you just nest a PLIST inside of the root PLIST, keyed off the initial key combination:
{
"^@g" = { // SearchLink commands
// IMDB Search
"m" = (setMark:, moveRight:, insertText:, " ", deleteToMark:, insertText:, " [", moveLeft:, deleteBackward:, moveRight:, yank:, moveLeft:, insertText:, "](!imdb)", moveRight:, deleteBackward:, moveLeft:);
};
}
Going Deeper
You can actually continue nesting, creating three-or-more -stroke sequences, though I don’t know how many of those I could keep track of. Mnemonics do make it easy, though. I use a three-deep setting for my Markdown link insertions, where I type ⌃⌘W (the container for most of my Markdown commands, a throwback to the Blogsmith Bundle for TextMate), then L (for link), then either C (to paste from clipboard), or T to insert just the brackets with https://
selected in the url.
{
"^@w" = { // Multi-stroke Markdown commands
"l" = { // Markdown link
// create a link for selected text, cursor between () `[selected text]([cursor])`
"t" = (setMark:, moveRight:, insertText:, " ", deleteToMark:, insertText:, " [", moveLeft:, deleteBackward:, moveRight:, yank:, moveLeft:, insertText:, "]()", moveRight:, deleteBackward:, moveLeft:, setMark:, insertText:, "https://", selectToMark:); // link text
// create a link for selected text, inserting clipboard as url `[[cursor]selected text](clipboard contents)`
"c" = (setMark:, moveRight:, insertText:, " ", deleteToMark:, insertText:, " [", moveLeft:, deleteBackward:, moveRight:, yank:, moveLeft:, insertText:, "](", setMark:, pasteAsPlainText:, insertText:, ")", moveRight:, deleteBackward:, moveLeft:, selectToMark:); // link with clipboard
};
};
}
These sequences open up pretty much limitless possibilities for memorably (mnemonic) keyboard shortcuts. You create a logical grouping on an open shortcut, and then assign single letters that are meaningful and easy to remember in context. These nested keystrokes can also use modifier keys, but I like the fact that once I’m inside of the main grouping, I only need single letters and not weird key combinations.
As always, check out the KeyBindings project for more inspiration. Have fun!