I'm reading about some concepts of Emacs Lisp. The lambda's seems interesting to me. After reading the manuals about lambda's in Emacs Lisp, I decided to play with it.

For example, I have this snippet:

(defun my-insert-arrow ()
    (insert "->")) 

(evil-define-key 'insert php-mode map (kbd "C-<next>") 'my-insert-arrow))

Okay, then I replace that function with a lambda:

(evil-define-key 'insert php-mode map (kbd "C-<next>") (lambda () (insert "->")))

But I got the following error:

command-execute: Wrong type argument: commandp, (lambda nil (insert "->"))

So it seems I applied the lambda in the wrong way. But I found no difference with the manuals, except that the lambda's in the tutorial examples contains arguments.

  • 1
    that lambda needs to be interactively-callable command (i.e., interactive function), you need (interactive) for it, even you don't care about argument.
    – xuchunyang
    Aug 30, 2015 at 20:58
  • 1
    ReneFroger: even though you can do this; it's not a good idea. stackoverflow.com/a/27264339/324105
    – phils
    Aug 30, 2015 at 21:08
  • 1
    interactive tells Emacs how to call the function interactively. without it emacs does not know how to pass the arguments in an interactive call. your corner case (no arguments) falls well into the general pattern.
    – sds
    Aug 30, 2015 at 21:25
  • 3
    ReneFroger: The lambda form needs an interactive declaration for exactly the same reason that you had put one into the original defun. It has nothing to do with using lambda to define the function, and everything to do with the fact that you're binding it to a key. Only commands may be bound to keys, and functions are not commands unless they have an interactive declaration.
    – phils
    Aug 30, 2015 at 21:35
  • 3
    If it helps demystify things, note that defun itself is defined in terms of lambda. All functions are ultimately defined by a lambda form. The primary purpose of defun is to alias that lambda form to a symbol name. So any behaviour which applies to lambda functions also necessarily applies to defun functions, and it makes no sense to suggest that the former should have special behaviour for key bindings which doesn't also apply to the latter. There needs to be a way to differentiate command functions from non-command functions, interactive is it, and it applies to all functions.
    – phils
    Aug 30, 2015 at 21:59


Browse other questions tagged or ask your own question.