【发布时间】:2011-07-07 07:09:04
【问题描述】:
我想在 Emacs 中执行以下操作:将当前缓冲区保存到新文件,同时保持当前文件打开。 当我执行 C-x C-w 时,当前缓冲区会被替换,但我想保持打开两个缓冲区。在不重新打开原始文件的情况下是否可以这样做?
【问题讨论】:
标签: file emacs duplicates buffer
我想在 Emacs 中执行以下操作:将当前缓冲区保存到新文件,同时保持当前文件打开。 当我执行 C-x C-w 时,当前缓冲区会被替换,但我想保持打开两个缓冲区。在不重新打开原始文件的情况下是否可以这样做?
【问题讨论】:
标签: file emacs duplicates buffer
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”提示输入进入“文件名”参数的文件名。
将其放入您的 .emacs 并使用 M-x write-and-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))
【讨论】:
这是一个我已经做了一段时间的sn-p
;;;======================================================================
;;; 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))
【讨论】:
我发现将 Scott 和 Chris 的上述答案结合起来很有帮助。用户可以调用另存为,然后在提示是否切换到新文件时回答“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)))
【讨论】: