Emacs Lisp Examples ₁

Buy Xah Emacs Tutorial. Master emacs benefits for life.
, , …,

This page shows several very simple emacs lisp code. They show you the basic programing in elisp, but also, they are very useful themselves.

To see a function's documentation, call describe-functionF1 f】. A variable's documentation is describe-variableF1 v】.

Insert Text

This code shows how to insert a string, and also position cursor after the insertion.

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

You can use this code to insert your {signature, function template, XML template, headers, footers, …}. (but if you want a systematic set of templates/snippets, better is Emacs Templates with YASnippet.)

Insert Around Region

This code shows how to place a string at the beginning and end of a region.

(defun wrap-markup-region (start end)
  "Insert a markup <b></b> around a region."
  (interactive "r")
    (goto-char end) (insert "</b>")
    (goto-char start) (insert "<b>")

You can use this code to add HTML begin/end tag on a selected text, or add parenthesis around a selection.

Thanks to Marcin Milewski for a correction on “wrap-markup-region”.

select-current-word, select-current-line

This code shows you how to set a mark (select text) programmatically.

(transient-mark-mode 1)

(defun select-current-word ()
"Select the word under cursor.
“word” here is considered any alphanumeric sequence with “_” or “-”."
 (let (pt)
   (skip-chars-backward "-_A-Za-z0-9")
   (setq pt (point))
   (skip-chars-forward "-_A-Za-z0-9")
   (set-mark pt)
(transient-mark-mode 1)

(defun select-current-line ()
  "Select the current line"
  (end-of-line) ; move to end of line
  (set-mark (line-beginning-position)))

See also: Emacs: What's Region, Active Region, transient-mark-mode?.

Find/Replace Text Region

This code illustrates how to do text replacements on a region. Very useful. For example:

(defun replace-greek-region (start end)
  "Replace “alpha” to “α” and other greek letters in current region."
  (interactive "r")
    (narrow-to-region start end)
    (goto-char (point-min))
    (while (search-forward " alpha" nil t) (replace-match " α" nil t))
    (goto-char (point-min))
    (while (search-forward " beta" nil t) (replace-match " β" nil t))
    (goto-char (point-min))
    (while (search-forward " gamma" nil t) (replace-match " γ" nil t))

You can modify the code to do other replacements. For example, HTML/XML Entities List or URL Percent Encoding.

For some detailed lesson on this code, see: Repeated Find Replace.

Delete Enclosed Text

This code illustrates how to delete a text enclosed by any pairs of delimiters.

For example, if you are editing HTML code, suppose you have text <p>good morn and this is a long sentence …</p> and your cursor is somewhere in between the tags. You want to quickly delete all texts inside the p tags. The following function will do. It will also, delete any text between quotes or parenthesis.

(defun delete-enclosed-text ()
  "Delete texts between any pair of delimiters."
    (let (p1 p2)
      (skip-chars-backward "^([<>“") (setq p1 (point))
      (skip-chars-forward "^)]<>”") (setq p2 (point))
      (delete-region p1 p2))))

For many related code, see: Emacs: extend-selection, select-text-in-quote, select-current-line, select-current-block

Delete Linebreaks

This example shows how to temporarily change a pre-defined variable's value, then call a function whose behavior depends on the var.

(defun remove-line-breaks ()
  "Remove line endings in current paragraph."
  (let ((fill-column (point-max)))
    (fill-paragraph nil)))

For detail, see: Emacs unfill-paragraph, unfill-region, compact-uncompact-block.

Next/Previous User Buffer

In this example, simple lisp constructions are shown, including while, and, string-match. This is also a very convenient function. It allows you to switch to the next buffer without going thru a bunch of irrelevant buffers that emacs created, ⁖ {*scratch*, *Messages*, *Shell Command Output*, *Completions*, *calc*, *grep*, …}.

(defun next-user-buffer ()
  "Switch to the next user buffer.
User buffers are those whose name does not start with *."
  (let ((i 0))
    (while (and (string-equal "*" (substring (buffer-name) 0 1)) (< i 20))
      (setq i (1+ i)) (next-buffer))))
(defun previous-user-buffer ()
  "Switch to the previous user buffer.
User buffers are those whose name does not start with *."
  (let ((i 0))
    (while (and (string-equal "*" (substring (buffer-name) 0 1)) (< i 20))
      (setq i (1+ i)) (previous-buffer) )))

You can give them a easy key. See Emacs: How to Define Keys.

Inserting a Random Number

(random t) ; seed it randomly

(defun insert-random-number ()
  "Insert a random number between 0 to 999999."
  (insert (number-to-string (random 999999))) )

For more, see: Emacs Lisp: Insert Random Number, Random String, Random Hexadecimal

Like it?
Buy Xah Emacs Tutorial
or share
blog comments powered by Disqus