Skip to content

Instantly share code, notes, and snippets.

@lispstudent
Forked from mmarshall540/translate-modifiers.el
Created February 24, 2022 15:52
Show Gist options
  • Save lispstudent/35250b6cb5975558f0a53da9f1263fdd to your computer and use it in GitHub Desktop.
Save lispstudent/35250b6cb5975558f0a53da9f1263fdd 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 ;;'(?\t ?\r ?\s ?\d ?\b)
'(up down left right menu print scroll pause
insert delete home end prior next
tab return space backspace
f1 f2 f3 f4 f5 f6 f7 f8 f9 f10 f11 f12)
;; <tab> does `C-i'.
;; <return> does `C-m'.
;; <escape> does `C-\['.
;; We want those keys to work, so we remove their translations from the list.
(remq ?i (remq ?m (remq ?\[ (number-sequence 33 126))))))
(define-key key-translation-map (my/make-key-string 'control char) (my/make-key-string 'meta char))
(define-key key-translation-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