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

北城以北 提交于 2019-11-26 15:36:49

问题


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.


回答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.




回答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.




回答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!)



来源:https://stackoverflow.com/questions/1250846/wrong-type-argument-commandp-error-when-binding-a-lambda-to-a-key

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!