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.
15 Answers
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.

- 397
- 1
- 11

- 55,321
- 43
- 129
- 155
-
14That's not directly renaming the current file. – Pablo Fernandez Dec 21 '08 at 17:21
-
5C-x b and you're back in the original buffer. You could write an Elisp function to do it, but I doubt you'll save many keystrokes with it. – Chris Conway Dec 22 '08 at 01:49
-
6Also, rather than C-x b, you can press C-x k to be back in the original buffer. – Yoo Oct 14 '10 at 12:55
-
1As to how to skip searching for the file from the dired buffer, see http://stackoverflow.com/questions/3933484/open-dired-and-select-the-file-associated-with-the-previous-buffer – Yoo Oct 15 '10 at 16:03
-
3Thanks for this answer. It's simpler than adding functions. As for "That's not directly renaming the current file", it seems to do just that. The file is renamed on the disk and the buffer. – titanofold Aug 08 '13 at 12:30
-
37The `C-x C-j` is not bound by default for me. Doing `M-x load-library RET dired-x RET` first makes it bound. – ntc2 Oct 31 '13 at 17:17
-
1To me this is the best answer as the concept of "direct" action becomes quite fuzzy when we're talking of a few keystrokes. Usually `quit-window` is bound to `q` so it's just a matter of `C-x C-j R [filename] RET q`. How "indirect" is this in practice? – Francesco Napolitano Jun 16 '16 at 14:17
-
5Another alternative if `C-x C-j` is unbound is just do `M-x dired-jump` on the first time. It will automatically load `dired-x` (which will also cause `C-x C-j` to be defined from this point on). – Fernando Basso Aug 11 '16 at 11:26
-
Completing the `dired-do-rename` step certainly renames the file. I am not sure what @pupeno is objecting to. – ctpenrose Dec 04 '20 at 05:14
-
I did not have to do the `C-x C-j` at all. `dired` brought me to the name of the current file and `R` let me rename it. I suspect `emacs` has made that more convenient in modern versions. – traday Dec 20 '21 at 14:50
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.

- 1,343
- 1
- 8
- 2
-
-
Not quite "save as", since the file you are editing will still be the original one. – asmeurer Jun 21 '13 at 22:31
-
14@asmeurer You are wrong! After saving, you will be editing the new file. – Joel G Mathew Aug 22 '13 at 14:23
-
3
-
8Perhaps asmeurer meant, "the file you [were] editing will still [exist]". Then, is that correct? I would check, but then, you cannot downvote my comment, hahaha. – Brady Trainor Mar 19 '14 at 04:47
-
-
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".

- 24,651
- 6
- 70
- 114

- 23,168
- 8
- 60
- 63
-
1Note: this method is not compatible with `(setq uniquify-buffer-name-style 'forward)` meaning if you have buffer named `users\index.html` (because you already have another buffer for `posts\index.html`) the renaming will fail – Ev Dolzhenko Sep 27 '12 at 11:49
-
`(set-buffer-modified-p nil)` seems unnecessary. If you called `rename-file-and-buffer` on a modified buffer and then attempted to kill it, it would happily do it without warning you about unsaved changes. – roldugin Jul 11 '13 at 07:19
-
This function will also (somewhat annoyingly) ask you for a new name *before* checking whether the current buffer is associated with a file at all (in which case it aborts). – Thomas Sep 11 '15 at 12:04
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.

- 29,987
- 31
- 114
- 156
-
This is NOT working properly. This one adds a "/" at the end of file. – mythicalcoder Mar 23 '17 at 18:57
-
2Thanks for posting that out for me. The SO way is to correct answers, not post new, slightly modified ones. – The Unfun Cat Mar 24 '17 at 07:54
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))

- 726
- 8
- 9
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.

- 55,802
- 13
- 100
- 117
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
.

- 4,485
- 4
- 48
- 56
-
2I've just tested this in Doom Emacs (v27.1) and it works as well. Thank you. – flarkmarup Feb 23 '21 at 14:59
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

- 91
- 1
- 4
-
1This has the (not necessarily desirable) side effect of adding the old and new files to be committed into version control. But +1 because it uses a builtin function to (mostly) answer the OP's question. – dinosaur Aug 28 '17 at 00:43
-
Works even today with GNU Emacs 25.3.2 (x86_64-pc-linux-gnu, GTK+ Version 3.18.9) – agent18 Sep 28 '20 at 20:15
-
1Got this when I used `vc-rename-file` please update files before moving them` – agent18 Dec 29 '21 at 01:16
-
Indeed, it's a neat solution but it doesn't always allow you to do the renaming. – pglpm Jun 16 '23 at 17:53
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.

- 1,813
- 17
- 23
-
2In Emacs 25 this will not change the name of the file that is in buffer, i.e. the next write will go to the old-filename again. – Christian Herenz Sep 21 '21 at 14:42
-
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)))))))

- 1,306
- 4
- 15
- 25
-
This one is better. The magnars version given above is NOT working properly. Add a "/" character at the end – mythicalcoder Mar 23 '17 at 18:57
I know the post is sooo old but in all the answer I have found, nothing is more simple that what said Emacs in their tutorial:
M-x Dired
(launch dired)
C-x C-q
(toggle read only in dired)
change the filename like if it was just a line in a text file:
prev:
/home/okiw4n/Documents/project/light_script:
total used in directory 20 available 339.2 GiB
drwxr-xr-x. 2 okiw4n okiw4n 4096 Sep 7 09:07 .
drwxr-xr-x. 3 okiw4n okiw4n 4096 Sep 4 10:03 ..
-rw-r--r--. 1 okiw4n okiw4n 3594 Sep 7 08:39 explication.org
-rw-r--r--. 1 okiw4n okiw4n 85 Sep 4 10:09 script.sh
-rw-r--r--. 1 okiw4n okiw4n 3594 Sep 5 20:08 tuc.org~
after:
/home/okiw4n/Documents/project/light_script:
total used in directory 20 available 339.2 GiB
drwxr-xr-x. 2 okiw4n okiw4n 4096 Sep 7 09:07 .
drwxr-xr-x. 3 okiw4n okiw4n 4096 Sep 4 10:03 ..
-rw-r--r--. 1 okiw4n okiw4n 3594 Sep 7 08:39 explication.org
-rw-r--r--. 1 okiw4n okiw4n 85 Sep 4 10:09 script.sh
-rw-r--r--. 1 okiw4n okiw4n 3594 Sep 5 20:08 TRUC.org~ (I have rewrite this file)
C-x C-s
(for save the changes)
C-x C-k
(kill the buffer and return to the file).

- 21
- 1
I didn't find any of the proposed solutions sufficient for my needs (buffer selection, overwrite confirmation, fast response, actually working etc.), here's what I'm using:
(defun rename-buffer-and-file (buffer newname)
(interactive "bRename buffer and its visiting file: \nFNew name: ")
(let ((oldname (buffer-file-name)))
(if (not oldname)
(message "Buffer '%s' is not visiting a file" buffer)
(if (file-exists-p newname)
(if (file-directory-p newname)
;; Signal an error if the user specified the name of an
;; existing directory.
(error "%s is a directory" newname)
(unless (y-or-n-p (format-message
"File `%s' exists; overwrite? "
newname))
(error "Canceled"))))
;; Rename buffer and its visiting file
(set-visited-file-name newname)
;; Delete old file
(delete-file oldname)
(save-buffer))))
bound to C-x C-r
for convenience with
(global-set-key (kbd "C-x C-r") 'rename-buffer-and-file)

- 71
- 1
- 3
Emacs 29.1 added rename-visited-file
command which does exactly this, without a key binding though.
From its NEWS:
** New command 'rename-visited-file'. This command renames the file visited by the current buffer by moving it to a new name or location, and also makes the buffer visit this new file.

- 10,547
- 4
- 53
- 66
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.

- 227
- 1
- 4
- 13
-
2You seem to be referring to "C" in the dired mode? That's copying the file, not renaming it, which (as @ChrisConway noted) in dired is done with "R". And besides, OP asked for a rename of the current buffer. – Davor Cubranic Jan 13 '15 at 19:33