Previous: , Up: Directories—Files—Buffers   [Index]


7.5 Insert File Contents

In a Lisp program, if you want to look at the contents of a file but not alter it, the fastest way is to use ‘insert-file-contents’ in a temporary buffer. Visiting the file is not necessary and takes longer.

insert-file ‘C-x i

This function is for interactive use only; in Lisp code use insert-file-contents instead

insert-file-contents
Function: insert-file-contents filename &optional visit end replace

Insert contents of file FILENAME after point. Returns list of absolute file name and number of characters inserted.

  • VISIT if non-nil, the buffer’s visited filename and last save file modtime are set, and it is marked unmodified.
  • BEG and END specify what portion of the file to insert. These arguments count bytes in the file, not characters in the buffer. If VISIT is non-nil, BEG and END must be nil.
  • REPLACE if non-nil, replace the current buffer contents (in the accessible portion) with the file contents. This is better than simply deleting and inserting the whole thing because (1) it preserves some marker positions and (2) it puts less data in the undo list. When REPLACE is non-nil, the second return value is the number of characters that replace previous buffer contents.