5 ways to get text from an Emacs buffer
Rajasegar Chandran

Rajasegar Chandran @rajasegar

About: Javascript Toolsmith, Front-end developer

Location:
Chennai
Joined:
Oct 8, 2019

5 ways to get text from an Emacs buffer

Publish Date: Jan 1 '24
3 2

Emacs Lisp is a powerful programming language that allows you to customize and extend your Emacs editor. One common task when working with text in Emacs Lisp is extracting or copying text from a buffer. In this blog post, we will explore different ways of getting text from an Emacs buffer.

1. Using buffer-substring

The buffer-substring function is a simple and straightforward way to extract text from a buffer. It takes two arguments: start and end positions, and returns the corresponding text.

(let ((start (point-min))
      (end (point-max)))
  (buffer-substring start end))
Enter fullscreen mode Exit fullscreen mode

In this example, we extract the entire buffer by setting the start position to (point-min) and the end position to (point-max).

2. Using buffer-substring-no-properties

If you want to exclude text properties (such as font face or color) from the extracted text, you can use the buffer-substring-no-properties function. It works the same way as buffer-substring.

(let ((start (point-min))
      (end (point-max)))
  (buffer-substring-no-properties start end))
Enter fullscreen mode Exit fullscreen mode

3. Using buffer-string

The buffer-string function returns the entire contents of the current buffer as a string. This is a convenient way to extract text when you don't need to specify start and end positions.

(buffer-string)
Enter fullscreen mode Exit fullscreen mode

4. Using region

If you have an active region in your Emacs buffer, you can use the region-beginning and region-end functions to get the start and end positions of the selected text. You can then use buffer-substring or buffer-substring-no-properties to extract the text.

(let ((start (region-beginning))
      (end (region-end)))
  (buffer-substring start end))
Enter fullscreen mode Exit fullscreen mode

5. Using thing-at-point

The thing-at-point function allows you to retrieve different types of text at the current point. You can specify the type of thing using the thing argument. For example, to get the word at point, you can use (thing-at-point 'word).

(thing-at-point 'word)
Enter fullscreen mode Exit fullscreen mode

You can also combine thing-at-point with bounds-of-thing-at-point to retrieve the start and end positions of the thing.

(let ((bounds (bounds-of-thing-at-point 'word)))
  (buffer-substring (car bounds) (cdr bounds)))
Enter fullscreen mode Exit fullscreen mode

These are just a few examples of different ways to extract text from an Emacs buffer in Emacs Lisp. Depending on your specific use case, you may choose a different approach. Emacs Lisp provides a wide range of functions and tools to manipulate text, giving you the flexibility to perform various text-related operations in Emacs.

If you know any other way of obtaining the text from the buffer, please let us know in the comments section. I would love to learn more tricks about Elisp functions for getting text from the buffer.

Comments 2 total

  • Robina
    RobinaJan 1, 2024

    good Job

  • Kalman Reti
    Kalman RetiJun 28, 2025

    Additional ways I get information from emacs buffers:

    1) char-before and char-after

    These two elisp functions take a position (either as a number or marker)
    and return the character before or after that position; I use this in
    situations where I need to skip characters (like the every-other null
    byte of windows wide characters), e.g.

    (coerce
        (save-excursion
          (loop until (eobp)
                for char = (char-after (point))
                unless (zerop char)
                collect char))
        'string)
    
    Enter fullscreen mode Exit fullscreen mode

    or in situations when I want to check some indicator character in
    columnar output after a search

       (let ((position (search-forward "prefix-string" nil t)))
         (when position
           (char-after (point))))
    
    Enter fullscreen mode Exit fullscreen mode

    2) read

    Read can return lisp objects (numbers, symbols and string) from buffer contents.

    For example, if I have a lot of lines that look like

       "foobar" -> baz
       testing -> (1 2 3 4)
    
    Enter fullscreen mode Exit fullscreen mode

    I can put them into a hash table as key value pairs by doing

       (loop with ht = (make-hash-table :test'equal)
             until (eobp)
             for (a b c) = (loop repeat 3 collect (read (current-buffer)))
             unless (eql b '->)
             do (debug (format "got unexpected token %s instead of ->" b))
             do (setf (gethash a ht) c)
                (end-of-line) (forward-char)
             finally (return ht))
    
    Enter fullscreen mode Exit fullscreen mode
Add comment