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.

Buffers

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
(buffer-name)

;; return the full path of the file
(buffer-file-name)

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

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

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

(info "(elisp) Buffers")

Files

{Open, Append, Write} Files

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

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

;; 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