Why can't I bind my function to a key or call it with M-x?

The name of the pictureThe name of the pictureThe name of the pictureClash 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?










share|improve this question























  • 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















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?










share|improve this question























  • 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













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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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

















  • 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
















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











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.






share|improve this answer






















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "583"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    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






























    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.






    share|improve this answer


























      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.






      share|improve this answer
























        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.






        share|improve this answer














        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.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited 22 mins ago


























        community wiki





        2 revs, 2 users 93%
        Tyler




























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            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













































































            Comments

            Popular posts from this blog

            What does second last employer means? [closed]

            Installing NextGIS Connect into QGIS 3?

            One-line joke