How do I rename an open file in Emacs?
EmacsEmacs Problem Overview
Is there a way to rename an open file in Emacs? While I'm viewing it? Something like save-as, but the original one should go away.
Emacs Solutions
Solution 1 - Emacs
Yes, with dired
mode, you can:
C-x d
to open diredRET
to select directory of current fileC-x C-j
(dired-jump
to the name of the current file, in Dired)R
to rename the file (ordired-do-rename
).q
to go back to the (renamed) file buffer
The rename is equivalent to a shell mv
, but it will also update any open buffers, and unlike mv
it will not change the access and modify times on the file in the filesystem.
Solution 2 - Emacs
Just for completeness, since some folks may visit this page thinking they will get an answer for the "save as" feature of Emacs, that's C-x C-w for an open file.
Solution 3 - Emacs
Try this function from Steve Yegge's .emacs:
;; source: http://steve.yegge.googlepages.com/my-dot-emacs-file (defun rename-file-and-buffer (new-name) "Renames both current buffer and file it's visiting to NEW-NAME." (interactive "sNew name: ") (let ((name (buffer-name)) (filename (buffer-file-name))) (if (not filename) (message "Buffer '%s' is not visiting a file!" name) (if (get-buffer new-name) (message "A buffer named '%s' already exists!" new-name) (progn (rename-file filename new-name 1) (rename-buffer new-name) (set-visited-file-name new-name) (set-buffer-modified-p nil))))))
Take a look at that page, there's another really useful related function there, called "move-buffer-file".
Solution 4 - Emacs
My favorite is the one from Magnars (of emacs rocks screencasts fame.)
Unlike the other alternatives, you don't have to type the name out from scratch - you get the current name to modify.
(defun rename-current-buffer-file ()
"Renames current buffer and file it is visiting."
(interactive)
(let* ((name (buffer-name))
(filename (buffer-file-name))
(basename (file-name-nondirectory filename)))
(if (not (and filename (file-exists-p filename)))
(error "Buffer '%s' is not visiting a file!" name)
(let ((new-name (read-file-name "New name: " (file-name-directory filename) basename nil basename)))
(if (get-buffer new-name)
(error "A buffer named '%s' already exists!" new-name)
(rename-file filename new-name 1)
(rename-buffer new-name)
(set-visited-file-name new-name)
(set-buffer-modified-p nil)
(message "File '%s' successfully renamed to '%s'"
name (file-name-nondirectory new-name)))))))
Thanks to James Yang for a correct version.
Solution 5 - Emacs
Here's a more robust version adapted from stevey.
;; Originally from stevey, adapted to support moving to a new directory.
(defun rename-file-and-buffer (new-name)
"Renames both current buffer and file it's visiting to NEW-NAME."
(interactive
(progn
(if (not (buffer-file-name))
(error "Buffer '%s' is not visiting a file!" (buffer-name)))
;; Disable ido auto merge since it too frequently jumps back to the original
;; file name if you pause while typing. Reenable with C-z C-z in the prompt.
(let ((ido-auto-merge-work-directories-length -1))
(list (read-file-name (format "Rename %s to: " (file-name-nondirectory
(buffer-file-name))))))))
(if (equal new-name "")
(error "Aborted rename"))
(setq new-name (if (file-directory-p new-name)
(expand-file-name (file-name-nondirectory
(buffer-file-name))
new-name)
(expand-file-name new-name)))
;; Only rename if the file was saved before. Update the
;; buffer name and visited file in all cases.
(if (file-exists-p (buffer-file-name))
(rename-file (buffer-file-name) new-name 1))
(let ((was-modified (buffer-modified-p)))
;; This also renames the buffer, and works with uniquify
(set-visited-file-name new-name)
(if was-modified
(save-buffer)
;; Clear buffer-modified flag caused by set-visited-file-name
(set-buffer-modified-p nil)))
(setq default-directory (file-name-directory new-name))
(message "Renamed to %s." new-name))
Solution 6 - Emacs
Here's another version, that's pretty robust and VC aware:
(defun rename-file-and-buffer ()
"Rename the current buffer and file it is visiting."
(interactive)
(let ((filename (buffer-file-name)))
(if (not (and filename (file-exists-p filename)))
(message "Buffer is not visiting a file!")
(let ((new-name (read-file-name "New name: " filename)))
(cond
((vc-backend filename) (vc-rename-file filename new-name))
(t
(rename-file filename new-name t)
(set-visited-file-name new-name t t)))))))
You can read more about it here.
Solution 7 - Emacs
If you're using Spacemacs then you get this behavior for free since it comes with an implementation of rename-current-buffer-file
(based on magnars) that by default bound to SPC-f-R
.
Solution 8 - Emacs
There is a way very easy, you press the command M-x and than type vc-rename-file, after that you just need to select your current file at the directory, and than choose the new name. The buff that has the changed file will be refreshed.
**Source:**https://www.gnu.org/software/emacs/manual/html_node/emacs/VC-Delete_002fRename.html
Solution 9 - Emacs
based on magnars version, I modified as below, fixed the INIT file name part:
(defun rename-current-buffer-file ()
"Renames current buffer and file it is visiting."
(interactive)
(let* ((name (buffer-name))
(filename (buffer-file-name))
(basename (file-name-nondirectory filename)))
(if (not (and filename (file-exists-p filename)))
(error "Buffer '%s' is not visiting a file!" name)
(let ((new-name (read-file-name "New name: " (file-name-directory filename) basename nil basename)))
(if (get-buffer new-name)
(error "A buffer named '%s' already exists!" new-name)
(rename-file filename new-name 1)
(rename-buffer new-name)
(set-visited-file-name new-name)
(set-buffer-modified-p nil)
(message "File '%s' successfully renamed to '%s'"
name (file-name-nondirectory new-name)))))))
Solution 10 - Emacs
Emacs 26.3 (2020-04-03) has rename-file
function which can be invoked using M-x rename-file
for renaming the current file or any other file for that matter.
Solution 11 - Emacs
The excellent crux
package has crux-rename-file-and-buffer
(along with many other useful functions).
Solution 12 - Emacs
It can be achieved by copy. shift+c on the file and emacs will ask you to denote a name for the path including the file name, so you just provide the new name,and enter...of course, you have to Delete the former one.