1

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.

4
  • Hmmm. If your "elegant" solution was provided, it would confuse the hell out of the majority of users. Think about implementing an editor and how your buffers should relate to files, then realize what you're asking is craaaaaaaaaaaazy. Commented May 14, 2013 at 10:40
  • @event_jr true. I do have a vague sense of the complications this would incur. But hey, this is Emacs. Commented May 14, 2013 at 10:56
  • hus787: To clarify, you genuinely want to be able to write to/clobber the file from multiple buffers, each with independent content? You can already do this (e.g. open a file buffer, then create a new buffer and write it to the same filename), but Emacs will throw loads of sensible warnings at you when you edit and save them. Are you saying you want to be able to circumvent all the warnings in this situation? Commented May 17, 2013 at 1:05
  • @phils well the editing part wasn't actually intended but could be. Although I wouldn't want to circumvent those warnings but a how to achieve that would be a plus Commented May 17, 2013 at 22:00

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

Suffice. But any luck on the original question?
2

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.

Comments

2

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.

Comments

1

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)))))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.