有没有办法在Emacs中重命名打开的文件?在我看的时候?类似于另存为,但原始文件应该删除。
当前回答
基于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)))))))
其他回答
如果您正在使用Spacemacs,那么您可以免费获得这种行为,因为它附带了一个重命名-current-buffer-file的实现(基于magnars),默认情况下绑定到src -f- r。
https://github.com/syl20bnr/spacemacs/blob/bd7ef98e4c35fd87538dd2a81356cc83f5fd02f3/layers/%2Bdistributions/spacemacs-base/funcs.el#L294
我知道这篇文章太老了,但在我找到的所有答案中,没有什么比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(终止缓冲区并返回到文件)。
有一个很简单的方法,你按下命令M-x,然后键入vc-rename-file,之后你只需要在目录中选择你当前的文件,然后选择新的名称。已更改文件的buff将被刷新。
来源:https://www.gnu.org/software/emacs/manual/html_node/emacs/VC-Delete_002fRename.html
基于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)))))))
我最喜欢的是来自Magnars (emacs岩石屏幕录制的名声)。
与其他选项不同的是,您不必从头输入名称—您可以获得要修改的当前名称。
(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的正确版本。