Home Link

Hello, my name is Brett Terpstra, and it’s nice to meet you. Elegant solutions to complex problems. Curious?

Quick Tip: repeat Cocoa text actions, Emacs/Vim style

Quick Tip: repeat Cocoa text actions, Emacs/Vim style Tagged:
Dec 04, 2011 (169 days ago) This post is part of a series: KeyBinding
New text navigation KeyBindings
April 22, 2012
A little KeyBinding sanity
December 21, 2011
Quick Tip: repeat Cocoa text actions, Emacs/Vim style
December 4, 2011
The keys that bind: KeyBinding Madness part 2
November 10, 2011
KeyBinding madness
August 13, 2011

Rainman pictureI just stumbled upon another keybinding tip from Jacob Rus1. It allows you to enter a numeric string before running a text command and repeat the command the specified number of times2. Emacs and Vim do it… turns out that any Cocoa text field can, too. The tip is from 2006, so this is old news, but still new to me.

It uses the NSRepeatCountBinding default in the Text System, which is defined exactly for this reason. You just have to give it a binding so you can let the system know you’re about to enter a number to apply to the command that comes after it. To enable this, you can use the defaults command to affect the global defaults:

defaults write -g NSRepeatCountBinding -string "^u"

In the example I’m using “^u” (Control-U) as the binding3. This setting will conflict with any other defaults and custom settings you have, so be sure to pick a combination that’s not already used on your system, or that you never do use4.

What can you do with it? Say you want to move the cursor down exactly 20 times. You would just type (assuming my shortcut above) ^u 20 and hit the down arrow. It can apply to any of the usual text features… type a letter to repeat that the specified number of times, or delete 5 words back with ^u 5 then opt-shift-delete (or ^w).

It also works with all of my custom keybindings, making them quite a bit more extensible. All of the cut, copy and motion keybindings work wonderfully, but my favorite is my list continuation keybinding. Now I can create an empty Markdown list of a specified length with any prefix using just a few keystrokes. My day has been made.

Try it out! Remember, you can change your shortcut at any time just by repeating the command above with a different shortcut, or use a blank string to clear it out (defaults write -g NSRepeatCountBinding -string "").


  1. His old posts have been a guiding light for me in the dark world of KeyBindings. And Lri, of course. 

  2. This is also a huge timesaver for people with OCD symptoms. Need to do everything exactly three times? This will make your life easier. 

  3. Jacob’s recommendation as well, and with Emacs heritage. 

  4. Control-U is an Emacs keybinding that actually is used in the text system to “kill” to beginning of line. I only use this regularly on the command line, though, having become accustomed to more Mac-like commands in Cocoa text fields. These don’t affect either Terminal or iTerm, so it works out for me. Choose wisely. 

One Response to “Quick Tip: repeat Cocoa text actions, Emacs/Vim style”

  1. Another really useful default mentioned in the developer.apple article:

    defaults write -g NSTextShowsControlCharacters -bool true
    

    It makes standard text views display ASCII control characters using caret notation.

Leave a Reply

Comments may use standard Markdown formatting


Notify me of followup comments via e-mail. You can also subscribe without commenting.

Entries (RSS) and Comments (RSS), or Subscribe by Email