Elisp: Define a Command

By Xah Lee. Date: . Last updated: .

What is a Command

A command is a function that can be called interactively by execute-extended-commandAlt+x】.

Define a Command

to make a function a command, add

(interactive)

after the doc string. 〔see Elisp: Interactive Form

(defun yay ()
  "Insert YES at cursor position."
  (interactive)
  (insert "YES"))

Command Template

Here is a function definition template that majority of elisp commands follow.

(defun my-command ()
  "One sentence summary of what this command do. Less than 70 chars, try.

More details here.
Be sure to document the parameters.
Be sure to mention the return value, if any.

Lines here should not be indented."
  (interactive)
  (let (var1 var2 etc)
    ;; do something here
    ;; last expression is returned
    ))

Reference

Emacs Lisp, Function