Overview of Text-Processing in Emacs Lisp

By Xah Lee. Date: . Last updated: .

This page is a overview of how text manipulation is done using emacs lisp.

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. This page gives a overview of how this environment works.

Emacs provides you with a text-edit programing framework. More specifically, a set of functions for text manipulation.

For example, there is a lisp function that returns the cursor position in a buffer. A function that returns the beginning/ending position of the text selection. Functions that move the cursor to a given position, or delete a block of text of given positions. Functions that insert a string at a given position. Functions that open or save files. Functions that list opened files. Functions that list buffer names. Function that returns the current mode of a given buffer. Functions that activate modes. Functions that make a section of text into a particular color or font, … and much more.

So, programing emacs to manipulate text, is a matter of calling these functions provided in Emacs.

Example of Simple Elisp Functions

Here are examples of simple elisp functions.

You can evaluate any of them. Just paste the code, select it, and call eval-region. 〔►see How to Evaluate Emacs Lisp Code

Cursor Position

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

;; returns the position of the beginning/end of region
(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)

Moving Cursor and Searching

;; 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")

Deleting/Inserting/Changing 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)

Strings

;; 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"

Buffers

;; 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.
)

Files

;; 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 illustrates 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))

Type the above, then select the whole code, Alt+x eval-region.

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

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

For a basic intro of elisp, see: Emacs Lisp Basics.

Programing a Major Mode

The above gives you many examples of text-processing functions in elisp.

The other area of elisp programing, is to program emacs itself, to create major or minor modes and their interface. For example, creating menus, interpreting keyboard inputs, changing behavior of mouse buttons, syntax coloring, manipulating windows, displaying image files, creating a file manager (dired), creating a interactive command line interface (shell), programing network clients (For example, ftp, irc, http), creating user interface (menus, buttons, toolbars, status bar), creating keyword completion, etc. In summary, these type of tasks we can call it “Programing a Major Mode”.

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

For most people, text edit programing is far more useful to learn, because that's what text editor users need to do in their jobs, and most major modes have already been written for them.

For writing a major mode, you should first have good elisp experience in text processing first. Here's a basic example of writing a major mode: How to Write a Emacs Major Mode for Syntax Coloring.

Emacs Lisp Basics Topic

  1. Emacs Lisp Basics
  2. Overview of Text-Processing in Emacs Lisp
  3. Emacs Lisp Examples, page 1
  4. How to Evaluate Emacs Lisp Code
  5. Emacs Lisp: Documentation Lookup
  6. Emacs Lisp: Search Documentation
  7. How to Edit Lisp Code with Emacs
Like it? Buy Xah Emacs Tutorial. Thanks.

or, buy something from Best Keyboard for Emacs