如果我C-xC-w (write-file)用来将当前缓冲区的内容写入新位置,那么之后我的缓冲区将访问新文件而不是原始文件.有时我想将缓冲区的副本保存到新位置,但随后在原始位置继续编辑.
我知道我可以从(write-file)迷你缓冲区历史记录中删除现有的文件名,然后在C-xC-f (find-file)提示符后立即将其重新访问以重新访问原始文件,这就是我现在使用的解决方法.然而,这感觉不够优雅.
我想知道是否(write-file)可能采用某种类型的前缀参数来访问缓冲区,但这只会影响覆盖确认.
那么:有没有更简单的方法将我的缓冲区内容保存到文件而不改变我正在访问的文件?
这是基于 Inaimathi 的回答和我对此的评论,以及一些额外的调整:
(defun my-write-copy-to-file ()
"Write a copy of the current buffer or region to a file."
(interactive)
(let* ((curr (buffer-file-name))
(new (read-file-name
"Copy to file: " nil nil nil
(and curr (file-name-nondirectory curr))))
(mustbenew (if (and curr (file-equal-p new curr)) 'excl t)))
(if (use-region-p)
(write-region (region-beginning) (region-end) new nil nil nil mustbenew)
(save-restriction
(widen)
(write-region (point-min) (point-max) new nil nil nil mustbenew)))))
(global-set-key (kbd "C-c w") 'my-write-copy-to-file)
Run Code Online (Sandbox Code Playgroud)
如果您定期进行此操作,以至于烦人,则可以定义此功能
(defun write-file-copy (filename)
(interactive "F")
(write-region (point-min) (point-max) filename))
Run Code Online (Sandbox Code Playgroud)
并将其绑定到对您有意义的东西上。
如果dired-x默认情况下需要,或以其他方式使dired-jump函数可用1,那么以下非常简单:
C-xC-jC (输入新名称) RETq
这是:
1 C-hig (dired-x) Optional Installation Dired Jump RET