Advanced Text Manipulation via Custom KeyBindings in macOS

After working in the awesome Atom text editor for some time now, and getting spoiled with the awesome keyboard-focused text manipulation tools available, I wanted to find something for working in nvALT to solve a couple of tedious and incessant activities:

  1. Moving chunks of text up and down in the document
  2. Moving a chunk of text to the end of a document

The first need is mostly for organizing information, primarily task/to-do lists.

The second is to bump completed tasks to the end of the text document.

Initially, I expected to find some sort of macOS Service I could assign a keyboard shortcut to. But then I stumbled across Brett Terpstra’s post on custom KeyBindings in macOS.

There are a boatload of command you can implement, but I’m going to just share the few I cherry-picked from Brett’s exhaustive list. And the code to move the current paragraph to the end of the document I had to “archaeologize” from his GitHub repository for this project as it seems to have been removed from more recent versions of the file.

⌃⌘↑ Move line up

// move line up

"^@\UF700" = (selectParagraph:, setMark:, deleteToMark:, moveLeft:, moveToBeginningOfParagraph:, yank:, moveLeft:, selectToMark:, moveLeft:);
⌃⌘↓ Move line down

// move line down

"^@\UF701" = (selectParagraph:, setMark:, deleteToMark:, moveToEndOfParagraph:, moveRight:, setMark:, yank:, moveLeft:, selectToMark:);
⌥SPACE Move to end of document

// move paragraph to end of document

"~ " = (selectParagraph:, setMark:, deleteToMark:, moveToEndOfDocument:, yank:, swapWithMark:);

You’re going to want to read Brett’s detailed post about how to implement all of this, but the short version is to create a text file named DefaultKeyBinding.dict and save it to the ~/Library/KeyBindings/ directory (create /KeyBindings/ if it doesn’t already exist).

Paste each of the above chunks of code into the DefaultKeyBinding.dict file, enclosed with curly brackets:

{

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

}

There should only be one open curly bracket at the top, and one closing at the bottom. All the keybindings code chunks should go in between. Add as many as you prefer.

Restart open applications for this to take effect.

These are very powerful time-savers, and there are a ton more to dig through than the few I posted here. There were just the ones I found myself most consistently pining for when editing text in nvALT.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s