Bleeding Fingers
Bleeding Fingers

Reputation: 7129

Open multiple instance of a file-

How can I open multiple different instances of file in Emacs? That is to say, the instances are totally independent of each other but write to the same file.

I actually want to have a reference to the original file in one buffers side-by-side with the buffer in which I will be editing while referring to the original content. I don't find opening a temporary buffer, yanking the entire original content into it and have it side-by-side, as an elegant solution.

Other solutions are welcome as well.

I have tried using clone-indirect-buffer and C-x C-v but it doesn't server the purpose.

Upvotes: 1

Views: 430

Answers (4)

Stefan
Stefan

Reputation: 28531

As mentioned, you might be able to use clone-buffer, although you'll have to let-bind buffer-file-name around the call since clone-buffer otherwise will refuse to clone it. Another option is to do:

M-x set-visited-file-name RET toto RET
C-x C-f thefile RET
C-x b RET
M-x set-visited-file-name RET thefile RET

the last set-visited-file-name should ask you if you really want to do that, but you can answer that you do and Emacs will accept your choice. Arguably, clone-buffer should not reject to do it, so you might like to submit a bug-report asking to make it behave similarly to what set-visited-file-name does.

Upvotes: 2

user797257
user797257

Reputation:

As I read through the find-file code, I see this warning coded into it:

"The file %s is already visited normally.
You have asked to visit it literally,
meaning no coding system decoding, format conversion, or local variables.
But Emacs can only visit a file in one way at a time.

Do you want to revisit the file literally now? "

Something that would imply that Emacs' code specifically tries to protect against the situation when the same file is visited multiple times, but the buffers are out of sync with each other. However... you could open two copies of Emacs, in which case they would not know about each other visiting the same file and so would allow this situation to happen.

I can understand that the above isn't a very nice option, but it looks like adding that kind of functionality will require some time understanding the reasons behind it being specifically prevented in the first place.

I've tried this:

M-:(switch-to-buffer (find-file-noselect-1 (create-file-buffer (buffer-file-name)) (buffer-file-name) t nil (buffer-file-name) 1))

And it seems like it would work, but I'm not sure of consequences - maybe different major modes may rely on the original Emacs treatment of files and their editing history, so use with care. The last number 1 is the number to be displayed after the file name, as in Foo.bar<1> So, you'd need to change that, if you need more copies.

Upvotes: 2

Chris Barrett
Chris Barrett

Reputation: 3375

You may have some luck with clone-buffer, depending on your mode. It has certain limitations that you can read about in the docs.

Otherwise, here's something quick and dirty:

(defun dodgy-clone-buffer ()
  "Clone the current buffer. The clone will write to the original file."
  (interactive)
  (switch-to-buffer-other-window
   (eval `(with-current-buffer
              ;; Create a new buffer or clear an existing one.
              (get-buffer-create ,(format "*clone: %s*" (buffer-name)))
            (delete-region (point-min) (point-max))
            (insert ,(buffer-string))
            (setq buffer-file-name ,(buffer-file-name))
            (funcall ',major-mode)
            (current-buffer)))))

Upvotes: 1

choroba
choroba

Reputation: 241758

You can create a new buffer (C-xb*new*) and insert the content of the file to it with C-xifilename.

Upvotes: 3

Related Questions