Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Customize the Cocoa text binding system System
I just wrote a very detailed article about the Cocoa Text System, covering a whole lot of really cool stuff about how it works. I hope you all enjoy it! Anyway, I thought I'd distill three or four quick hints from the writeup into a general Cocoa text system hint for macosxhints. If any of this is unclear, please hop over to the article, which tries to be as explicit as possible, and explain things from the beginning.

Hint #0: Default keyboard shortcuts

I've made a list of all the default keyboard shortcuts in Cocoa text boxes.

Hint #1: Key bindings to emulate another environment

It is possible to create a special file, ~/Library -> KeyBindings -> DefaultKeyBinding.dict, which can modify the keyboard shortcuts in every Cocoa text field (which means most text fields in programs on OS X, though not Microsoft Word, or Firefox, or Adobe programs). This is a property list file which lists the actions to be performed with each of the desired shortcuts. It is possible, by modifying this file, to get Cocoa text fields to behave a bit more like Windows, or BBEdit (which has some excellent additions to standard Mac behavior, such as shift-delete for forward delete), or Emacs (the terminal-based editor that has more shortcuts than you can shake a stick at). Here are the partial emulation bindings I've come up with. To see how to extend these to do more, see my full article.
  • Emacs 1 (using Option key as meta)
  • Emacs 2 (using Escape as meta)
  • Windows (this is pretty incomplete, as I'm not too familiar with Windows' bindings, so leave a comment if there is other functionality you'd like to see)
  • BBEdit
To use one of these, simply take the text file, and rename it to DefaultKeyBinding.dict, and then put it in the ~/Library/KeyBindings/ folder (where ~ stands for your home folder), which can be created if it doesn't already exist. Then relaunch your Cocoa applications, and enjoy some new key bindings.

Hint #2: Key bindings to do really freaking cool stuff

It is possible to do some really wacky things with these key bindings. They can save your document, select, move, and copy text, change your formatting, even use the speech synthesizer to say your text out loud. The full article explains the format in detail, but one of my favorite things the system can do is type arbitrary text. This allows common phrases or letters to be entered with one keystroke.

For instance, imagine that I type lots of HTML code, and I'm getting tired of typing <blockquote></blockquote>, <code></code>, and <div></div> over and over, etc. Instead, I want to type Shift-Control-H (for HTML), and have the word I just typed turned into such a tag. If I add the following text into my DefaultKeyBinding.dict file, I can do just that:
  {
      /* This command will let Ctrl-Shift-H turn the previous word
       * into an HTML/XML opening and closing tag pair. */
      "^H" = ( "setMark:",
               "moveWordBackward:",
               "deleteToMark:",
               "insertText:", "<",
               "yank:",
               "insertText:", ">",
               "setMark:",
               "insertText:", "</",
               "yank:",
               "insertText:", ">",
               "swapWithMark:"
              );
  }
After putting the above text in DefaultKeyBinding.dict, try relaunching TextEdit, or Mail, or Safari, and typing any word, followed by Shift-Control-H. Prepare to be amazed! There is much much more that can be done with bindings, but I'll let you read the article to find out about it. I've tried to make a complete list of the actions that can be used with key bindings, but I'm sure I missed a few.

Hint #3: Incremental Search

It is possible to get incremental search working in all Cocoa text fields. Camino and Firefox users should be familiar with this--the basic idea is that as you type a search, your program finds words that match, rather than needing to click an "okay" button.

To do this, install the Incremental Search Input Manager (direct link to a universal binary), which is an amazing free input manager from Michael McCracken. Then make a DefaultKeyBinding.dict file that looks like this:
  {
      /* Ctrl-S and Ctrl-R for incremental search */

      "^s" = "ISIM_incrementalSearch:";
      "^r" = "ISIM_reverseIncrementalSearch:";
      "^$g" = "abort:";
  }
Now just restart TextEdit, open a big text document, type ⌃S, and start typing a search. You'll never do Command-F again!

Hint #4: Repeat your keystrokes as many times as you like

Type the following command into the terminal:
  defaults write -g NSRepeatCountBinding -string "^u"
Now start up TextEdit, and type ⌃U, followed by a number, maybe 44 or something, and then any letter key. Here's what I get if I type ⌃U then 44 then Option-G:
  ©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©©
That's a lot of copyright signs--and 44 could just as easily be 84 or 144. If you want, you can make any keyboard shortcut be your "repeat count binding;" it doesn't have to be ⌃U. That's the traditional emacs shortcut, so it's what I use.

An old related hint:

The hint Enabling meta-key Emacs shortcuts in Cocoa apps from 2001 talked about putting in some emacs key bindings. Needless to say, there's a lot more that the system can do than that!

I hope you enjoy this hint, and the full article!
    •    
  • Currently 3.30 / 5
  You rated: 2 / 5 (10 votes cast)
 
[46,917 views]  

Customize the Cocoa text binding system | 17 comments | Create New Account
Click here to return to the 'Customize the Cocoa text binding system' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Customize the Cocoa text binding system
Authored by: lagroue on Mar 21, '06 08:20:51AM

Thansk ! Very interesting article.

And you had me realize that Tiger brought back the Keyboard Desktop Accessory ! Gee !



[ Reply to This | # ]
Customize the Cocoa text binding system
Authored by: demmons65 on Mar 21, '06 08:44:59AM

Excellent list of shortcuts. I had no idea there were so many, thanks.

And the incremental search capability is something I hoped could be made available in other apps besides Firefox. Now I have it all over, thanks again!


---
d a v e

http://www.hostwerks.com/~dave/



[ Reply to This | # ]
Customize the Cocoa text binding system
Authored by: ra5ul on Mar 21, '06 08:53:43AM

how did you get M-u to work as a keyboard shortcut? any of the meta modifiers that generate diacritics keep failing here.



[ Reply to This | # ]
Customize the Cocoa text binding system
Authored by: hmelman on Mar 21, '06 09:50:21AM

I have the same problem. Using Option as Meta, M-c works but M-u does not.

Also the C-x bindings don't work either. C-x C-s would be wonderful. :)



[ Reply to This | # ]
Customize the Cocoa text binding system
Authored by: jacobolus on Mar 21, '06 10:55:36AM

darnit. Silly me. Apparently "saveDocument:" doesn't work, but "save:" does. I'll fix that. I thought I had tested it.



[ Reply to This | # ]
Customize the Cocoa text binding system
Authored by: hmelman on Mar 21, '06 11:56:16AM

Yes. save: and saveAs: both work. Neither openDocument: nor open: seem to work though.



[ Reply to This | # ]
Customize the Cocoa text binding system
Authored by: jacobolus on Mar 21, '06 11:00:26AM

You're right. The diacretics make it fail. If you want to use ~u as a binding, you'll probably have to make a new keyboard layout, look at my full article. It links to an application called Ukelele which can do this.

Apparently the diacretic is getting caught at a lower level than the text system, so its shortcut won't work as a binding.



[ Reply to This | # ]
Customize the Cocoa text binding system
Authored by: monecky on Mar 21, '06 09:39:29AM

Does anybody know if there has been any work to integrate vim commands? I've searched a couple times over the past year, and couldn't find anything. My productivity would skyrocket.



[ Reply to This | # ]
vim commands
Authored by: Lectrick on Mar 21, '06 10:45:37AM

I can't find any either, but I'm sure that if you did a little work to create the keybinding for it (even if it only covered the most popular vim commands), you would make a lot of geeks happy...

---
In /dev/null, no one can hear you scream



[ Reply to This | # ]
Customize the Cocoa text binding system
Authored by: jacobolus on Mar 21, '06 11:03:01AM

It's not very possible, because Vim is a modal editor, whereas NSTextView widgets are not. But if you want, you can try to approximate some vim bindings by using modifier keys and leaving it non-modal.

If you come up with some good vim approximations, let me know and I'll add them to my page.



[ Reply to This | # ]
Customize the Cocoa text binding system
Authored by: hbp4c on Mar 21, '06 03:07:35PM

Vi keybindings exist for the bash shell, a user enter inserts mode by default unless they hit esc to go into command mode for whatever reason.

I don't see why a keybinding couldn't be designed that emulated this functionality.



[ Reply to This | # ]
Customize the Cocoa text binding system
Authored by: ky11x on Mar 21, '06 08:39:06PM

Because Cocoa text fields are not modal. The original poster already told you why it is not possible to do this within the context of the Cocoa classes without doing more programming than mere configuration.



[ Reply to This | # ]
Customize the Cocoa text binding system
Authored by: mazatty on Mar 22, '06 10:29:57PM

while not a complete solution it is a working proof of concept, and still quite handy. i'm using it right now to write this message.

you can find viAllOver at http://www.dabble.org/viallover/

it works in most text fields of most cocoa apps, including text fields in html pages with Safari. also works in TextEdit, Mail, and SubEthaEdit to name a few.

i am the author and am still looking for help. enjoy.



[ Reply to This | # ]
Customize the Cocoa text binding system
Authored by: barefootguru on Mar 21, '06 11:13:14PM

Are there some defaults around to modify other aspects of the Cocoa text system? In particular I'd like to turn off the 'smart' selection when deleting a highlighted word--so the space _isn't_ deleted along with the word.



[ Reply to This | # ]
Customize the Cocoa text binding system
Authored by: jacobolus on Mar 22, '06 03:18:25AM

Hmm. that's a weird one. It seems to depend on how you select the text. If you double click it, it will delete the space. If you drag to select, it won't. One way to avoid deleting the space is to modify the selection after double-clicking the word, and then it won't take the space out. You could conceivably make a binding to deal with this, but I'm not sure if there is any other way to change the behavior on a systemwide basis.



[ Reply to This | # ]
Customize the Cocoa text binding system
Authored by: seb2 on Mar 22, '06 12:08:00AM

Haven't seen this here -- and just for completeness: the possible values for the key bindings can be found here:
/System/Library/Frameworks/AppKit.framework/Versions/C/Headers/NSResponder.h -- look under the section "Standard bindable commands".



[ Reply to This | # ]
Customize the Cocoa text binding system
Authored by: jacobolus on Mar 22, '06 03:07:26AM
Actually, NSResponder is just the tip of the iceberg. I should have put a link in the hint, but I discuss it in the longer article. Here's a list of selectors that can be used:

[ Reply to This | # ]