macOS keybinding tricks: the repeat count binding
Another trick for readers interested in honing their keyboard-fu on macOS.
In Vim, most operations have can have a count specified in the keystrokes for the command, e.g. 3dd
to delete 3 lines. You can do similar in any Cocoa text field (all Apple apps, plus most native apps). You just need to specify a keyboard shortcut to use before the count. Then you can hit that keyboard shortcut, type a number (X) and then hit a key or key combination to have that event repeated X times.
To set the repeat shortcut:
defaults write -g NSRepeatCountBinding -string "~r"
The above would set the repeat binding to ⌥R. Use the following shortcuts when defining the operator, being sure not to overwrite a keyboard shortcut you use elsewhere (control-option combinations are pretty safe):
Symbol | Modifier |
---|---|
~ | Option |
@ | Command |
^ | Control |
$ | Shift |
You can use any combination of symbols when defining your shortcut. I like ⌥R (~r
) because it has no conflicts that I know of and is easily reachable with my left hand. Also there’s a mnemonic quality with “r” for “repeat.” Choose whatever you like1.
Once the above defaults
command is run, relaunch whatever app you want to test it in. It will work in TextEdit, nvUltra, Bear, Pages… most apps that you would use on your Mac. But a restart of the app will be required for the new bindings to be recognized.
Now, type your selected shortcut (⌥R if you didn’t change it), then type a number (any number of digits), then type “x”. If you typed <SHORTCUT>10x
, you should get xxxxxxxxxx
. This works with both character insertions and movement keys and text operations. For example, if you type <SHORTCUT>5
and then hit ⌥⌫, it would delete backwards 5 words. Each step of the repeat process is stored in the undo buffer, so undoing it would require five ⌘Zs. This is actually a feature, because if you’re estimating how many words back to delete but guess wrong, you can undo a couple to restore them individually.
One niche example use for this is when you want to insert a redacted password or API key when you’re writing. You know the length of the password or key should be 20 characters, so you just type ⌥R20x
and get a string of x’s of the appropriate length. Or if you want to select the previous 3 words in the current line, just type ⌥R3
⌥⇧←. Much like Vim, it takes a second of thought to figure out how many times you want to repeat an action, and in some cases it’s faster to just hit the key that number of times. But give it a go and make a conscious effort to use it for a while, I bet it will stick.
Weirdly this trick doesn’t work for ⌘V, which is disappointing because one very common use I could see for it would be to copy something and then paste it X number of times. I can’t explain why that doesn’t work.
Hope that leads to some fun/increased productivity for folks willing to try it. If you have any favorite keyboard tricks, please feel free to share in the comments!
-
As noted by Dr. Drang, Option-R is the shortcut for the ® symbol, so if you use that, you’ll obviously want to pick a different shortcut. I enter all my special characters using LaunchBar’s emoji/symbol picker, so I didn’t consider that. I’d recommend Control-Option-R as a safe alternative. ↩