Quick Tip: repeat Cocoa text actions, Emacs/Vim style
I 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 ""
).
-
His old posts have been a guiding light for me in the dark world of KeyBindings. And Lri, of course. ↩
-
This is also a huge timesaver for people with OCD symptoms. Need to do everything exactly three times? This will make your life easier. ↩
-
Jacob’s recommendation as well, and with Emacs heritage. ↩
-
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. ↩