2

I would like to bind #'window-toggle-side-windows to the key combination shift and escape. That is hammered into my muscle memory by decades of using Intellij.

The obvious (global-set-key "s-ESC" #'window-toggle-side-windows) does not work as expected. It fails recognize the key combination of shift escape.

I am mainly interested in having this work in GUI emacs.

Edit: (global-set-key "S-<escape>" #'window-toggle-side-windows) doesn't work either.

1 Answer 1

4

When defining keybindings in Emacs, it is generally easiest to lookup their 'string representations' pressing C-h k followed by pressing the key chords you would like to bind.1 You will see the 'string representation' in the beginning of the help window. Subsequently, you can simply wrap it with kbd and use it in define-key or global-set-key etc. as follows:

(global-set-key (kbd "S-<escape>") #'window-toggle-side-windows)

For some reason, even when knowing about this, this information is still not straightforward to find on the web or within the Emacs documentation. Therefore, I am repeating this answer here.

From Emacs version >= 29, one can use keymap-set in which wrapping the string representation with kbd is not required anymore.

1. As ESC is a prefix key by default, using C-h k does not work directly here. In that case you could try pressing M-: (read-key "Press key (chord): ") followed by the key (chord), to obtain the string representation.

4
  • 1
    Don't you need to get rid of the S-<escape> --> ESC translation somehow? If I do C-h k S-ESC, I get S-<escape> flashing by in the echo area and quickly replaced by ESC- which is a prefix key waiting for me to type something.
    – NickD
    Commented Feb 11 at 17:34
  • Interesting. I did not notice, as I just started setting the keybinding (after emacs -q). Then I quickly checked what S-ESC returns, and of course already it was not a prefix key anymore. Anyway, the (main) instruction for setting the keybinding should work correctly, but the instruction about using C-h k needs some extra info, however, I'm not about what would be the best advice here. Commented Feb 11 at 19:13
  • Ah, if it doesn't cause trouble in the rebinding, then I think you should just point out the translation possibility (basically just to prevent any confusion like mine) and state that the rebinding works regardless. I was a bit worried that ESC might not be a prefix key any longer: I should have tried it out.
    – NickD
    Commented Feb 11 at 20:31
  • 1
    Tested it and it works fine (but you mean M-: (read-key ...) - right?)
    – NickD
    Commented Feb 12 at 3:12

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.