【问题标题】:Emacs: Write buffer to new file, but keep this file openEmacs:将缓冲区写入新文件,但保持此文件打开
【发布时间】:2011-07-07 07:09:04
【问题描述】:

我想在 Emacs 中执行以下操作:将当前缓冲区保存到新文件,同时保持当前文件打开。 当我执行 C-x C-w 时,当前缓冲区会被替换,但我想保持打开两个缓冲区。在不重新打开原始文件的情况下是否可以这样做?

【问题讨论】:

    标签: file emacs duplicates buffer


    【解决方案1】:
    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(或定义键序列)调用。

    【讨论】:

    • 糟糕!这不会按照您的要求保持新文件打开。
    • 是的,这是我的主要问题:保持两个文件/缓冲区都打开。
    【解决方案2】:

    我不认为有什么内置的,但写起来很容易:

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

    【讨论】:

      【解决方案3】:

      这是一个我已经做了一段时间的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))
      

      【讨论】:

      • 不,这不是我想要的。我想打开两个文件。 [尽管如此,一个不错的 sn-p,也会将其添加到我的 .emacs 中]
      • 我之前注意到了,所以我修改了帖子以使用 find-file-noselect 功能。两个缓冲区都保持加载状态,但原始缓冲区仍然具有焦点。如果您希望两者同时可见,请改用 find-file-other-window
      • 哈哈,我的评论太快了。谢谢!
      • 警告:此 sn-p 将覆盖现有文件而不会发出警告。
      【解决方案4】:

      我发现将 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)))
      

      【讨论】:

      • 哦,另一个不错的答案。非常感谢,会试试这个。
      猜你喜欢
      • 1970-01-01
      • 2014-02-26
      • 1970-01-01
      • 2015-06-11
      • 1970-01-01
      • 2015-08-20
      • 1970-01-01
      • 2010-09-19
      • 1970-01-01
      相关资源
      最近更新 更多