Elisp: Text Properties

By Xah Lee. Date: . Last updated: .

Text in a buffer can have “text properties”.

Text properties is used to color text, or add keybinding to that text (for example, pressing Enter ↵ on a link in HTML opens the link instead of insert return.), and other purposes.

Here's a command that makes a region red, by adding a text property of 'font-lock-face to it.

(defun x-make-word-red (begin end)
  "make current region colored red, using text properties"
  (interactive "r")
  (put-text-property begin end 'font-lock-face '(:foreground "red")))

Select a region, then Alt+x x-make-word-red

Here's example of adding a keymap to text, so that when Enter ↵ is pressed, it tries to open the file, taking current line as file name/path.

(defun x-open-me ()
  "open a file, using current line as file name/path"
  (interactive)
  (find-file
   (buffer-substring-no-properties (line-beginning-position) (line-end-position))))

(defvar x-keymap nil "sample keymap")
(setq x-keymap (make-sparse-keymap))
(define-key x-keymap (kbd "RET") 'x-open-me)

(defun x-add-prop (begin end)
  "add text properties to a region."
  (interactive "r")
    (put-text-property begin end 'font-lock-face '(:foreground "blue"))
    (put-text-property begin end 'keymap x-keymap))

To try the above code:

  1. Open a new file, with some lines being file path.
  2. Go to a line that's a file path, select it, then Alt+x x-add-prop. The region will be colored blue, and pressing Enter ↵ key on it will call x-open-me

What's Text Property

Text Property is a property list attached to text.

A property list is a list but interpreted as key and value pairs. Like this:

'(key1 val1 key2 val2 …)

〔►see Elisp: Property List

When a property list is attached to text, some keys have special meaning that are predefined by emacs.

Here are some of the most useful property keys for writing a major/minor mode:

For a complete list, see (info "(elisp) Special Properties")

Functions for Working with Text Property

There are many functions to get values of text properties, add/change/delete them, or find out where a property begins, etc. See

(info "(elisp) Text Properties")

Writing Major Mode

  1. How to Write a Emacs Major Mode for Syntax Coloring
  2. Elisp: html6-mode
  3. Elisp: Font Lock Mode Basics
  4. Elisp: How to Define Face
  5. Elisp: How to Color Comment in Major Mode
  6. Elisp: How to Write Comment Command in Major Mode
  7. Elisp: How to Write Your Own Comment Command from Scratch
  8. Elisp: How to Write Keyword Completion Command
  9. Elisp: How to Create Keymap for Major Mode
  10. Elisp: Create Abbrev and Templates for Major Mode
  11. Elisp: Text Properties
  12. Elisp: Overlay Highlighting
  13. Emacs: Lookup Google, Dictionary, Documentation
  14. Elisp: Syntax Table Tutorial

  1. Elisp: How to Name Your Major Mode
  2. Elisp: What's “feature”?
  3. Elisp: require, load, load-file, autoload, feature, Explained

Syntax Table

  1. Elisp: Syntax Table Tutorial
  2. Elisp: How to Find Syntax of a Character?
  3. Elisp: How to Modify Syntax Table Temporarily
  4. Elisp: How to Determine If Cursor is Inside String or Comment
  5. Elisp: Regex Patterns and Syntax Table
  6. Elisp: Find Matching Bracket Character
Liket it? Put $5 at patreon. Or Buy Xah Emacs Tutorial. Thanks.