Elisp: Read File

By Xah Lee. Date: . Last updated: .

If you don't care about speed, use

to open a file into buffer. Then you can process it. and call save-buffer to save, like you would with using emacs.

(find-file "~/test.html")

If the file doesn't exit, it'll be created when you save it.

Note that find-file has lots side-effects. It will call all functions in find-file-hook. Many packages and major modes add stuff to that hook. It'll load a major mode (which does syntax coloring, usually very slow), it'll record undo for any modification in the buffer.

[see Emacs Lisp Text Processing: find-file vs with-temp-buffer]

Open File for Reading in Batch Script

To process thousands of files, read only, use with-temp-buffer.

(defun my-process-file (fPath)
  "Process the file at path FPATH …"
    (insert-file-contents fPath)
    ;; process it …

(info "(elisp) Current Buffer")

Read File as String or List of Lines

Elisp: Read File Content as String or List of Lines

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 Examples

ELisp Write Major Mode



Basic Functions

Writing Command

Writing Script

Lisp Data Structure

Lisp Symbol

Elisp Misc