Emacs Lisp: How to Write a Command
- A command is a function that emacs user can call by
- To make a function a command, add
(interactive)right after the doc string. [see Emacs Lisp: Interactive Form]
- When a function is also a command, we say that the function is available for interactive use.
Evaluate the following code. Then, you can call it by
(defun yay () "Insert “Yay!” at cursor position." (interactive) (insert "Yay!"))
(info "(elisp) Defining Commands")
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 mention the return value if relevant. Lines here should not be longer than 70 chars, and don't indent them." (interactive) (let (var1 var2 etc) ;; do something here ;; last expression is returned ))
In your doc string, if you want clickable URL, or clickable reference to other commands, etc., See: Emacs Lisp: Function Doc String Markup.
be sure to also read Emacs Lisp: Simple Emacs Lisp Examples