有没有办法在Emacs中重命名打开的文件?在我看的时候?类似于另存为,但原始文件应该删除。
当前回答
我没有发现任何建议的解决方案足以满足我的需求(缓冲区选择,覆盖确认,快速响应,实际工作等),以下是我正在使用的:
(defun rename-buffer-and-file (buffer newname)
(interactive "bRename buffer and its visiting file: \nFNew name: ")
(let ((oldname (buffer-file-name)))
(if (not oldname)
(message "Buffer '%s' is not visiting a file" buffer)
(if (file-exists-p newname)
(if (file-directory-p newname)
;; Signal an error if the user specified the name of an
;; existing directory.
(error "%s is a directory" newname)
(unless (y-or-n-p (format-message
"File `%s' exists; overwrite? "
newname))
(error "Canceled"))))
;; Rename buffer and its visiting file
(set-visited-file-name newname)
;; Delete old file
(delete-file oldname)
(save-buffer))))
绑定到C-x - C-r以便于
(global-set-key (kbd "C-x C-r") 'rename-buffer-and-file)
其他回答
我没有发现任何建议的解决方案足以满足我的需求(缓冲区选择,覆盖确认,快速响应,实际工作等),以下是我正在使用的:
(defun rename-buffer-and-file (buffer newname)
(interactive "bRename buffer and its visiting file: \nFNew name: ")
(let ((oldname (buffer-file-name)))
(if (not oldname)
(message "Buffer '%s' is not visiting a file" buffer)
(if (file-exists-p newname)
(if (file-directory-p newname)
;; Signal an error if the user specified the name of an
;; existing directory.
(error "%s is a directory" newname)
(unless (y-or-n-p (format-message
"File `%s' exists; overwrite? "
newname))
(error "Canceled"))))
;; Rename buffer and its visiting file
(set-visited-file-name newname)
;; Delete old file
(delete-file oldname)
(save-buffer))))
绑定到C-x - C-r以便于
(global-set-key (kbd "C-x C-r") 'rename-buffer-and-file)
我最喜欢的是来自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的正确版本。
这是另一个版本,它非常健壮,并且支持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)))))))
你可以在这里阅读更多信息。
下面是一个改编自史蒂夫的更健壮的版本。
;; 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))
试试Steve Yegge的.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”。