Yes, what you want to do: visit the file in the buffer and operate on the text in this buffer.
You must not display buffer, ie, the user should not see him.
And as for efficiency: manipulating text in a buffer is usually the most efficient way to manage text.
You can visit the file in the buffer in several ways. You can use an existing file buffer for this, depending on the use case. That is, if the file is already βopenβ in Emacs, then you can use its buffer.
Or you can discard any existing file buffer for an already βopenβ file and read the file again into a new buffer. To do this, as @Sean mentions, you can use insert-file-contents with the generated buffer. You can create a buffer using with-temp-buffer or generate-new-buffer , depending, again, on what you need / need to do with it.
If you want to reuse a buffer that is already visiting the file, you can check if it has been changed in memory, if it has been narrowed, etc., and do whatever is appropriate for your use case. You can check if there is already a buffer by visiting the file (using any path / file name) using the find-buffer-visiting function.
To view a file using any existing buffer that visits it, you can use find-file-noselect . This function returns the buffer that the file visits, so you can pass this buffer as the first argument to with-current-buffer . Here is a simple example.
(with-current-buffer (let ((enable-local-variables ())) (find-file-noselect file)) ;; Do some stuff with the text in the buffer. ;; Optionally save the buffer back to the file. )
(Binding enable-local-variables with nil is a minor optimization for the normal case where you don't need to worry about local buffer variables.)
source share