Elisp: How to Write Commands

By Xah Lee. Date: . Last updated: .

Here's the basics of how to write a emacs command that user can call by Alt+x name.

Command Template

This is the typical template for user-defined emacs commands.

(defun my-command ()
  "One sentence summary of what this command do.

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."
  (let (var1 var2 …)
    (setq var1 …)
    (setq var2 …)
    ;; do something …

In your doc string, if you want clickable URL, or clickable reference to other commands, etc., See: Elisp: Doc String Markup.

be sure to also read Elisp: Simple Emacs Lisp Examples

Put $5 at https://www.patreon.com/xahlee , or goto paypal.com and pay to Xah@XahLee.org . Message me on XahLee discord, say so, ask me questions.
Or Buy Xah Emacs Tutorial

Emacs Tutorial

Emacs Init

Emacs Keys


ELisp Examples

ELisp Write Major Mode



Basic Functions

Writing Command

Writing Script

Lisp Data Structure

Lisp Symbol

Elisp Misc