0

In all my exploration, it seems that M-[0-9] and C-[0-9] apply numeric prefixes in the same way everywhere. Does this change for any common modes? I'm considering overwriting one of the sets of bindings (because they're accessible bindings), but I'm always so tentative to step on the toes of the default bindings. I'm reaching out to this wonderful community to see if you know of any areas where I'll regret this overwrite.

I include the major-mode and minor-mode tags because I'm wary of how these bindings will be altered by popular modes and packages I may not have yet explored. It's easy enough to see that they work similarly in the starting buffer, but I'm curious about the rest of the popular parts of the ecosystem.

I'm particularly focused on these bindings because they are very quick with my keyboard. I'm a QMK user, and my keyboard layout makes M-[0-9] much faster than dealing with even a single prefix.

3
  • In general, you should be wary of changing these things: they make your emacs incompatible with just about everybody else's emacs (which might or might not make a difference to you). There is a whole class of keybindings that are specifically reserved for users (C-c <letter> and function keys <F5>-<F9> without modifiers - see Key Binding Conventions). Defining some to be prefix keys vastly increases the number of keybindings available at the cost of one extra keystroke.
    – NickD
    Oct 2, 2022 at 16:59
  • The question is unclear: "I'm considering overwriting one of the sets of bindings". Overriding in the global map or in some local (major-mode) map, or in some minor-mode map, or on some text (text-property keymap)?
    – Drew
    Oct 2, 2022 at 17:38
  • Those keys are essentially the same in the global-map. They might not be the same in other keymaps, which override the global-map in various situations. Are you asking only about overriding keys in the global-map? You included tags major-mode and minor-mode - why? Please consider clarifying your question.
    – Drew
    Oct 2, 2022 at 17:39

2 Answers 2

3

When I do C-h k M-1, I get

M-1 runs the command digit-argument (found in global-map), which is an interactive byte-compiled Lisp function in simple.el.

It is bound to

C-9, C-8, C-7, C-6, C-5, C-4, C-3, C-2, C-1, C-0, ESC 0..9,

C-M-9, C-M-8, C-M-7, C-M-6, C-M-5, C-M-4, C-M-3, C-M-2, C-M-1and C-M-0.

(digit-argument ARG)

Part of the numeric argument for the next command. C-u following digits or minus sign ends the argument.


When I do C-h w digit-argument, you get

digit-argument is on

C-9, C-8, C-7, C-6, C-5, C-4, C-3, C-2, C-1, C-0,ESC 0..9,C-M-9, C-M-8, C-M-7, C-M-6, C-M-5, C-M-4, C-M-3, C-M-2, C-M-1, C-M-0


Whenever there is a multiple key binding for the same command, it is usually to accommodate emacs -nw (terminal emacs) users.

Try running C-h k C-1 etc, C-h k M-1 etc, and C-h k C-M-1 etc on a emacs -nw and see what you get.

The C-1...C-9, M-1...M-9, C-M-1...C-M-9 are convenient ways to get single decimal digits.

And you can always input digit arguments using C-u 123 etc.. So yes, you can override the above keys.

I do have plenty of key bindings on the numeric keys.


For the sake of completion, this is what C-h k C-u shows

C-u runs the command universal-argument (found in global-map), which is an interactive byte-compiled Lisp function in simple.el.

It is bound to C-u.

(universal-argument)

Begin a numeric argument for the following command.

Digits or minus sign following C-u make up the numeric argument.

C-u following the digits or minus sign ends the argument.

C-u without digits or minus sign provides 4 as argument.

Repeating C-u without digits or minus sign multiplies the argument by 4 each time.

For some commands, just C-u by itself serves as a flag that is different in effect from any particular numeric argument.

These commands include C-SPC and M-x start-kbd-macro.


Following the leads in

Here is an "attempt" to get more BONUS keys ... The snippet is works on GUI Emacs.

