0

I already tried some different ways of doing this, but never got it quite right, so I put it here.

When working in buffers using some specific modes (lisp-mode, emacs-lisp-mode) I want to make it so a specific key inserts a different character.

In my case,

If Elisp / Lisp mode activated:

  • pressing key ö inserts character (
  • pressing key ä inserts character )

How can I do this?

1 Answer 1

2

This should work:

(define-key
  emacs-lisp-mode-map
  "ö"
  (lambda () (interactive) (insert "(")))
(define-key
  emacs-lisp-mode-map
  "ä"
  (lambda () (interactive) (insert ")")))

Most major modes have an associated *-map variable, which holds the keyboard bindings for that mode. You can use define-key to add bindings to those maps.

3
  • 1
    Lambda expressions are self evaluating: you do not need to quote them.
    – NickD
    Commented Dec 20, 2022 at 12:39
  • TIL. I've updated the answer, thanks!
    – saintamh
    Commented Dec 20, 2022 at 12:43
  • Thank you SO much!
    – Jborg
    Commented Dec 20, 2022 at 14:17

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.