3

Most of the convenient OS X keybindings that GNU Emacs defines, such as s-a for C-x h, are not all that useful to me. I'd like to make Super synonymous with Ctrl. Is there a way to easily map all key chords involving Super to the equivalent Ctrl version? Failing that, is there a way to map a key to another key that works for prefix keys as well as non-prefix keys?

I've tried using keyboard-translate and global-set-key so far.

(keyboard-translate (kbd "s-a") (kbd "C-a")) results in a type error. I'm not sure why.

(global-set-key (kbd "s-a") (kbd "C-a")) works fine, but (global-set-key (kbd "s-h") (kbd "C-h")) does not seem to do much of anything.

3
  • 2
    If you are using the GUI version of Emacs instead of the terminal, then perhaps this answer will help: stackoverflow.com/a/34933409/2112489 You can modify it as you see fit. For any variable listed, just type M-x describe-variable or C-h v to learn more about the available options.
    – lawlist
    Nov 28, 2016 at 7:18
  • That does solve the immediate problem, but I'm a little curious why keyboard-translate didn't work as intended and why using global-set-key and the like works differently for prefix keys. It might be more appropriate to structure those as separate questions or hunt around more, I'm not sure. Nov 28, 2016 at 7:24
  • 1
    Another forum participant will need to help with keyboard-translate as I've never used it. I have set up my own keyboard shortcuts and disabled those that I didn't find useful, so I have no need to remap anything. Sorry that I couldn't be of more assistance.
    – lawlist
    Nov 28, 2016 at 7:27

2 Answers 2

1

So I think:

(keyboard-translate (kbd "s-a") (kbd "C-a"))

results in a type error because keyboard-translate takes character parameters, but kbd returns an "internal Emacs key representation", something very different than a character.

Also I don't think your use of global-set-key is doing what you think it's doing: when you say (global-set-key (kbd "s-a") (kbd "C-a")), you are actually binding the key s-a to the output of the command (kbd "C-a"). Apparently (surprisingly, to me) that happens to do what you meant for a single-key command, but it makes sense to me that it wouldn't work at all for prefix-map keybindings.

As lawlist said in the link in their comment, setting ns-command-modifier is the way to do what you want here.

2
  • I still don't follow why (kbd "C-a") doesn't give do what I want in the case of prefix keys. I thought that kbd just maps from a human-readable string notation for sequence of keys to an internal representation that makes sense to emacs. I guess the fundamental question here is why mapping a key to a key works in some cases but not others. I still don't have a good intuition there. I've been using the ns-command-modifier workaround for the past few months and haven't had any problems with it. Feb 7, 2017 at 4:30
  • OK, so for prefix commands, e.g. C-h is bound to a special value that says it can be used as a prefix. When you (global-set-key (kbd "s-h") (kbd "C-h")), you aren't binding S-h as a prefix key, you are binding it to the output of (kbd "C-h"), which is just a string. So striking S-h will invoke a string, which doesn't do anything. Does that make sense?
    – Willy Lee
    Feb 7, 2017 at 5:28
1

Adding another option in case someone is looking for different solutions.

If the Super => Control mapping is only desired in GUI Emacs (on macOS), one can use Karabiner Elements (FOSS) with the appropriate "complex modification." These are the steps:

  1. Put this in a file, like super_to_control_on_emacs.json:

    { "title": "Super to Ctrl on Emacs",
      "rules": [
        { "description": "Turn Command (Super) into Ctrl on Emacs",
          "manipulators": [
            { "type": "basic",
              "from": { "key_code": "left_command",
                        "modifiers": { "optional": ["any"] } },
              "to": [ { "key_code": "left_control" } ],
              "conditions": [ { "type": "frontmost_application_if",
                                "bundle_identifiers": ["^org\\.gnu\\.Emacs$"] } ] },
            { "type": "basic",
              "from": { "key_code": "right_command",
                        "modifiers": { "optional": ["any"] }},
              "to": [ { "key_code": "right_control" } ],
              "conditions": [ { "type": "frontmost_application_if",
                                "bundle_identifiers": [ "^org\\.gnu\\.Emacs$" ] } ] } ] } ] }
    
  2. Copy it to ~/.config/karabiner/assets/complex_modifications/

  3. Open Karabiner Elements > Complex Modifications > Add Rule > Turn Command (Super) into Ctrl on Emacs > Enable. That's it!

This template can be used for other Emacs-specific modifications as well.

Your Answer

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

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