0

Is there a way to make Emacs visit already visited file(with active buffer) via symbolic-link as a different file? I want them(links/file paths) to be visited by different buffers. It will be best if this behavior can be controlled with a directory local variable.

Why I need it:

I have a bunch of source files under actual project root source directory with a project file (it configures build tools) for production builds. But I also have those files under a different root (for development environment) The whole thing resides in the same git repo and has .dir-locals.el with:

((nil . ((find-file-visit-truename . nil)
         (vc-follow-symlinks       . nil))))

It works fine (despite obvious problems with version control). The only thing that is really inconvenient is that I sometimes visit source files via production root paths and then Emacs always opens them via the paths and it messes up with development tools/Emacs integration. So I have to kill the buffer and re-visit files under develop.

  • This is most likely an XY-problem. Maybe it would be better if you wrote what you actually want to achieve. Two buffers for one file is a sure way to loose edits if the buffers are not generally read-only. Note that you can edit both buffers independently. It may be that you need an ediff merge after a while. – Tobias Apr 24 at 12:00
  • Note that there are methods for having two buffers for the same file: clone-buffer-indirect, jac available as library on github. – Tobias Apr 24 at 12:17
  • @Tobias What if the second buffer (if a file already visited via different link) will be opened in a indirect-buffer? – JAre Apr 24 at 16:11
  • @Tobias added explanation why I need it. – JAre Apr 24 at 16:30
4

From File Aliases:

Normally, if you visit a file which Emacs is already visiting under a different name, Emacs displays a message in the echo area and uses the existing buffer visiting that file. This can happen on systems that support hard or symbolic links, or if you use a long file name on a system that truncates long file names, or on a case-insensitive file system. You can suppress the message by setting the variable find-file-suppress-same-file-warnings to a non-‘nil’ value. You can disable this feature entirely by setting the variable find-file-existing-other-name to ‘nil’: then if you visit the same file under two different names, you get a separate buffer for each file name.

If the variable find-file-visit-truename is non-nil, then the file name recorded for a buffer is the file's truename (made by replacing all symbolic links with their target names), rather than the name you specify. Setting find-file-visit-truename also implies the effect of find-file-existing-other-name.

For future reference: this is the first node that comes up in the Emacs Info manual when you type i symbolic

  • I added the find-file-visit-truename bit since one can stumble over it. – Tobias Apr 24 at 11:55
  • I was hoping to convince OP to read the original, either on the web or inside emacs :-) – rpluim Apr 24 at 11:59
  • I added find-file-existing-other-name . nil to my .dir-locals.el file and it work. Thx – JAre Apr 24 at 22:49

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.