If I open a file in emacs, and the file subsequently gets moved or renamed by a different program, emacs will still save the file to the old location and name.

Example process:

  1. Open ex.txt in emacs.
  2. Make some changes to the file.
  3. From another terminal, move ex.txt to the dir/ subdirectory.
  4. In emacs, save the modified file.

Expected behavior: emacs realized the file was moved and saves the changes to the new file location.

Actual behavior: emacs saves the changes to the old filename and location. There are now two files, the old ex.txt in dir/ and the modified ex.txt in the original directory.

How can I get emacs to realize an open file has been moved and update the file in its new location?

  • 2
    How would emacs know where to look for the moved file?
    – nispio
    Oct 6, 2016 at 18:31

2 Answers 2


This doesn't answer the question as posed, but one option that you have for moving a file that is being visited by a buffer is to use dired. Files that are being visited by a buffer automatically have their buffer-file-name value updated when they are moved or renamed via dired.

As an example:

  • C-x C-f /tmp/testfile.txt to create a new file
  • Type some text in the buffer
  • C-x C-s To save the file
  • Type a little more text without saving
  • C-x d RET To open a dired buffer
  • Use arrows (or n and p) to move your cursor to testfile.txt
  • R newfile.txt to rename the file to newfile.txt
  • q to dismiss the dired buffer
  • Notice that the buffer name has changed to reflect the new file name
  • C-x C-s to save the unsaved changes to the new file location.

I doubt this would ever become the default behavior, but it's a pretty good idea for an optional one.

The real problem is implementing it. Linux currently provides two APIs that Emacs can use to get notifications about changes to files, inotify and fanotify. The newer API, fanotify, doesn't send notifications about file moves at all. inotify is happy to notify you that a file was moved or renamed, but it does so in a rather odd way. If you're watching the directory containing the file, inotify will tell you that the file was moved away. Unless you're already watching the destination, however, inotify won't tell you where the file was moved to.

I'm fairly certain that Windows and OSX have similar APIs, but since I don't actually do any programming for those platforms I don't really know much about them. I suggest that the first step towards implementing this would be to research these and see if it's doable. There are a few other packages for Emacs which are platform-specific, so don't let that stop you.

  • Emacs 24.4 and later offers filenotify.el, a package for handling fie notification evens coming from the underlying OS. It supports different backends for the platforms Emacs runs. For Linux, it supports inotify events. Oct 9, 2016 at 13:46

Your Answer

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

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