Elisp: Buffer and File Functions

By Xah Lee. Date: . Last updated: .

Here's the basic functions for file and buffer.

You should memorize these by heart.


Functions that act on the buffer data type. Most buffer function 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.

;; return the name of current buffer

;; return the full path of the file

;; switch to a given buffer
(set-buffer myBuffer)

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

;; use a temp buffer to manipulate string
  (insert myStr)
  ;; manipulate the string here
  (buffer-string) ; get result

(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. Elisp: Cursor Position Functions
  2. Elisp: Text Editing Functions
  3. Elisp: Buffer and File Functions
  4. Elisp: File and Directory Functions
Like my tutorial? Put $5 at patreon

Or Buy Xah Emacs Tutorial

Or buy a nice keyboard: Best Keyboard for Emacs

Ask me question on patreon