如何在Emacs中重命名打开的文件?

时间:2008-12-21 09:19:42

标签: emacs

有没有办法在Emacs中重命名打开的文件?在我看的时候?像save-as之类的东西,但最初的东西应该消失。

12 个答案:

答案 0 :(得分:318)

是的,使用dired模式,您可以:

  • C-x d打开dired
  • RET选择当前文件的目录
  • C-x C-jdired-jump到当前文件的名称,在Dired中)
  • R重命名文件(或dired-do-rename)。
  • q返回(重命名)文件缓冲区

重命名相当于shell mv,但它也会更新任何打开的缓冲区。

答案 1 :(得分:119)

为了完整起见,因为有些人可能会访问此页面,认为他们会得到Emacs的“另存为”功能的答案,那就是打开文件的C-x C-w。

答案 2 :(得分:84)

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

看看那个页面,那里有另一个真正有用的相关功能,名为“move-buffer-file”。

答案 3 :(得分:23)

我最喜欢的是Magnars(emacs rocks screencasts成名)。

与其他替代方案不同,您不必从头开始输入名称 - 您将获得要修改的当前名称。

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

感谢James Yang的正确版本。

答案 4 :(得分:14)

这是一个更强大的版本改编自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))

答案 5 :(得分:10)

这是另一个版本,非常强大且可识别VC:

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

您可以详细了解here

答案 6 :(得分:4)

有一种方法非常简单,你按下命令 Mx 而不是输入 vc-rename-file ,之后你只需要选择你当前的文件。目录,然后选择新名称。具有更改文件的buff将被刷新。

<强>来源: https://www.gnu.org/software/emacs/manual/html_node/emacs/VC-Delete_002fRename.html

答案 7 :(得分:2)

基于magnars版本,我修改如下,修复了INIT文件名部分:

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

答案 8 :(得分:2)

优秀的 crux package 具有 crux-rename-file-and-buffer(以及许多其他有用的功能)。

答案 9 :(得分:1)

如果你正在使用Spacemacs,那么你会免费获得这种行为,因为它带有rename-current-buffer-file(基于magnars)的实现,默认绑定到SPC-f-R

https://github.com/syl20bnr/spacemacs/blob/bd7ef98e4c35fd87538dd2a81356cc83f5fd02f3/layers/%2Bdistributions/spacemacs-base/funcs.el#L294

答案 10 :(得分:0)

Emacs 26.3(2020-04-03)具有rename-file函数,可以使用M-x rename-file调用此函数来重命名当前文件或与此相关的任何其他文件。

答案 11 :(得分:-1)

可以通过复制来实现。文件上的shift + c和emacs将要求您表示路径的名称,包括文件名,因此您只需提供新名称,然后输入...当然,您必须删除前一个。