You have to make C-M-w
a prefix key first (by default it it bound to append-next-kill
):
(setq c-m-w-keymap (make-sparse-keymap))
(define-key prog-mode-map (kbd "C-M-w") c-m-w-keymap)
Then you can add keybindings to it:
(define-key c-m-w-keymap (kbd "r") #'eglot-rename)
Then both prog-mode
and its children (e.g. c++-mode
) will know about it.
EDIT: ... and since the above hijacks an existing keybinding in order to allow the key to become a prefix key, you might want to add a binding in the new keymap to replace the old binding, e.g.:
(define-key c-m-w-keymap (kbd "a") #'append-next-kill)
Alternatively, as @phils suggests in a comment, you might choose an unused key, e.g. C-M-z
is not used in prog-mode-keymap
.
I tend to avoid such ad-hoc bindings, primarily because if I don't use them often enough I forget about their existence; instead I tend to define function keys[1] as prefix keys for clusters of related commands. I can remember only a few of those too, but I can add stickers on the rest to remind me for which cluster a particular function key is a prefix, and I can use something like <f5> C-h
to find what's hidden behind the <f5>
prefix key.
[1] You might want to only use <f5>
-<f12>
- Emacs defines the first four by default.