有没有办法在Emacs中重命名打开的文件?在我看的时候?类似于另存为,但原始文件应该删除。


当前回答

我知道这篇文章太老了,但在我找到的所有答案中,没有什么比Emacs在他们的教程中所说的更简单的了: M-x烘干(下水烘干) C-x C-q(在dired中切换为只读) 更改文件名,就像它只是文本文件中的一行一样: ::

  /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~

后:

  /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(保存更改) C-x C-k(终止缓冲区并返回到文件)。

其他回答

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

c - xd打开干燥 RET选择当前文件的目录 C-x C-j (Dired -跳转到当前文件的名称,在Dired中) R来重命名文件(或dired-do-rename)。 Q返回(重命名的)文件缓冲区

重命名相当于shell mv,但它也会更新任何打开的缓冲区,与mv不同的是,它不会改变文件系统中文件的访问和修改时间。

优秀的crux包具有crux-rename-file-and-buffer(以及许多其他有用的函数)。

下面是一个改编自史蒂夫的更健壮的版本。

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

这是另一个版本,它非常健壮,并且支持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)))))))

你可以在这里阅读更多信息。

我知道这篇文章太老了,但在我找到的所有答案中,没有什么比Emacs在他们的教程中所说的更简单的了: M-x烘干(下水烘干) C-x C-q(在dired中切换为只读) 更改文件名,就像它只是文本文件中的一行一样: ::

  /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~

后:

  /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(保存更改) C-x C-k(终止缓冲区并返回到文件)。