(cl-loop for (x . fmt-string) in
         `(
           ;; ????: The `C-xxx` keys are problematic to generate in
           ;; terminal.  If you type it, `Emacs` may report a
           ;; different thing.  The behaviour varies with the
           ;; terminal.
           ("C" . "C-%s")
           ("M" . "M-%s")
           ;; ????: The previous comment applies
           ("C-M" . "C-M-%s")
           ,@(when (display-graphic-p)
               '(("ESCAPE" . "<escape> %s"))))
         do (cl-loop for digit in (number-sequence 0 9)
                     for key = (format fmt-string digit)
                     for sym = (intern (format "BLAH-%s-%s" x digit))
                     do (condition-case err
                            (progn
                              (message "%s -> %s" key sym)
                              (define-key input-decode-map (kbd key) (vector sym)))
                          (error (message "Error %S" err)))))

;; Bind `C-1' to `find-file'
(global-set-key (kbd "<BLAH-C-1> f") 'find-file)

(add-hook 'emacs-lisp-mode-hook 'outline-minor-mode)

;; Bind `C-0' as `outline-minor-mode-prefix'
(custom-set-variables
 ;; Key zero look like `o` (for outline)
 '(outline-minor-mode-prefix (kbd "<BLAH-C-0>")))

;; Use `C-0 C-0' to to enable `outline-minor-mode'
(global-set-key (kbd "<BLAH-C-0> <BLAH-C-0>") 'outline-minor-mode)

(add-hook 'hs-minor-mode-hook
          (defun my-hs-minor-mode-hook ()
            ;; Overwrite `hs-minor-mode-map'. `C-c @' is difficult to
            ;; type.
            (setq hs-minor-mode-map
                  (let ((map (make-sparse-keymap)))
                    ;; These bindings roughly imitate those used by Outline mode.
                    (define-key map (kbd "C-S-b") 'hs-show-block)
                    (define-key map (kbd "C-b") 'hs-hide-block)
                    (define-key map (kbd "C-a") 'hs-hide-all)
                    (define-key map (kbd "C-S-a") 'hs-show-all)
                    (define-key map (kbd "C-l") 'hs-hide-level)
                    (define-key map (kbd "C-c") 'hs-toggle-hiding)
                    (define-key map (kbd "<BLAH-C-2>") 'hs-minor-mode)
                    map))
            ;; Put `hs-' commands on `C-2' prefix;
            ;; 2 and `@' are on the same key on the keyboard.
            (fset 'hs-minor-mode-map hs-minor-mode-map)
            (global-set-key (kbd "<BLAH-C-2>") 'hs-minor-mode-map)))

;; Use `C-2 C-2' to toggle `hs-minor-mode'
(global-set-key (kbd "<BLAH-C-2> <BLAH-C-2>") 'hs-minor-mode)


Apropos the remark "some C-xxx keys are problematic to generate in terminal" in the snippet above,

  • On gnome-terminal, with emacs -Q -nw, when I do C-h k C-1, Emacs says

    1 runs the command self-insert-command (found in global-map)
    
  • On xterm, with emacs -Q when I do C-h k C-2, Emacs says

    C-@ runs the command set-mark-command (found in global-map)
    

Similar observations can be made for "some"(?) C-M-xxx bindings. C-M-xxx is essentially M- and C-xxx and M- is emitted with ESC and if you have issues with C-xxx it shouldn't be surprising that there are issues with C-M-xxx.

Comprehensive keyboard handling in terminals is a good introduction to terminals, and with a quick glimpse one can reasonably anticipate which key sequences are going to give you problematic on the terminal Emacs.

I understand none of what I say, so take what I say with a grain of salt. But the behavioural observations I make can be independently verified. I am on Debian / sid, btw.

5
  • I use -nw, actually (so it was very easy to test your suggestion). The outcome of C-[0-9] and M-[0-9] is the same on -nw. Thanks. Oct 2, 2022 at 10:04
  • With emacs -Q -nw, on gnome-terminal when I do C-h k C-1, I actually see that Emacs is receiving 1 runs the command self-insert-command (found in global-map), and on xterm when I do C-h k C-2, Emacs is reporting C-@ runs the command set-mark-command (found in global-map). I am VERY SURPRISED that you are able to actually enter those key sequences, and Emacs is reporting the key sequences you typed (instead of picking its own). I expect you to see surprises.
    – user31220
    Oct 2, 2022 at 10:30
  • 1
    Btw, Comprehensive keyboard handling in terminals is a good introduction to terminals, and with a quick glimpse at that you can reasonably anticipate which key sequences are going to give you surprises.
    – user31220
    Oct 2, 2022 at 10:33
  • I have made some more snippets ... since we last talked.
    – user31220
    Oct 2, 2022 at 13:48
  • My config is definitely not "emacs -q -nw". I use the term-keys package and a fleshed-out Alacritty config. That being said, I'm able to basically replicate the GUI behavior with the terminal. Thanks for pointing out the bonus keys. I hadn't even really though about taking advantage of C-i, C-m, etc., but they really are prime real estate (and I think I can get around the terminal limitations with my configuration). Oct 2, 2022 at 23:16
1

Without knowing the bindings myself, I'd say go ahead and adjust the bindings to your liking. Even if you're overwriting some default binding, if you never use it, why not adjust it a way that's most convenient for you? If you plan on sharing your changes with the public, that's another story of course. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.