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 ()
(interactive)
(bar))
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 defun
s here. As @Stefan points out in the comments, keyboard macros are a special case: They can be considered commands, but they are not functions.
C-h i
, choose the Elisp manual,i command
- puts you right in nodeWhat is a function?
, which makes all of this crystal clear. Help users learn to ask Emacs. (Just one opinion.)