I just stumbled upon another keybinding tip from Jacob Rus. It allows you to enter a numeric string before running a text command and repeat the command the specified number of times. 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 binding. 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 use.
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 ""
).