Overview of Text-Processing in Emacs Lisp

By Xah Lee. Date: . Last updated: .

In emacs, a user can program it using the embedded language (called Emacs Lisp, or elisp) so that he can have custom functions to insert texts, templates, process files, and many other features of emacs.

Emacs provides functions for text manipulation. For example:

Example of Simple Elisp Functions

Cursor Position

;; current cursor position is called “point”.
;; Left of first char in buffer is 1
;; This returns the current cursor position
(point)

;; returns the position of the beginning/end of region (selection)
(region-beginning)
(region-end)

;; position for beginning/ending of current line
(line-beginning-position)
(line-end-position)

;; returns the position for the beginning/end of buffer, taking account of narrow-to-region
(point-min)
(point-max)

Note: position is considered between characters. The left of the first character in buffer has position 1.

By default, cursor is shown as black square over a character, the position is to the left of the black square.

Try this. Type

(point)

in a buffer. Then, Evaluate Emacs Lisp Code

The result is current cursor position.

Move Cursor, Search Text

;; move cursor to position 39
(goto-char 39)

;; move cursor by 4 chars
(forward-char 4)
(backward-char 4)

;; move cursor to the location of a string
;; returns the new position
(search-forward "some") ; to end of “some”
(search-backward "some") ; to beginning of “some”

;; move cursor to the location matched by a regex
;; returns the new position
(re-search-forward "[0-9]") ; digit
(re-search-backward "[0-9]")

;; move cursor to the first char that's not “a to z”
;; Returns the distance traveled.
(skip-chars-forward "a-z")
(skip-chars-backward "a-z")

Delete, Insert, Change, Text

;; delete 9 chars starting at current cursor pos
(delete-char 9)

;; deleting text from pos 3 to 10
(delete-region 3 10)

;; insert string at current cursor position
(insert "i ♥ cats")

;; get the string from pos 71 to 300
(setq x (buffer-substring 71 300))

;; capitalize letters in a region
(capitalize-region 71 300)

String

;; length
(length "abc")
; returns 3

;; substring
(substring "abcdefg" 3 4)
; returns "d"

;; change a given string using regex
(replace-regexp-in-string "[0-9]" "X" "abc123")
;; returns "abcXXX"

Buffer

;; return the name of current buffer
(buffer-name)

;; return the full path of current file
(buffer-file-name)

;; switch to the buffer named xyz
(set-buffer "xyz")

;; save current buffer
(save-buffer)

;; close a buffer named xyz
(kill-buffer "xyz")

;; temporarily sets a buffer as current to work with
(with-current-buffer "xyz"
  ;; do something here. delete/insert text, etc.
)

File

;; open a file (in a buffer)
(find-file "~/")

;; same as “Save As”.
(write-file path)

;; insert file into current position
(insert-file-contents path)

;; append a text block to file
(append-to-file start-pos end-pos path)

;; renaming file
(rename-file file-name new-name)

;; copying file
(copy-file old-name new-name)

;; deleting file
(delete-file file-name)

;; get dir path
(file-name-directory full-path)

;; get filename part
(file-name-nondirectory full-path)

;; get filename's suffix
(file-name-extension file-name)

;; get filename sans suffix
(file-name-sans-extension file-name)

A Simple Example

This code shows how to insert a string, then position cursor somewhere inside.

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

Copy and paste the above into any buffer, then select the whole code, Alt+x eval-region.

To call the command, Alt+x insert-p-tag.

For more simple and practical elisp examples, see Elisp Examples.

Programing a Major/Minor Mode

Programing emacs is more than text processing. For example, all of the following major modes are written in elisp:

Tasks of writing a mode is more complex, because it involves understanding many of emacs's systems: keyboard input event, display (windows and fonts), user interface (menu, windows, scroll bar, tool bar), major/minor mode's structure, coloring text, package structure, etc.

You should have some experience doing text processing in elisp before writing a major mode.

Emacs Lisp How to Write Major Mode