How do I rename an open file in Emacs?

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.


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


Yes, with dired mode, you can:

  • Cx Cj ( dired-jump to the name of the current file, in Dired)
  • R to rename the file (or dired-do-rename ).
  • Cx k RET to go back to the (renamed) file buffer
  • The rename is equivalent to a shell mv , but it will also update any open buffers.


    只是为了完整性,因为有些人可能会访问这个页面,认为他们会得到Emacs的“另存为”功能的答案,这就是打开文件的Cx Cw。

    链接地址: http://www.djcxy.com/p/38566.html

    上一篇: 如何用Emacs中的换行符替换一个字符?

    下一篇: 我如何在Emacs中重命名打开的文件?