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 …"
  (with-temp-buffer
    (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

Elisp, Writing Scritpts

  1. Run Elisp Script in Shell
  2. Get Command Line Args
  3. Read File
  4. Read File Content as List
  5. Write File
  6. Print, Output
  7. Format String
  8. Walk Dir
  9. Call Shell Command
  10. Get Script Name at Run Time
Patreon me $5 patreon

Or Buy Xah Emacs Tutorial

Or buy a nice keyboard: Best Keyboard for Emacs

If you have a question, put $5 at patreon and message me.