如何在Emacs中重命名打开的文件?
有没有办法在Emacs中重命名打开的文件? 当我正在查看它? 像保存一样,但原来的应该消失。
试试Steve Yegge的.emacs中的这个函数:
;; 来源:http://steve.yegge.googlepages.com/my-dot-emacs-file (defun重命名文件和缓冲区(新名称) “重命名当前的缓冲区和文件访问到新名称”。 (互动的新名称:“) (let((name(buffer-name)) (filename(buffer-file-name))) (如果(不是文件名) (消息“缓冲区'%s'没有访问文件!”名称) (如果(获取缓冲区新名称) (消息“名为'%s'的缓冲区已经存在!”new-name) (progn这个 (重命名文件文件名新名称1) (重命名缓冲区新名称) (set-visited-file-name new-name) (set-buffer-modified-p nil))))))
看看那个页面,那里还有一个非常有用的相关函数,叫做“move-buffer-file”。
是的,在直接模式下,您可以:
-
Cx Cj
(直接dired-jump
到Dired中当前文件的名称) -
R
重命名该文件(或直接dired-do-rename
)。 -
Cx k RET
返回(重命名)的文件缓冲区
重命名相当于一个shell mv
,但它也会更新任何打开的缓冲区。
只是为了完整性,因为有些人可能会访问这个页面,认为他们会得到一个Emacs的“另存为”function的答案,这就是打开文件的Cx Cw。
我最喜欢的是来自Magnars( emacs rocks screencasts fame)的。
与其他替代scheme不同,您不必从头开始input名称 – 您可以修改当前名称。
(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提供的正确版本。
这是从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 Cz Cz 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 tt)))))))
你可以在这里阅读更多。
基于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)))))))
有一种非常简单的方法,即按下命令Mx,然后键入vc-rename-file ,之后只需在目录中select当前文件,然后再select新的名称。 有更改文件的buff将被刷新。
资料来源: https : //www.gnu.org/software/emacs/manual/html_node/emacs/VC-Delete_002fRename.html
它可以通过复制来实现。 shift + c文件,emacs会要求你指定包含文件名的path名,所以你只需要提供新的名字,然后input…当然,你必须删除前一个。