reddit / emacs

Change FILE of a buffer

reliktary · 2022-10-18 13:03
Hi everyone,I have an interactive function that moves currently opened file into different folder.I do it with `rename-file`. The issue is that when the file is moved, the buffer opened doesn't really know about it and after any change in the buffer and then save, the file gets saved into old location. How do I associate a buffer with new location? I found somewhere that I can use \`write-file\` function and it suppose to change "the pointer" to file but it didn't. It does write to specified location but `save-buffer` still saves it into old file. Thanks
akirakom · 2022-10-18 13:09
Use `vc-rename-file` or `crux-rename-file-and-buffer` from [crux](https://github.com/bbatsov/crux)
reliktary · 2022-10-18 13:33
I ended up not using any of those but their internals helped me to find `set-visited-file-name` which does exactly what I'm looking for. Thanks a lot!
Bodertz · 2022-10-18 18:05
`write-file` uses `set-visited-file-name`, so it should have done what you want.
reliktary · 2022-10-18 19:42
Yes, you're right. I called it with wrong path.
SaltyMycologist8 · 2022-10-18 20:49
I use a custom function modified from Steve Yegge's config https://github.com/bcmertz/dotfiles/blob/main/.emacs.d/lisp/custom-funcs.el#L332 Uses `set-visited-file-name` as you discussed below, but adds some useful projectile side effects to update the projectile cache if you use that library