Emacs Lisp Examples, page 1

By Xah Lee. Date: . Last updated: .

This page shows very simple and useful emacs lisp functions. They show you the basic programing in elisp.

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, template, headers, footers, …}.

Put cursor at the last parenthesis, then call eval-last-sexpCtrl+x Ctrl+e】.

Then, you can call the command you just defined by name. For example, M-x insert-p-tag.

〔➤see How to Evaluate Emacs Lisp Code

To see a function's documentation, call describe-functionCtrl+h f】.

〔➤see Emacs Lisp: Documentation Lookup

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 brackets around a selection.

Select Current Word, Select Current Line

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

;; turn on highlight selection
(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?.

For full versions, see: Emacs: Select Line, between Quotes, Extend Selection

Find Replace String in Region

Here's how to do text replacements on a region. Very useful.

(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 or URL Percent Encoding.

see also:

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>something something long …</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: Select Line, between Quotes, Extend Selection

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: Hard Wrap Lines, fill-paragraph, unfill-paragraph.

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, ➢ for example: {*scratch*, *Messages*, *Shell Command Output*, *Completions*, *calc*, *grep*, …}.

See: Emacs: Next/Previous User Buffer

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 / Hexadecimal

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

Like it? Buy Xah Emacs Tutorial. Thanks.

or, buy something from my keyboard store.