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

发布于 2024-10-19 22:45:20 字数 108 浏览 3 评论 0原文

我想在 Emacs 中执行以下操作:将当前缓冲区保存到新文件,但同时保持当前文件打开。 当我执行 Cx Cw 时,当前缓冲区将被替换,但我想保持两个缓冲区打开。这可以在不重新打开原始文件的情况下实现吗?

I want to do the following in Emacs: Save the current buffer to a new file, but also keep the current file open.
When I do C-x C-w then current buffer gets replaced, but I want to keep open both buffer. Is this possible without reopening the original file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半世晨晓 2024-10-26 22:45:20

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

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

I don't think there's anything built in, but it's easy enough to write:

(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))
小红帽 2024-10-26 22:45:20

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

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

Here's a snippet I've had for a while to do just this

;;;======================================================================
;;; 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))
盛装女皇 2024-10-26 22:45:20

我发现将斯科特和克里斯的上述答案结合起来很有帮助。用户可以调用另存为,然后在提示是否切换到新文件时回答“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)))

I found it helpful to combine Scott's and Chris's answers above. The user can call save-as and then answer "y" or "n" when prompted whether to switch to the new file. (Alternatively, the user can select the desired functionality via the function name save-as-and-switch or save-as-but-do-not-switch, but this requires more keystrokes. Those names are still available for being called by other functions in the future, however.)

;; 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)))
再浓的妆也掩不了殇 2024-10-26 22:45:20
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 中并使用 Mx write-and-open 进行调用(或定义按键序列)。

C-x h

selects all the buffer, then

M-x write-region

writes the region (whole buffer in this example) to another file.

Edit: this function does what you need

(defun write-and-open ( filename )
  (interactive "GClone to file:")
  (progn
    (write-region (point-min) (point-max) filename )
      (find-file filename  ))
      )

It's a bit crude, but modify to your will.

The interactive code 'G' prompts for a filename which goes into the 'filename' argument.

Drop this into your .emacs and call with M-x write-and-open (or define a key sequence).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文