7

I want to rename a file that is bound current buffer in Emacs.

I found following elisp from this article: How do I rename an open file in 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))))))

It works fine, but if possible I want to set current file name as a default value. How would I write this?

Community
  • 1
  • 1
ironsand
  • 14,329
  • 17
  • 83
  • 176

3 Answers3

5

Could you elaborate more on why you need to do it? What are the conditions? Because this has never come up since I started using Emacs.

But here's what I do sometimes when I want to rename something:

  1. Say I'm editing spam-spom-spam.cc. And I want to fix the name.
  2. C-x d
  3. C-s spo
  4. C-x C-q
  5. DEL a
  6. C-c C-c

This may seem like a lot of commands, but they flow quite naturally. As a bonus, you get an overview of your directory and of how the newly renamed file looks there.

abo-abo
  • 20,038
  • 3
  • 50
  • 71
  • 2
    see `dired-jump` from "dired-x" – event_jr Jul 24 '13 at 10:23
  • Thanks, that is useful. I had instead a shortcut for `(dired default-directory)`, but this is a bit better. – abo-abo Jul 24 '13 at 10:51
  • Thanks for your reply. I want to change file name when I start to write a shell script but afterwards I want to change the name of it. And I still writing the script. – ironsand Jul 25 '13 at 00:58
5

There recently was a post on the Emacs Redux blog on this subject.

Basically it is implemented in the Prelude configuration (by the same author) which you can install to get this behaviour and tons of other interesting stuff. Otherwise, you can put in your configuration file only the relevant snippet (taken from the blog post above):

(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)))))))

(global-set-key (kbd "C-c r")  'rename-file-and-buffer)
François Févotte
  • 19,520
  • 4
  • 51
  • 74
  • Edited the original function to check if the file with the new name is existed or not. According to https://rejeep.github.io/emacs/elisp/2010/03/26/rename-file-and-buffer-in-emacs.html – CodyChan Jul 01 '17 at 11:29
4

No external tool needed for this. See

http://www.gnu.org/software/emacs/manual/html_node/emacs/Wdired.html

After changing the file-name in the dired-buffer, the buffer-name is changed too.

Andreas Röhler
  • 4,804
  • 14
  • 18