5

When I try to do this:

(global-set-key (kbd "C-x 8 l") (lambda () (insert "λ")))

It just doesn't work:

Wrong type argument: commandp (lambda nil (insert "λ"))

Does anyone know a decent way to do this?

Czipperz
  • 317
  • 2
  • 9
  • 4
    Actually, I think you could replace the whole function with just the lambda string. – Dan Jul 21 '15 at 00:42
  • 2
    Dan is right. Keyboard macros are *commands* too, and `"λ"` is a valid keyboard macro for inserting a lambda character, so `(global-set-key (kbd "C-x 8 l") "λ")` will work fine. – phils Jul 21 '15 at 03:54
  • 2
    Possible duplicate of [Why can't I bind my function to a key or call it with M-x?](https://emacs.stackexchange.com/questions/45401/why-cant-i-bind-my-function-to-a-key-or-call-it-with-m-x) – Tyler Oct 16 '18 at 20:24

2 Answers2

10

Keys can only be bound to commands.

A command is (usually) a function which contains an interactive form. This form must be included even if your function takes no arguments.

Hence:

(global-set-key (kbd "C-x 8 l") (lambda () (interactive) (insert "λ")))

In addition, keyboard macros are also commands, and can therefore similarly be bound to keys.

"λ" is a valid keyboard macro for inserting a lambda character, and so (as per Dan's comment above) the following also works:

(global-set-key (kbd "C-x 8 l") "λ")

See also:

phils
  • 48,657
  • 3
  • 76
  • 115
4

You can also easily expand the C-x 8 bindings in this fashion:

(require 'iso-transl)
;; Add custom bindings to "C-x 8" map
(dolist (binding '(("l" . [?λ]) ; greek small letter lambda
                   ))
  (define-key iso-transl-ctl-x-8-map (kbd (car binding)) (cdr binding)))

If you need to add more bindings, you simply add them to that alist.


Here is how I use it:

(require 'iso-transl)
;; Add custom bindings to "C-x 8" map
(dolist (binding '(;; >
                   (">"       . nil) ; First unbind ">" from the map
                   (">="      . [?≥]) ; greater than or equal to
                   (">>"      . [?≫]) ; much greater than
                   (">\""     . [?»]) ; right-pointing double angle quotation mark
                   (">'"      . [?›]) ; single right-pointing angle quotation mark
                   (">h"      . [?☛]) ; black right pointing index
                   ;; <
                   ("<"       . nil) ; First unbind "<" from the map
                   ("<="      . [?≤]) ; less than or equal to
                   ("<<"      . [?≪]) ; much less than
                   ("<\""     . [?«]) ; left-pointing double angle quotation mark
                   ("<'"      . [?‹]) ; single left-pointing angle quotation mark
                   ("<h"      . [?☚]) ; black left pointing index
                   ;; "
                   ("\"`"     . [?“]) ; left double quotation mark
                   ("\"'"     . [?”]) ; right double quotation mark
                   ;; arrows
                   ("<right>" . [?→]) ; rightwards arrow
                   ("<left>"  . [?←]) ; leftwards arrow
                   ("<up>"    . [?↑]) ; upwards arrow
                   ("<down>"  . [?↓]) ; downwards arrow
                   ;; misc
                   ("r"       . [?▯]) ; white vertical rectangle
                   ("R"       . [?▮]) ; black vertical rectangle
                   ("*r"      . [?₹]) ; indian rupee sign
                   ("1/3"     . [?⅓]) ; fraction one third
                   ("0"       . [?​]) ; zero width space
                   ))
Kaushal Modi
  • 25,203
  • 3
  • 74
  • 179