Emacs:将缓冲区写入新文件,但保持此文件打开

Oca*_*tal 8 emacs buffer file duplicates

我想在Emacs中执行以下操作:将当前缓冲区保存到新文件,但也保持当前文件打开.当我执行Cx Cw时,当前缓冲区被替换,但我想保持打开两个缓冲区.如果不重新打开原始文件,这可能吗?

sco*_*zer 12

我不认为内置任何东西,但写起来很容易:

(defun my-clone-and-open-file (filename)
  "Clone the current buffer writing it into FILENAME and open it"
  (interactive "FClone to file: ")
  (save-restriction
    (widen)
    (write-region (point-min) (point-max) filename nil nil nil 'confirm))
  (find-file-noselect filename))
Run Code Online (Sandbox Code Playgroud)


Chr*_*han 7

这是我有一段时间做这个片段的片段

;;;======================================================================
;;; provide save-as functionality without renaming the current buffer
(defun save-as (new-filename)
  (interactive "FFilename:")
  (write-region (point-min) (point-max) new-filename)
  (find-file-noselect new-filename))
Run Code Online (Sandbox Code Playgroud)


Ala*_*lan 5

我发现将斯科特和克里斯的上述答案结合起来很有帮助。用户可以调用另存为,然后在提示是否切换到新文件时回答“y”或“n”。(或者,用户可以通过函数名称 save-as-and-switch 或 save-as-but-do-not-switch 选择所需的功能,但这需要更多的击键。这些名称仍然可供其他人调用不过,将来会起作用。)

;; based on scottfrazer's code
(defun save-as-and-switch (filename)
  "Clone the current buffer and switch to the clone"
  (interactive "FCopy and switch to file: ")
  (save-restriction
    (widen)
    (write-region (point-min) (point-max) filename nil nil nil 'confirm))
  (find-file filename))

;; based on Chris McMahan's code
(defun save-as-but-do-not-switch (filename)
  "Clone the current buffer but don't switch to the clone"
  (interactive "FCopy (without switching) to file:")
  (write-region (point-min) (point-max) filename)
  (find-file-noselect filename))

;; My own function for combining the two above.
(defun save-as (filename)
  "Prompt user whether to switch to the clone."
  (interactive "FCopy to file: ")
  (if (y-or-n-p "Switch to new file?")
    (save-as-and-switch filename)
    (save-as-but-do-not-switch filename)))
Run Code Online (Sandbox Code Playgroud)