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.

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."
  (interactive)
  (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.

Basic Functions

Be sure you've already read the following:

Elisp Basic Functions

  1. Cursor
  2. Text Editing
  3. Get Buffer String
  4. String Functions
  5. Buffer
  6. Read/Write File
  7. File/Dir Path

be sure to also read Elisp: Simple Emacs Lisp Examples

Elisp: Writing Command

  1. Writing Command, Basics
  2. Mark and Region
  3. Get Buffer String
  4. Work with Lines
  5. Copy Cut Paste kill-ring
  6. Get User Input
  7. Interactive Form
  8. Get universal-argument
  9. Find Replace Text
  10. thing-at-point
  11. Get Dired Marked Files

If you have a question, put $5 at patreon and message me.
Or Buy Xah Emacs Tutorial
Or buy a nice keyboard: Best Keyboards for Emacs

Emacs

Emacs Lisp

Misc