Why can't I bind my function to a key or call it with M-x?
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I wrote a function, and I want to call it via M-x, and bind it to a key. This is my function:
(defun my-function ()
(message "This is a great function"))
If I try to call it with M-x my-function
, I get the error: [no match]
in the mini-buffer.
If I try to bind it to a key:
(global-set-key (kbd "C-c a") 'my-function)
It appears to work, but when I try to call it with C-c a
, I get the error
Wrong type argument: commandp, my-function
Why can't I use my function?
functions commands interactive
add a comment |Â
up vote
1
down vote
favorite
I wrote a function, and I want to call it via M-x, and bind it to a key. This is my function:
(defun my-function ()
(message "This is a great function"))
If I try to call it with M-x my-function
, I get the error: [no match]
in the mini-buffer.
If I try to bind it to a key:
(global-set-key (kbd "C-c a") 'my-function)
It appears to work, but when I try to call it with C-c a
, I get the error
Wrong type argument: commandp, my-function
Why can't I use my function?
functions commands interactive
I volunteer this question as a generic response to frequently asked questions on this subject. Feel free to expand or clarify however makes sense to make the question and answer discoverable by & useful to people with similar problems!
â Tyler
1 hour ago
Thanks for doing this, Tyler. I flagged the Q for a moderator to please convert this to a community question.
â Drew
12 mins ago
One thing I wonder about is whether the title should just quote the error message. That might be easier for people to find, and it might allow for answers that do not have to do with just addinginteractive
- e.g., sometimes a command disappears from a new version of a library. The error can be raised in any context where Emacs expects a command.
â Drew
10 mins ago
It would be good if folks now searched the wiki for, say,commandp
, to try to find other Qs that can be closed as duplicates of this one. Be careful to read the Q&A, though, as some are different. In some cases, the answer (and the Q context) might be worth repeating here. In other cases the question is unrelated and should be left as is (not closed).
â Drew
7 mins ago
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I wrote a function, and I want to call it via M-x, and bind it to a key. This is my function:
(defun my-function ()
(message "This is a great function"))
If I try to call it with M-x my-function
, I get the error: [no match]
in the mini-buffer.
If I try to bind it to a key:
(global-set-key (kbd "C-c a") 'my-function)
It appears to work, but when I try to call it with C-c a
, I get the error
Wrong type argument: commandp, my-function
Why can't I use my function?
functions commands interactive
I wrote a function, and I want to call it via M-x, and bind it to a key. This is my function:
(defun my-function ()
(message "This is a great function"))
If I try to call it with M-x my-function
, I get the error: [no match]
in the mini-buffer.
If I try to bind it to a key:
(global-set-key (kbd "C-c a") 'my-function)
It appears to work, but when I try to call it with C-c a
, I get the error
Wrong type argument: commandp, my-function
Why can't I use my function?
functions commands interactive
functions commands interactive
edited 19 mins ago
Drew
45.6k460102
45.6k460102
asked 1 hour ago
Tyler
10.5k12044
10.5k12044
I volunteer this question as a generic response to frequently asked questions on this subject. Feel free to expand or clarify however makes sense to make the question and answer discoverable by & useful to people with similar problems!
â Tyler
1 hour ago
Thanks for doing this, Tyler. I flagged the Q for a moderator to please convert this to a community question.
â Drew
12 mins ago
One thing I wonder about is whether the title should just quote the error message. That might be easier for people to find, and it might allow for answers that do not have to do with just addinginteractive
- e.g., sometimes a command disappears from a new version of a library. The error can be raised in any context where Emacs expects a command.
â Drew
10 mins ago
It would be good if folks now searched the wiki for, say,commandp
, to try to find other Qs that can be closed as duplicates of this one. Be careful to read the Q&A, though, as some are different. In some cases, the answer (and the Q context) might be worth repeating here. In other cases the question is unrelated and should be left as is (not closed).
â Drew
7 mins ago
add a comment |Â
I volunteer this question as a generic response to frequently asked questions on this subject. Feel free to expand or clarify however makes sense to make the question and answer discoverable by & useful to people with similar problems!
â Tyler
1 hour ago
Thanks for doing this, Tyler. I flagged the Q for a moderator to please convert this to a community question.
â Drew
12 mins ago
One thing I wonder about is whether the title should just quote the error message. That might be easier for people to find, and it might allow for answers that do not have to do with just addinginteractive
- e.g., sometimes a command disappears from a new version of a library. The error can be raised in any context where Emacs expects a command.
â Drew
10 mins ago
It would be good if folks now searched the wiki for, say,commandp
, to try to find other Qs that can be closed as duplicates of this one. Be careful to read the Q&A, though, as some are different. In some cases, the answer (and the Q context) might be worth repeating here. In other cases the question is unrelated and should be left as is (not closed).
â Drew
7 mins ago
I volunteer this question as a generic response to frequently asked questions on this subject. Feel free to expand or clarify however makes sense to make the question and answer discoverable by & useful to people with similar problems!
â Tyler
1 hour ago
I volunteer this question as a generic response to frequently asked questions on this subject. Feel free to expand or clarify however makes sense to make the question and answer discoverable by & useful to people with similar problems!
â Tyler
1 hour ago
Thanks for doing this, Tyler. I flagged the Q for a moderator to please convert this to a community question.
â Drew
12 mins ago
Thanks for doing this, Tyler. I flagged the Q for a moderator to please convert this to a community question.
â Drew
12 mins ago
One thing I wonder about is whether the title should just quote the error message. That might be easier for people to find, and it might allow for answers that do not have to do with just adding
interactive
- e.g., sometimes a command disappears from a new version of a library. The error can be raised in any context where Emacs expects a command.â Drew
10 mins ago
One thing I wonder about is whether the title should just quote the error message. That might be easier for people to find, and it might allow for answers that do not have to do with just adding
interactive
- e.g., sometimes a command disappears from a new version of a library. The error can be raised in any context where Emacs expects a command.â Drew
10 mins ago
It would be good if folks now searched the wiki for, say,
commandp
, to try to find other Qs that can be closed as duplicates of this one. Be careful to read the Q&A, though, as some are different. In some cases, the answer (and the Q context) might be worth repeating here. In other cases the question is unrelated and should be left as is (not closed).â Drew
7 mins ago
It would be good if folks now searched the wiki for, say,
commandp
, to try to find other Qs that can be closed as duplicates of this one. Be careful to read the Q&A, though, as some are different. In some cases, the answer (and the Q context) might be worth repeating here. In other cases the question is unrelated and should be left as is (not closed).â Drew
7 mins ago
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
The core point is that there is a difference between a function and a command.
In Emacs lisp, functions are not interactively callable by default. That means you can't access them via M-x
or bind them to a key. If you want to do that, you need to explicitly declare the function to be interactive
, which you do by adding an (interactive)
form as the first line in the body (following the documentation string). An interactive function is called a command This is explained in the manual: (info "(elisp) Using Interactive")
(online version).
The error message you see, Wrong type argument: commandp, my-function
, is indicating that you're trying to call a function interactively, but that function isn't a command.
To explain the actual error, the letter p
is often used in lisp to indicate a predicate or test. In this case, Emacs is testing my-function
to see if it is a command using the test commandp
. It isn't, which leads to the error. Similar errors pop up whenever you use an object of the wrong type: if Emacs expects a string and you pass a symbol, you might see a reference to stringp
, for example.
To answer the question as asked, you need to add the (interactive)
line to the definition:
(defun my-function ()
(interactive)
(message "This is a great function"))
There are a lot of options for the interactive
form, supporting all kinds of ways of passing information to your function. Check the manual for all the details.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
The core point is that there is a difference between a function and a command.
In Emacs lisp, functions are not interactively callable by default. That means you can't access them via M-x
or bind them to a key. If you want to do that, you need to explicitly declare the function to be interactive
, which you do by adding an (interactive)
form as the first line in the body (following the documentation string). An interactive function is called a command This is explained in the manual: (info "(elisp) Using Interactive")
(online version).
The error message you see, Wrong type argument: commandp, my-function
, is indicating that you're trying to call a function interactively, but that function isn't a command.
To explain the actual error, the letter p
is often used in lisp to indicate a predicate or test. In this case, Emacs is testing my-function
to see if it is a command using the test commandp
. It isn't, which leads to the error. Similar errors pop up whenever you use an object of the wrong type: if Emacs expects a string and you pass a symbol, you might see a reference to stringp
, for example.
To answer the question as asked, you need to add the (interactive)
line to the definition:
(defun my-function ()
(interactive)
(message "This is a great function"))
There are a lot of options for the interactive
form, supporting all kinds of ways of passing information to your function. Check the manual for all the details.
add a comment |Â
up vote
3
down vote
The core point is that there is a difference between a function and a command.
In Emacs lisp, functions are not interactively callable by default. That means you can't access them via M-x
or bind them to a key. If you want to do that, you need to explicitly declare the function to be interactive
, which you do by adding an (interactive)
form as the first line in the body (following the documentation string). An interactive function is called a command This is explained in the manual: (info "(elisp) Using Interactive")
(online version).
The error message you see, Wrong type argument: commandp, my-function
, is indicating that you're trying to call a function interactively, but that function isn't a command.
To explain the actual error, the letter p
is often used in lisp to indicate a predicate or test. In this case, Emacs is testing my-function
to see if it is a command using the test commandp
. It isn't, which leads to the error. Similar errors pop up whenever you use an object of the wrong type: if Emacs expects a string and you pass a symbol, you might see a reference to stringp
, for example.
To answer the question as asked, you need to add the (interactive)
line to the definition:
(defun my-function ()
(interactive)
(message "This is a great function"))
There are a lot of options for the interactive
form, supporting all kinds of ways of passing information to your function. Check the manual for all the details.
add a comment |Â
up vote
3
down vote
up vote
3
down vote
The core point is that there is a difference between a function and a command.
In Emacs lisp, functions are not interactively callable by default. That means you can't access them via M-x
or bind them to a key. If you want to do that, you need to explicitly declare the function to be interactive
, which you do by adding an (interactive)
form as the first line in the body (following the documentation string). An interactive function is called a command This is explained in the manual: (info "(elisp) Using Interactive")
(online version).
The error message you see, Wrong type argument: commandp, my-function
, is indicating that you're trying to call a function interactively, but that function isn't a command.
To explain the actual error, the letter p
is often used in lisp to indicate a predicate or test. In this case, Emacs is testing my-function
to see if it is a command using the test commandp
. It isn't, which leads to the error. Similar errors pop up whenever you use an object of the wrong type: if Emacs expects a string and you pass a symbol, you might see a reference to stringp
, for example.
To answer the question as asked, you need to add the (interactive)
line to the definition:
(defun my-function ()
(interactive)
(message "This is a great function"))
There are a lot of options for the interactive
form, supporting all kinds of ways of passing information to your function. Check the manual for all the details.
The core point is that there is a difference between a function and a command.
In Emacs lisp, functions are not interactively callable by default. That means you can't access them via M-x
or bind them to a key. If you want to do that, you need to explicitly declare the function to be interactive
, which you do by adding an (interactive)
form as the first line in the body (following the documentation string). An interactive function is called a command This is explained in the manual: (info "(elisp) Using Interactive")
(online version).
The error message you see, Wrong type argument: commandp, my-function
, is indicating that you're trying to call a function interactively, but that function isn't a command.
To explain the actual error, the letter p
is often used in lisp to indicate a predicate or test. In this case, Emacs is testing my-function
to see if it is a command using the test commandp
. It isn't, which leads to the error. Similar errors pop up whenever you use an object of the wrong type: if Emacs expects a string and you pass a symbol, you might see a reference to stringp
, for example.
To answer the question as asked, you need to add the (interactive)
line to the definition:
(defun my-function ()
(interactive)
(message "This is a great function"))
There are a lot of options for the interactive
form, supporting all kinds of ways of passing information to your function. Check the manual for all the details.
edited 22 mins ago
community wiki
2 revs, 2 users 93%
Tyler
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2femacs.stackexchange.com%2fquestions%2f45401%2fwhy-cant-i-bind-my-function-to-a-key-or-call-it-with-m-x%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
I volunteer this question as a generic response to frequently asked questions on this subject. Feel free to expand or clarify however makes sense to make the question and answer discoverable by & useful to people with similar problems!
â Tyler
1 hour ago
Thanks for doing this, Tyler. I flagged the Q for a moderator to please convert this to a community question.
â Drew
12 mins ago
One thing I wonder about is whether the title should just quote the error message. That might be easier for people to find, and it might allow for answers that do not have to do with just adding
interactive
- e.g., sometimes a command disappears from a new version of a library. The error can be raised in any context where Emacs expects a command.â Drew
10 mins ago
It would be good if folks now searched the wiki for, say,
commandp
, to try to find other Qs that can be closed as duplicates of this one. Be careful to read the Q&A, though, as some are different. In some cases, the answer (and the Q context) might be worth repeating here. In other cases the question is unrelated and should be left as is (not closed).â Drew
7 mins ago