Keybindings that everyone should have

[Tweet : ADN : nvALT]

I’m very aware that while many are impressed with my massive keybindings file, 99% of people who check it out shy away from ever implementing it. Even I don’t implement all of it. I thought I’d point out a few of my favorites that you can set up on their own in a more manageable way.

To use any (or all) of these:

  1. Create a blank text file
  2. Add a pair of curly brackets like this:

     {
         // insert your bindings here
     }
    
  3. Save the file as “~/Library/KeyBindings/DefaultKeyBinding.dict” (unless you already have one!)
  4. Restart apps as needed for the keybindings to load

TextMate-style Command-Return

One of the keybindings that I miss the most when I’m outside of a good code editor is ⌘↩. In most editors (and first in TextMate to the best of my knowledge), hitting this combination will insert a new line after the current paragraph and jump you to it, regardless of where the cursor is in the paragraph.

I also use one with ⌘⇧↩ that will do the same, but above the current paragraph.

// TextMate Command-Return (Command Enter)
"@\U000D" = (moveToEndOfParagraph:, insertNewline:);
// Insert blank line above paragraph (Command Shift Enter)
"@$\U000D" = (moveToBeginningOfParagraph:, moveLeft:, insertNewline:);

Select the current paragraph with Option-s

You can select a paragraph by hitting ⌘←, using the up arrow to get to the beginning, then hitting ⌘⇧→ and using the down arrow to get to the end. That’s horrible.

You can use ^a to jump straight to the beginning of the paragraph instead of the edge of the screen. Then hit ^⇧e to select to the end of the actual paragraph instantly. That’s much better.

If you’re me, though, you want to do that in a single stroke. This keybinding lets you select the current line/paragraph with ⌥s. If you want to make sure you get everything, including the last line break, use ⌥⇧s.

// select entire line/paragraph
"~S" = (moveToBeginningOfParagraph:, moveToEndOfParagraphAndModifySelection:, moveRightAndModifySelection:);
// select from beginning of paragraph to last character
"~s" = (moveToBeginningOfParagraph:, moveToEndOfParagraphAndModifySelection:);

Hyphenate a series of words

If you blog at all, you’re used to creating slugs (hyphenated words). If you do anything with file naming and prefer to keep spaces out of your titles, you’ve probably done this as well. This keystroke (⌘⌥⇧-) lets you add a hyphen between the next two words from the cursor, then advance so that repeated keystrokes continue to hyphenate.

// hyphenate next space and move to next word 
"@~_" = (selectWord:, moveRight:, setMark:, moveWordRight:, moveWordLeft:, moveLeft:, selectWord:, insertText:, "-", moveWordRight:); 

Save your spot in a text file

With two simple commands you can have a pair of keyboard shortcuts that will store the cursor position in your text so you can go and making an edit or check a reference elsewhere in the document, then jump right back to where you were. I bind these to ⌥1 and ⌥2.

// bookmark
"~1" = (setMark:);
// jump to bookmark
"~2" = (swapWithMark:,centerSelectionInVisibleArea:);

Move lines around

This one is especially handy when working with lists in Markdown, but is good for arranging paragraphs or lines of text anywhere. With ^⌘↑ and ^⌘↓ you can move the current line of text above or below whatever precedes or follows it.

Note: this will balk if you try to move into an area with no newline where it’s trying to go. Thus, the last item in a list that’s the last thing on the page is a little problematic. Beyond that minor snag, this is something I use daily.

// move line up
"^@\UF700" = (selectParagraph:, setMark:, deleteToMark:, moveLeft:, moveToBeginningOfParagraph:, yank:, moveLeft:, selectToMark:, moveLeft:);
// move line down
"^@\UF701" = (selectParagraph:, setMark:, deleteToMark:, moveToEndOfParagraph:, moveRight:, setMark:, yank:, moveLeft:, selectToMark:);

You can always use alternate keybindings such as ^⌘j (^@j) and ^⌘k (^@k) with these. In fact, edit all the bindings to match your needs. @ is ⌘, ~ is ⌥, ^ is Control, and $ is ⇧. Knock yourself out!

For more on my keybindings, check the archive!