Emacs Lisp: Buffer Functions

By Xah Lee. Date: . Last updated: .

What is a Buffer

A buffer is a special emacs lisp data structure that holds text (e.g. file content) and allows you to search or edit it in a efficient way, programatically. You can think of it as a enhanced string data structure for storing large text for modification.

Here's the most useful functions for buffer.

Most buffer functions assume the current buffer if no argument is given. Some requires a argument. The argument can usually be a buffer's name (a string), or a buffer object (returned by buffer functions).

Get Buffer Name

(buffer-name &optional BUFFER)
Return the name of current buffer.
(buffer-file-name &optional BUFFER)
Return the full path of the file, or nil if not a file.

Switch Buffer

(with-current-buffer BUFFER-OR-NAME &rest BODY)
Temporarily make a buffer current.

Tip: Most of the time, you want to use this. Because it takes care of switching back to the original buffer when the function is done.

;; make myBuf current temporarily
(with-current-buffer myBuf
  ;; code to edit text here
(set-buffer BUFFER-OR-NAME)
Switch to a given buffer. (but does not make the buffer visible.)

Tip: there is also switch-to-buffer, but it's not designed to be used in lisp code. Use it only if you need the buffer to be visible.


  ;; switch to myBuf
  (set-buffer myBuf)

  ;; do stuff, such as insert/delete text

Create Buffer

(with-temp-buffer &rest BODY)
Create a temporary buffer, and evaluate BODY, return the last expression.

Tip: most of the time, you should just use with-temp-buffer to create new buffers. Because that saves you code of creating buffer, switching to it, do something, possibly close it, and restore (switch back) to the buffer that was current.

;; use a temp buffer to manipulate string

(setq myStr "big text")

  (insert myStr)

  ;; manipulate the string here

  ;; return buffer content as string
(generate-new-buffer NAME)
Create and return a buffer with a name based on NAME. Buffer name is created by calling generate-new-buffer-name.

Typically used like this:

;; name for new buffer. If start with space, undo is disabled
(setq newBufName " xyz")

;; create a new buffer, save it to a var, so later you can switch to it or kill it
(setq newBuf (generate-new-buffer newBufName))

;; make it current (but does not make it visible), so all insert etc operations works on it.
(set-buffer newBuf)
(get-buffer-create BUFFER-OR-NAME)
  • Returns the buffer, but doesn't make it current. use set-buffer to make it current.
  • BUFFER-OR-NAME can be a string or buffer.
  • If BUFFER-OR-NAME is a buffer datatype and the buffer exists, it's just returned. if not exist, new is created.
  • If BUFFER-OR-NAME is a string and start with a space, undo is not enabled.
;; create new buffer, without undo info. make sure the string passed is unique and has space in front
(setq newBuf (get-buffer-create " xyz"))

;; make it current (but does not make it visible), so all insert etc operations works on it.
(set-buffer newBuf)

Kill Buffer

(kill-buffer &optional BUFFER-OR-NAME)
Close current buffer or a specified buffer.

(info "(elisp) Buffers")

Emacs Lisp File/Buffer

Practical Elisp ⭐

Writing Command

Text Processing

Get User Input


Writing Script