Answers:
从Steve Yegge的.emacs尝试以下功能:
;; 来源:http://steve.yegge.googlepages.com/my-dot-emacs-file (defun重命名文件和缓冲区(新名称) “重命名当前缓冲区和文件以访问NEW-NAME。” (交互式“ sNew名称:”) (让[{name(buffer-name)) (文件名(缓冲文件名))) (如果(不是文件名) (消息“缓冲区'%s'不在访问文件!”名称) (如果(获取缓冲区新名称) (消息“已存在名为'%s'的缓冲区!”新名称) (前进 (重命名文件文件名新名称1) (重命名缓冲区新名称) (访问文件集的新名称) (set-buffer-modified-p nil))))))
看一下该页面,那里还有另一个非常有用的相关功能,称为“ move-buffer-file”。
(set-buffer-modified-p nil)
似乎没有必要。如果您调用rename-file-and-buffer
了已修改的缓冲区,然后尝试杀死它,它将很乐意执行此操作,而不会警告您未保存的更改。
是的,使用dired
模式,您可以:
C-x d
打开骰子RET
选择当前文件的目录C-x C-j
(dired-jump
到当前文件的名称,以Dired表示)R
重命名文件(或dired-do-rename
)。q
返回(重命名)文件缓冲区重命名等效于shell mv
,但它还将更新所有打开的缓冲区,并且与重命名不同,mv
它不会更改文件系统中文件的访问和修改时间。
C-x C-j
默认情况下不会对我的约束。这样做M-x load-library RET dired-x RET
首先使得它的约束。
C-x C-j
未绑定,另一种选择是M-x dired-jump
在第一次执行。它将自动加载dired-x
(这也将导致C-x C-j
从此刻开始定义)。
仅出于完整性考虑,由于某些人可能会访问此页面,以为他们会得到Emacs的“另存为”功能的答案,即打开文件的Cx Cw。
File > Save as...
我最喜欢的是来自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的正确版本。
这是来自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))
这是另一个版本,它非常健壮并且支持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)))))))
您可以在此处了解更多信息。
如果您使用的是Spacemacs,则可以免费获得此行为,因为该行为带有rename-current-buffer-file
(基于magnars)的实现,默认情况下绑定到SPC-f-R
。
有一种非常简单的方法,您可以按命令Mx,然后键入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)))))))
可以通过复制来实现。在文件上使用shift + c,emacs会要求您为包含文件名的路径指定一个名称,因此,您只需提供新名称,然后输入...当然,您必须删除前一个。
(setq uniquify-buffer-name-style 'forward)
如果您命名了缓冲区users\index.html
(因为您已经有另一个缓冲区posts\index.html
),则此方法与含义不兼容,重命名将失败