Skip to content

Instantly share code, notes, and snippets.

@mmarshall540
Last active November 23, 2022 01:50
Show Gist options
  • Save mmarshall540/8db9b5bac8dc5670cb9323e387de1317 to your computer and use it in GitHub Desktop.
Save mmarshall540/8db9b5bac8dc5670cb9323e387de1317 to your computer and use it in GitHub Desktop.
Translating modifier keys in Emacs
;; There seems to be no built-in mechanism to swap modifier keys in
;; Emacs, but it can be accomplished (for the most part) by
;; translating a near-exhaustive list of modifiable keys. In the case
;; of 'control and 'meta, some keys must be omitted to avoid errors or
;; other undesired effects.
(defun my/make-key-string (modsymbol basic-event)
"Convert the combination of MODSYMBOL and BASIC-EVENT.
BASIC-EVENT can be a character or a function-key symbol. The
return value can be used with `define-key'."
(vector (event-convert-list `(,modsymbol ,basic-event))))
;; Escaped chars are:
;; tab return space del backspace (typically translated to del)
(dolist (char (append '(up down left right menu print scroll pause
insert delete home end prior next
tab return space backspace escape
f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11 f12)
;; Escape gets translated to `C-\[' in `local-function-key-map'
;; We want that to keep working, so we don't swap `C-\[' with `M-\['.
(remq ?\[ (number-sequence 33 126))))
;; Changing this to use `input-decode-map', as it works for more keys.
(define-key input-decode-map (my/make-key-string 'control char) (my/make-key-string 'meta char))
(define-key input-decode-map (my/make-key-string 'meta char) (my/make-key-string 'control char)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment