tags:

views:

1237

answers:

5

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.

A: 

Did you try google? See if this helps.

Artelius
+6  A: 

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 name 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".

Matt Curtis
+12  A: 

Go to dired mode (C-x d or just open the directory using C-c C-f) and rename the file (R or dired-do-rename). This is equivalent to a shell mv, but will also update any open buffers.

Chris Conway
That's not directly renaming the current file.
J. Pablo Fernández
C-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
Also, rather than C-x b, you can press C-x k to be back in the original buffer.
RamyenHead
As 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
RamyenHead
+2  A: 

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.

Jim G
C-x C-w or use the menu `File > Save as...`
RamyenHead
+1  A: 

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)))
     (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)))
  ;; If the file isn't saved yet, skip the file rename, but still update the
  ;; buffer name and visited file.
  (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))
  (message "Renamed to %s." new-name)))
Shawn Hoover