Use your digits and a personal key map for super shortcuts

Lots of commands in Emacs behave differently if called with a prefix argument C-u. For example, by default C-s runs the command isearch which searches for text as you type. If you use a prefix argument C-u C-s then isearch searches with regular expressions instead of literal string text.

You can also pass numerical arguments to commands, which for many simple commands will cause the command to repeat that number of times, so for example C-u 3 C-p passes the argument 3 to the command C-p which then runs previous-line three times. What is more, you can also pass these numerical arguments with the form C-3 C-p, where C-3 is equivalent to C-u 3. Of course this works for any digit 0-9, not just 3!

By default, Emacs uses the combinations C-3, M-3, C-M-3 all for this purpose, again for all digits 0-9. The thing is, I simply never use them in this way, and this is a huge waste of really useful keyboard shortcuts you could be using for other things.

To reclaim them, add the following to your emacs config file, with credit to this reddit post:

;; unset C- and M- digit keys
(dotimes (n 10)
  (global-unset-key (kbd (format "C-%d" n)))
  (global-unset-key (kbd (format "M-%d" n)))

You can then still access the digit prefixes with C-M-3 and similar but you now have all the C- and M- digit combinations to play with.

One great way to make use of this is to set up your own key map using one of these newly freed-up combinations. I set C-1 as the prefix key for my own map:

;; set up my own map
(define-prefix-command 'bjm-map)
(global-set-key (kbd "C-1") 'bjm-map)

Then I assign some keys to that map, for example

(define-key bjm-map (kbd "m") 'mu4e)
(define-key bjm-map (kbd "g") 'bjm/open-gcal-agenda)

Now I can use C-1 m to open mu4e for my email, and C-1 g to view my google calendar.

  • Stephen Eglen

    Don’t forget the standard key binding C-x m

    together with:

    (setq mail-user-agent ‘mu4e-user-agent)

    which satisfies a lot of other users.

  • glucas

    There is some rationale for all the digit prefix bindings, I think. It allows you to prefix any key sequence without having to change modifiers. That is, you can use `M-3 M-f` to jump forward three words and `C-3 C-f` to jump forward three characters. But I agree, it does take up a lot of key binding possibilities!