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?

3
  • 4
    Actually, I think you could replace the whole function with just the lambda string.
    – Dan
    Commented Jul 21, 2015 at 0: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
    Commented Jul 21, 2015 at 3:54
  • 2
    Possible duplicate of Why can't I bind my function to a key or call it with M-x?
    – Tyler
    Commented Oct 16, 2018 at 20:24

2 Answers 2

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:

0
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
                   ))

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.