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?
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:
(elisp) Defining Commands
RET
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
))
"λ"
is a valid keyboard macro for inserting a lambda character, so(global-set-key (kbd "C-x 8 l") "λ")
will work fine.