When posting questions and answers here, people sometimes use the terms "function" and "command" interchangeably. In other cases, people only use one of the two terms to discuss specific pieces of code. As their posts usually focus on other topics, they don't explain why they are using one term but not the other. So:

Q: In Emacs Lisp, what is the difference between a function and a command?

  • 13
    You have every right to do so, of course, and I'm sure some people will be helped by this. But FWIW I'm not in favor of Q & A here for every little thing that it might be better to teach someone to ask Emacs itself about. This is one such, IMO - it is not hard to find this out, and Emacs gives a good answer. C-h i, choose the Elisp manual, i command - puts you right in node What is a function?, which makes all of this crystal clear. Help users learn to ask Emacs. (Just one opinion.) – Drew Nov 15 '14 at 4:32
  • 1
    That said, you posed and answered the question well. – Drew Nov 15 '14 at 4:35
  • 5
    @Drew I agree 100% on teaching people to ask Emacs first. The main purpose of this specific Q&A is to make it easier to promote correct use of the terms and to make users aware of the differences between commands and functions if necessary: Sometimes, people are not aware that this is a question they should be asking, and having a generic resource to point them to is easier than repeating the same content over and over again in the comments. – itsjeyd Nov 15 '14 at 9:42
  • 1
    That said, thanks for mentioning how to find relevant information inside of Emacs :) – itsjeyd Nov 15 '14 at 9:42
  • 1
    We agree - it's about helping users use Emacs. This site is a means to that end. – Drew Nov 15 '14 at 15:03

Every command is a function, but not every function is also a command.1

A command includes a call to interactive; this is why commands are commonly referred to as "interactive functions". Commands can be invoked via M-x name-of-command RET, and they can also be bound to a key sequence. Regular functions do not include a call to interactive, can not be called using M-x, and you can't bind them to a key sequence. To run a function that is not interactive, you can press M-: (eval-expression), enter the name of the function followed by values for any arguments it needs enclosed in parentheses, and press RET:

M-: (name-of-function arg1 arg2 arg3) RET

If the function is not supposed to operate on the current buffer, you can also enter

(name-of-function arg1 arg2 arg3)

in the *scratch* buffer and press C-x C-e (eval-last-sexp) with point positioned after the closing parenthesis.

To make a function bar available as a command you can wrap it in a custom interactive function (foo) as follows:

(defun foo ()

Of course, if bar takes one or more arguments, you will have to supply them in order to make foo work correctly.

If you see people using the terms "function" and "command" interchangeably, this might indicate (depending on context) that they are not aware of the differences between the underlying concepts.

1 Note that I am talking about defuns here. As @Stefan points out in the comments, keyboard macros are a special case: They can be considered commands, but they are not functions.

  • 5
    Minor nitpick: interactive "calls" are usually called declarations (the function itself doesn't actually do anything). – shosti Nov 15 '14 at 16:21
  • 5
    @itsjeyd: Actually, no, there are also commands which are not functions. This is the case for keyboard macros. E.g. M-: (commandp [?a]) RET will (correclty) tell you that [?a] is a command, but it is not a function. – Stefan Apr 27 '15 at 15:20
  • @Stefan Thanks for pointing that out. I updated my answer. – itsjeyd Apr 28 '15 at 11:58

Your Answer

