"Wrong type argument: commandp" error when binding a lambda to a key

29,040

Solution 1

global-set-key expects an interactive command. (lambda () (interactive) (forward-line 5)) ought to work.

By the way, C-h f commandp is a pretty good starting point for errors like that.

Solution 2

The correct form should be this -

(global-set-key (kbd "M-n") (lambda () (interactive) (forward-line 5)))

The problem was that you forgot to put (interactive) (as brendan mentioned).

By the way, you will notice that I used the (kbd) function for specifying the key-binding. That function is immensely useful since you can put the key-bindings almost literally.

Solution 3

I've also seen this error on a new machine where I am using my usual .emacs file but haven't installed my packages, and the command to be executed is in one of those packages. (Because a command that can't be executed definitely isn't interactive!)

Share:
29,040
Paul Nathan
Author by

Paul Nathan

Software engineer/craftman/programmer Passion for quality and correctness as exemplified in the continuous improvement/kaizen model. Obsessive about tools and infrastructure Focused on working in Scala/Rust/Lisp/Haskell/OCaml Articulate Lisp - a Common Lisp environment tutorial site. Learn Common Lisp today!

Updated on June 19, 2020

Comments

  • Paul Nathan
    Paul Nathan about 4 years

    I am getting a "Wrong type argument: commandp, (lambda nil (forward-line 5))" here.

    (global-set-key [?\M-n] (lambda () (forward-line 5)))
    

    What is the error? I'm fairly sure it's simple & I'm missing something obvious.

  • Dangelov
    Dangelov over 5 years
    I think there are no down sides. From the documentation: >> The "call" to ‘interactive’ is actually a declaration rather than a function; it tells ‘call-interactively’ how to read arguments to pass to the function. When actually called, ‘interactive’ just returns nil.
  • peterhil
    peterhil almost 4 years
    So according to (commandp) documentation, lambda functions with top level call to (interactive) work, but how can I define a function with (defun) that would work?