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

File Emacs:将缓冲区写入新文件,但保持此文件打开,file,emacs,duplicates,buffer,File,Emacs,Duplicates,Buffer,我想在Emacs中执行以下操作:将当前缓冲区保存到新文件,但同时保持当前文件处于打开状态。 当我执行C-xc-w时,当前缓冲区会被替换,但我希望两个缓冲区都保持打开状态。是否可以在不重新打开原始文件的情况下执行此操作 C-x h 选择所有缓冲区,然后 M-x write-region 将区域(本例中为整个缓冲区)写入另一个文件 编辑:此函数满足您的需要 (defun write-and-open ( filename ) (interactive "GClone to file:")

我想在Emacs中执行以下操作:将当前缓冲区保存到新文件,但同时保持当前文件处于打开状态。 当我执行C-xc-w时,当前缓冲区会被替换,但我希望两个缓冲区都保持打开状态。是否可以在不重新打开原始文件的情况下执行此操作

C-x h
选择所有缓冲区,然后

M-x write-region
将区域(本例中为整个缓冲区)写入另一个文件

编辑:此函数满足您的需要

(defun write-and-open ( filename )
  (interactive "GClone to file:")
  (progn
    (write-region (point-min) (point-max) filename )
      (find-file filename  ))
      )
这有点粗糙,但要根据你的意愿修改

交互代码“G”提示输入进入“filename”参数的文件名


把这个放到你的.emacs中,用M-x write和open调用(或定义一个键序列)。

我认为没有内置任何东西,但编写起来非常简单:

(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))

这里有一个片段,我已经有一段时间来做这件事了

;;;======================================================================
;;; 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))

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


哎呀!这无法按照您的要求保持新文件打开。是的,这是我的主要问题:保持两个文件/缓冲区都打开。不,这不是我想要的。我想打开两个文件。[尽管如此,这是一个很好的片段,也会将其添加到我的.emacs中]我在前面注意到了这一点,所以我修改了这篇文章以使用find file noselect函数。两个缓冲区都保持加载状态,但原始缓冲区仍具有焦点。如果你想同时看到这两个文件,请使用“查找文件其他窗口”而不是“哈哈,我的评论太快了”。谢谢警告:此代码段将在没有警告的情况下覆盖现有文件。哦,另一个很好的答案。非常感谢,我会试试这个。
;; 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)))