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
(find-file FILENAME &optional WILDCARDS)
Open a file. 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
(write-region START END FILENAME &optional APPEND VISIT LOCKNAME MUSTBENEW)
The most useful function for writing buffer content to file.
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-buffer &optional ARG)
Save current buffer.
;; save current buffer (write to the associated file)
(save-buffer)
write-file
(write-file FILENAME &optional CONFIRM)
Write buffer content into a new file, like “save as”, and open that file.
;; like “Save As”. Save current buffer, close it, and open the new saved
(write-file "~/new.txt")

Append File

append-to-file
(append-to-file START END FILENAME)
Append text between 2 positions in current buffer to a file.
;; append text between positions 100 to 200 to file
(append-to-file 100 200 "~/test.txt")

Close File

kill-buffer
(kill-buffer &optional BUFFER-OR-NAME)
Close buffer.
;; 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
(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")

If you have a question, put $5 at patreon and message me on xah discord.
Or support me by Buy Xah Emacs Tutorial

Emacs Tutorial

Emacs Init

Emacs Keys

ELisp

ELisp Examples

ELisp Write Major Mode


ELisp

Basics

Basic Functions

Writing Command

Writing Script

Lisp Data Structure

Lisp Symbol

Elisp Misc