1

Is there a way in emacs to open a file (or a dired directory) in a new buffer, even if there's a buffer with the same file (or directory)?

find-file and find-alternate-file always return the open buffer instead of creating a new one.

EDIT: The use case I have is that I am using dired-single to navigate through directories using a single buffer (so that new directories are opened in the same buffer using find-alternate-file). However, if I have two separate buffers for two different directories (which I do when copying files from directory to another, for example) and I navigate in one buffer to the directory of the other, I end up with a single buffer and two frames. I would instead like to have two separate buffers showing the same directory. Much like what happens when I explore directories in my file explorer.

6
  • 1
    Why do you want to do that? If you make changes in one buffer, what do you expect to happen in the other? Emacs is trying to protect you from screwing up your file by returning the old buffer, so trying to circumvent that behavior is probably a mistake. You might want to edit your question with what you are really trying to accomplish. – NickD May 28 '19 at 13:04
  • I added the use case I am trying to address. – Tohiko May 28 '19 at 13:22
  • 1
    I'm not quite getting it. What's the problem with having one buffer shown in two frames? Would a cloned buffer suffice for your purposes? – zck May 28 '19 at 13:42
  • I find myself in the following situation many times: I have two open dired buffers one at /A/B and one at /A. I want to copy a file from /A/C to /A. So I use the first buffer starting from /A/B and I hit up (calling dired-single-up-directory) to go to /A, intending to navigate to C within A. However, since I have /A already open, that buffer is just brought up I end with a single buffer. A file explorer allows multiple independent views of the same directory and I am trying to a achieve that with dired. – Tohiko May 28 '19 at 14:07
  • If you intend to navigate to A/C then why don't you just do that directly from A/B instead of first going up (^) to A? Just C-x d, remove the B from the inserted default directory name A/B, and hit RET. Or, if you also want to keep buffer A/B and its window, use C-x 4 d and do the same thing. It sounds like you are complicating things for yourself. – Drew May 28 '19 at 15:50
1

You can do the following to create an arbitrary number of buffers visiting the same file:

  • Create a new buffer
  • Set its buffer-file-name to the desired file's path
  • Probably also call rename-buffer to match (this is cosmetic)
  • Revert the buffer

Thusly created buffers differ from regular cloned buffers in that their contents is not shared. Thus, it can be used to e.g. see the file contents as it is on disk right now, as opposed to whatever is in the existing Emacs buffer.

Putting it all together:

(defun revisit-file ()
  "Open the file that the current buffer is visiting in a new buffer."
  (interactive)
  (let* ((fn buffer-file-name)
         (buf (create-file-buffer fn)))
    (with-current-buffer buf
      (setq buffer-file-name fn)
      (revert-buffer t t))
    (switch-to-buffer-other-window buf)))

However, this approach doesn't work with dired buffers, as they're not literally visiting a file.

Here, it is enough to trick dired into creating a new buffer by temporarily setting dired-buffers to nil:

(defun revisit-dired ()
  "Open the directory that the current buffer is visiting in a new dired buffer."
  (interactive)
  (switch-to-buffer-other-window
   (save-window-excursion
     (let (dired-buffers)
       (dired default-directory)))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.