Emacs Lisp: 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 "/home/jane/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 "/home/jane/new.txt")

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

;; close a buffer
(kill-buffer myBuffName)

(info "(elisp) Files")

Like it? Buy Xah Emacs Tutorial. Thanks.

or, buy something from Best Keyboard for Emacs