Custom Key Binding Ideas

Key Binding to Swap Words

In the Web Excursions from February 28th, Brett referred to the NSStandardKeyBindingResponding documentation.

This gave me the idea to create a key binding that can swap two words.

For example, if I have the following text. I set a cursor between ipsum and dolor, hit Option-Shift-t and It swaps the words.

Lorem ipsum dolor sit amet
Lorem dolor ipsum sit amet

Here’s my key binding:

"~$t" = (moveWordLeft:, moveWordRightAndModifySelection:, cut:, 
deleteBackward:, moveWordRight:, moveRight:, 
paste:, insertText:, " ", moveWordLeft:);

Feel free to share additional ideas.

This is cool, but isn’t there a “transpose words” method built in? I haven’t tried it, but I recall seeing it…

I only know of Control-t to transpose two characters.

Oh, I found out what you are referring to. There is already a function transposeWords in NSStandardKeyBindingResponding.