Elisp: Read/Write File

By Xah Lee. Date: . Last updated: .

Here's the most useful functions for working with file.

Emacs work with files via buffer. Typically, read/write file is always done via a buffer.

[see Elisp: Buffer Functions]

Open File

find-file → open a file.

(find-file FILENAME &optional WILDCARDS)

FILENAME can be a full path or just a file name.

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

Write File

write-region → the most useful function for writing buffer content to file.

(write-region START END FILENAME &optional APPEND VISIT LOCKNAME MUSTBENEW)

START and END are buffer positions, and FILENAME is a file path or name relative to default-directory.

It'll write the region text to the file.

;; write whole buffer to a file. overwrites the file content
(write-region (point-min) (point-max) "text.txt" )

save-buffer → save current buffer.

(save-buffer &optional ARG)

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

write-file → write buffer content into a new file, like “save as”, and open that file.

(write-file FILENAME &optional CONFIRM)

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

Append File

append-to-file → append text between 2 positions in current buffer to a file.

(append-to-file START END FILENAME)

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

Close File

kill-buffer → close buffer.

(kill-buffer &optional BUFFER-OR-NAME)

;; close a buffer
(kill-buffer myBuffName)

Create New File

There are several ways to create a file, depending on what you want to do exactly.

find-file can be used to create a new file.

(find-file FILENAME &optional WILDCARDS)

FILENAME can be a full path. if FILENAME does not exist, a new file will be created, but only when the buffer is saved.

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

Here's another way to create a file, with more control.

To create a file, just create a buffer, and save it to a file path.

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

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

;; close it
(kill-buffer newBuf)

[see Elisp: Buffer Functions]

with-temp-file

with-temp-file(with-temp-file FILE BODY).

Create a new buffer, make it current, evaluate BODY, and write the buffer to FILE. The value returned is the value of the last form in BODY.

;; create a file with text hello

(with-temp-file "test.txt"
  (insert "hello"))

(info "(elisp) Files")

Elisp Basic Functions

  1. Cursor
  2. Text Editing
  3. Get Buffer String
  4. String Functions
  5. Buffer
  6. Read/Write File
  7. File/Dir Path

If you have a question, put $5 at patreon and message me.
Or Buy Xah Emacs Tutorial
Or buy a nice keyboard: Best Keyboards for Emacs

Emacs

Emacs Lisp

Misc