Moving a File in Emacs

Sometimes you want to move a file you have open in Emacs. More often, perhaps, you simply want to rename the file. Emacs has some functions that do this but none of them are entirely satisfactory. The write-file function comes close but it leaves the old file in place and the rename functions don’t take care of the buffer. Zachary Kanfer has an interesting bit of Elisp that does the right thing.

His code copies the file to a new location (or name) using write-file so that the buffer name is changed and any changes later saved will be written to the new file. If the move is successful, the old file is deleted.

This seems pretty simple and it is but it’s surprising how often it comes up. If you find yourself in this situation more than once it’s probably worth your while to add Kanfer’s code to your init.el.

This entry was posted in General and tagged . Bookmark the permalink.