Skip to content

Instantly share code, notes, and snippets.

@ttscoff
Created November 8, 2011 23:45
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ttscoff/1349738 to your computer and use it in GitHub Desktop.
Save ttscoff/1349738 to your computer and use it in GitHub Desktop.
Flexible DefaultKeyBinding trick to continue plain text lists
// Add to (or create) ~/Library/KeyBindings/DefaultKeyBinding.dict
// Incorporated change by Lri to not require additional words on the line to work
// fixed to handle first character of list item being non-alphanumeric
// Issues:
// If there are more than one symbols ([[, ![, etc.) at the beginning of the line, anything beyond the first one is captured and added to new line
// FIXED: Entering more than one blank item in a row and then using ⌘Z leaves a placeholder character on the previous line.
{
// Command Option Enter
// continue a list item with indentation and
// include the same delimiter as the current line
"~@\U000D" = ( breakUndoCoalescing,
moveToEndOfParagraph:,
insertText:, "x",
moveToBeginningOfParagraph:,
selectWord:,
moveRightAndModifySelection:,
moveWordForwardAndModifySelection:,
moveWordBackwardAndModifySelection:,
moveLeftAndModifySelection:,
setMark:,
deleteToMark:,
yank:,
moveToEndOfParagraph:,
deleteBackward:,
insertNewlineIgnoringFieldEditor:,
yank:,
insertText:, " ",
selectWord:,
insertText:, " ");
}
@ttscoff
Copy link
Author

ttscoff commented Nov 8, 2011

It looks messy, but it takes advantage of the quirks of word movement in OS X. Preserves indentation of the current list item and picks up the same delimiter (*,+,-,1.,etc.) and repeats it. Works for Markdown because if the first item is any number, it doesn't matter what the following items are. It just becomes an ordered list.

This keybinding works from anywhere in the line, selected text or no. It won't break the line, just starts a new item below it.

@ttscoff
Copy link
Author

ttscoff commented Nov 8, 2011

This little part is my favorite:

            insertText:, " ", 
            moveLeft:,
            selectWord:,
            insertText:, " "

Removes extra space that might show up after the intial character by padding it with a space and selecting word (which will grab all adjoining space) and then replacing the whole selection with a single space.

@ttscoff
Copy link
Author

ttscoff commented Nov 8, 2011

For more: http://brettterpstra.com/keybinding-madness/

I have about 30 more new ones. I'll be blogging a few more soon.

@ttscoff
Copy link
Author

ttscoff commented Nov 9, 2011

Lri, you're such a spoilsport sometimes. I messed around a lot with using placeholders to stop the selectWord from going too far, but always ran into problems. I'll give yours a shot in a bit, though. You're usually right :).

@ttscoff
Copy link
Author

ttscoff commented Nov 9, 2011

Yours doesn't work with numbered lines (1.), which is what took me so long to figure out. Seems like the placeholder works perfectly, though, so I'll try to blend the two.

@ttscoff
Copy link
Author

ttscoff commented Nov 9, 2011

I should specify: it doesn't work with indented numbered lists. If there's not whitespace, it's fine.

@ttscoff
Copy link
Author

ttscoff commented Nov 9, 2011

"~@\U000D" = (moveToEndOfParagraph:, insertText:, "x", moveToBeginningOfParagraph:, selectWord:, moveRightAndModifySelection:, moveWordForwardAndModifySelection:, moveWordBackwardAndModifySelection:, setMark:, deleteToMark:, yank:, moveToEndOfParagraph:, deleteBackward:, insertNewlineIgnoringFieldEditor:, yank:);

Fixed

@ttscoff
Copy link
Author

ttscoff commented Nov 9, 2011 via email

@ttscoff
Copy link
Author

ttscoff commented Nov 9, 2011

Oh man, I just learned that breakUndoCoalescing works in keybindings. And you know where? From Lri.

P.S. the extra "x" problem is fixed now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment