1

key-translation-map allows you to globally translate keys.

For example:

(define-key key-translation-map (kbd "<f11>") (kbd "<f12>"))

But what if I want to restrict the remapping to a particular major mode?

In my case, I keep typing ` instead of ~ in org-mode and I would like to remap it so I can make my mistake without noticing. Is this possible?

2

I don't recommend it, but you can do what you've asked for like so:

(add-hook 'org-mode-hook 'my-org-mode-hook)

(defun my-org-mode-hook ()
  "Custom `org-mode' behaviours."
  ;; Buffer-local key translation from "`" to "~".
  (let ((keymap (make-sparse-keymap)))
    (set-keymap-parent keymap key-translation-map)
    (setq-local key-translation-map keymap)
    (define-key key-translation-map (kbd "`") (kbd "~"))))

I think what you actually want to do is bind ` to insert a ~ character, which covers your requirement without the same side-effects. e.g.:

(with-eval-after-load "org"
  (define-key org-mode-map (kbd "`") (kbd "~")))

The latter will still allow you to type (for instance) C-c`, while the former will not.

  • You've grasped my intention accurately. The second solution works swimmingly, thank you. – Matthew Piziak Mar 6 '18 at 3:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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