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

时间:2011-03-02 13:42:15

标签: file emacs duplicates buffer

我想在Emacs中执行以下操作:将当前缓冲区保存到新文件,但也保持当前文件打开。 当我做C-x C-w然后当前缓冲区被替换,但我想保持打开两个缓冲区。如果不重新打开原始文件,这可能吗?

4 个答案:

答案 0 :(得分: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))

答案 1 :(得分: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))

答案 2 :(得分:3)

我发现将Scott和Chris的答案结合起来很有帮助。用户可以调用save-as,然后在提示是否切换到新文件时回答“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)))

答案 3 :(得分:2)

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写入和打开(或定义键序列)进行调用。