如果我使用 Cx Cw (write-file)
将当前缓冲区的内容写入新位置,那么之后我的缓冲区将访问新文件而不是原来的。有时我想将缓冲区的副本保存到新位置,但随后继续在原始位置编辑 。
我知道我可以从(write-file)
迷你缓冲区历史记录中删除现有文件名,然后在 Cx Cf (find-file)
提示符后面回来之后立即重新访问原始文件,这是我目前使用的解决方法。然而,这感觉不够优雅。
我想知道(write-file)
是否可能采用某种类型的前缀参数来访问缓冲区,但这只会影响覆盖确认。
那么:有没有更简单的方法将我的缓冲区内容保存到文件而不用更改我正在访问的文件?
答案 0 :(得分:20)
使用 C-x h 选择整个缓冲区,然后使用 M-x write-region
。
答案 1 :(得分:4)
如果您默认需要dired-x
,或者使dired-jump
功能可用 1 ,那么以下内容非常简单:
Cx Cj C (输入新名称) RET q
这是:
1 Ch i g (dired-x) Optional Installation Dired Jump
RET
答案 2 :(得分:3)
如果你经常这样做,这对你来说很烦人,你可以定义这个功能
(defun write-file-copy (filename)
(interactive "F")
(write-region (point-min) (point-max) filename))
并将其绑定到对您有意义的内容。
答案 3 :(得分:2)
这是基于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)
答案 4 :(得分:1)
读完phils' (dired-jump)
回答,对于那些不太熟悉Dired的人,我发现了以下方法:
M - !
cp FILE NEWNAME
RET
作为奖励,您可以使用标签页,并且您已经在FILE
目录中,因此输入第一个文件名非常快捷。
答案 5 :(得分:1)
这是基于@phil 的响应,适合从 Lisp 以编程方式调用。有关详细信息,请参阅文档字符串。
(defun my-backup-buffer-or-region (&optional buffer-or-name file beg end)
"Write copy of BUFFER-OR-NAME between BEG and END to FILE.
BUFFER-OR-NAME is either a buffer object or name. Uses current
buffer when none is passed. Uses entire buffer for region when
BEG and END are nil. Prompts for filename when called
interactively. Will always ask before overwriting. Returns the
name of the file written to.
See URL `https://stackoverflow.com/a/18780453/5065796'."
(interactive)
(let* ((buffer-or-name (or buffer-or-name (current-buffer)))
(buffo (or (get-buffer buffer-or-name) (error "Buffer does not exist"))) ; buffer object
(buffn (or (buffer-file-name buffo) (buffer-name buffo))) ; buffer name
(beg (or beg (if (use-region-p) (region-beginning) beg)))
(end (or end (if (use-region-p) (region-end) end)))
(prompt (if (and beg end) "region" "buffer"))
(new (if (called-interactively-p 'interactive)
(read-file-name
(concat "Write " prompt " to file: ")
nil nil nil
(and buffn (file-name-nondirectory buffn)))
(or file (error "Filename cannot be nil"))))
;; See `write-region' for meaning of 'excl
(mustbenew (if (and buffn (file-equal-p new buffn)) 'excl t)))
(with-current-buffer buffo
(if (and beg end)
(write-region beg end new nil nil nil mustbenew)
(save-restriction
(widen)
(write-region (point-min) (point-max) new nil nil nil mustbenew))))
new))