How to Learn Emacs Lisp?

By Xah Lee. Date: . Last updated: .

A few people have expressed this problem of learning elisp. It goes like this:

I've read the basic of elisp, but how do you actually stick to it?

Here's a suggestion. Suppose you code in Ruby (or C, PHP, Perl, Python, …). Now, begin by stop using the default major mode. Roll your own! This is actually easier than it sounds. You can begin by writing a very basic mode, that does nothing but syntax coloring of the keywords. This is very easy. If you know the basic syntax of elisp, you can do this in a hour. See: How to Write a Emacs Major Mode for Syntax Coloring.

Once you have that, setup emacs to use your own mode. 〔➤see Emacs: Set File to Open in a Major Mode

Now, you will start to find problems. For example, keyword completion, function templates, reference lookup, indentation. You can now gradually add features, add commands.

Soon, you'll have learned a ton of elisp. If you keep at it, after a year, you'll end up with your own robust mode. You'll also start to understand all the useful packages you installed. Maybe fixed some bugs or added features. And maybe write something useful that doesn't exist yet.

My Path to Emacs Lisp

My path to elisp is similar to this. I code HTML raw since 1996, by manually typing each tag. I started to use emacs in 1998. Emacs's HTML mode is very basic, i started to master that mode, to insert tags, delete tags. I started to learn elisp in 2006. I started to write many commands to help me code and maintain my websites, that's about 5 thousand hand-coded HTML pages (with the help of emacs automation, and some perl, python, scripts. (vast majority of these scripts are now emacs lisp)).

Here's one of my first elisp code. I was actually overjoyed when i learned it.

(defun insert-p-tag ()
  "Insert <p></p> at cursor point."
  (insert "<p></p>")
  (backward-char 4))

Today, this code went thru many versions, and now looks like this:

(defun xah-html-wrap-html-tag (*tag-name &optional *class-name)
  "Insert HTML open/close tags to current text unit or text selection.
When there's no text selection, the tag will be wrapped around current {word, line, text-block}, depending on the tag used.

If current line or word is empty, then insert open/end tags and place cursor between them.
If `universal-argument' is called first, then also prompt for a “class” attribute. Empty value means don't add the attribute.
Version 2016-04-24"
    (ido-completing-read "HTML tag:" xah-html-html5-tag-list "PREDICATE" "REQUIRE-MATCH" nil xah-html-html-tag-input-history "div")
    (if current-prefix-arg
        (read-string "class:" nil xah-html-class-input-history "")
      nil )))
  (let (-bds -p1 -p2 -wrap-type )
    (setq -wrap-type (xah-html-get-tag-type *tag-name))
    (setq -bds
           ((equal -wrap-type "w") (xah-html--get-thing-or-selection 'word))
           ((equal -wrap-type "l") (xah-html--get-thing-or-selection 'line))
           ((equal -wrap-type "b") (xah-html--get-thing-or-selection 'block))
           (t (xah-html--get-thing-or-selection 'word))))
    (setq -p1 (elt -bds 1)
          -p2 (elt -bds 2))

      (narrow-to-region -p1 -p2)
      (when ; trim whitespace
           (not (use-region-p))
            (equal -wrap-type "l")
            (equal -wrap-type "b"))
           (not (or
                 (string-equal *tag-name "pre")
                 (string-equal *tag-name "code"))))
          (goto-char (point-min))
          (goto-char (point-max))

      ;; add blank at start/end
      (when (equal -wrap-type "b")
          (goto-char (point-min))
          (insert "\n")
          (goto-char (point-max))
          (insert "\n")))
      (xah-html-add-open-close-tags *tag-name *class-name (point-min) (point-max)))

    (when ; put cursor between when input text is empty
        (not (xah-html-tag-self-closing-p *tag-name))
      (when (and
             (= -p1 -p2))
        (search-backward "</" )))))

〔The code needs other stuff from Emacs: Xah HTML Mode.〕

I have close to a hundred other commands for HTML. For example:

And any CSS related needs, HTML5, … anything i need or want. After some 5 years, i started my own HTML mode, the way i wanted things to work. 〔➤see Emacs: Xah HTML Mode

When you use a light-weight markup system, you type a few chars to markup, such as =title= or ==title==. 〔➤see Markdown Tutorial〕 But with my own system, i press ONE KEY to transfrom text under cursor to a customized HTML markup i want. I can confidently say that writing raw HTML is actually faster than using any light-weight markup (such as org-mode or markdown) or any web site framework, plus, i have complete control of the raw HTML code, and can use full HTML features, any tag, not limited to what light-weight markup supports, and the generated HTML are all W3C valid HTML. 〔➤see HTML Correctness and Validators

Interesting? Start your own mode today!

Like it? Buy Xah Emacs Tutorial.

or, buy something from my keyboard store.