Elisp: Buffer and File Functions

By Xah Lee. Date: . Last updated: .

Here's the most useful functions for file and 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, or a buffer object.

Get Buffer Name

buffer-name → return the name of current buffer.

;; return the name of current buffer

buffer-file-name → return the full path of the file, or nil if not a file.

;; return the full path of the file

Switch Buffer

with-current-buffer → temporarily make a buffer current.

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
  ;; insert or delete text here

set-buffer → switch to a given buffer. (but does not make the buffer visible.)


  ;; switch to myBuf
  (set-buffer myBuf)

  ;; do stuff, such as insert/delete text

Note, 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.

Create Buffer

(with-temp-buffer &rest BODY) → Create a temporary buffer, and evaluate BODY there like progn. See also with-temp-file and with-output-to-string.

;; use a temp buffer to manipulate string

(setq myStr "big text")

  (insert myStr)

  ;; manipulate the string here

  ;; print whole buffer content
  (message "%s" (buffer-string)))

Note: 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.

generate-new-buffer → create a new buffer, returns it.

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)
;; 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 → close a given buffer.

;; close a given buffer
(kill-buffer myBuffer)

(info "(elisp) Buffers")


{Open, Append, Write} Files

;; open a file (returns a buffer)
(find-file "~/test.txt")

;; save current buffer (write to the associated file)

;; like “Save As”. Save current buffer, close it, and open the new saved
(write-file "~/new.txt")

;; append text between positions 100 to 200 to file
(append-to-file 100 200 "~/test.txt")

;; close a buffer
(kill-buffer myBuffName)

(info "(elisp) Files")

Elisp Basic Functions

  1. Cursor
  2. Text Editing
  3. Get Buffer String
  4. String Functions
  5. Buffer and File
  6. File and Directory
Patreon me $5 patreon

Or Buy Xah Emacs Tutorial

Or buy a nice keyboard: Best Keyboards for Emacs

If you have a question, put $5 at patreon and message me.