I want certain keys and key combinations to behave as other keys or key combinations in Emacs. For example, I want F5 to behave as a substitute for C-c for every possible combination involving it, or C-S- as C-. Is it possible to do that without manually rebinding all such key combinations?
2 Answers
The keys you are referring to are known as 'prefix keys'. A prefix key has its own keymap, so to make another key behave the same, you need to assign it to the same keymap. For control-c, you use the mode-specific-map
:
(global-set-key (kbd "<f5>") mode-specific-map)
Control on its own isn't a prefix key, or really a key at all, since it doesn't send a keypress to Emacs[1] until you hit another key. I'm not sure how to remap C-S- to C- within Emacs. You could do it system-wide with xmodmap, but that's probably not what you want.
[1] the control key (and shift, alt) do send a keypress to the operating system, but Emacs doesn't 'see' this unless there's another key pressed at the same time

- 9,872
- 2
- 33
- 57
-
1+1 but... *"Control on its own isn't a prefix key, or really a key at all, since it doesn't send a keypress"*... Control definitely does send a keypress by itself. You can use, say, *xev* under Linux to see that. It sends both a keypress when you press it and a keyrelease when you release it. A *xev* output for CTRL on my system gives: *KeyPress eventKeyPress event... state 0x14, keycode 37 (keysym 0xffe3, Control_L)...* etc. I kinda dispute that you wrote that control isn't "really a key at all". My keyboard's CTRL and CAPS-LOCK remapped to act as CTRL do take offense ; ) – TacticalCoder May 04 '12 at 20:35
-
1@TacticalCoder: Fair point, as far as X is concerned. But I don't think the keypress registered by X is visible to Emacs on its own. There isn't anyway for Emacs to detect or respond to a modifier key if it isn't modifying another key. So from Emacs' perspective, CTRL on its own isn't a key until it is combined with another key. – Tyler May 04 '12 at 20:46
I prefer
(define-key key-translation-map [f5] (kbd "\C-c"))
Here is a good resource.
To summarize the link given above: The disadvantage of global-set-key is that, when you define a key combination to enter a symbol, it doesn't work in isearch.
key-translation-map also has a problem. Imagine you defined a symbol | to execute a command and C-| to enter the symbol |, pressing C-| will execute the command.

- 21,988
- 13
- 81
- 109

- 58,701
- 10
- 113
- 156
-
-
This doesn't work well with prefix commands. For instance, `C-x 8 m` generates the micro sign "µ". But if `key-translation-map` is used to remap `|` to `C-x`, the sequence `| 8 m` yields `C-x 8 m is undefined` instead of the micro sign. – vinc17 Aug 10 '23 at 23:33