如何强制Emacs即使认为也保存(不需要保存任何更改)

发布于 2024-09-09 00:57:18 字数 237 浏览 3 评论 0原文

这种情况经常发生在我身上:

  • 我在 emacs 中打开了一个文件,
  • 我保存它('save-buffer),
  • 文件在磁盘上发生变化(或被删除、移动等),
  • 但我想要它回来,所以我尝试再次在 emacs 中保存('save-buffer),而不是保存它说“(不需要保存任何更改)”并且不执行任何操作。

是否有不同的函数或设置可以用来强制 emacs 保存?

This happens to me all the time:

  • I have a file open in emacs,
  • I save it ('save-buffer),
  • the file changes on disk (or get's deleted, moved, etc.)
  • but I want it back, so I try to save again in emacs ('save-buffer) and instead of saving it says "(no changes need to be saved)" and does nothing.

Is there a different function, or a setting, that I can use to force emacs to save?

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

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

发布评论

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

评论(6

梨涡少年 2024-09-16 00:57:18

在 save-buffer 周围封装一个函数,标记首先修改的缓冲区:

(defun save-buffer-always ()
  "Save the buffer even if it is not modified."
  (interactive)
  (set-buffer-modified-p t)
  (save-buffer))

Wrap a function around save-buffer that marks the buffer modified first:

(defun save-buffer-always ()
  "Save the buffer even if it is not modified."
  (interactive)
  (set-buffer-modified-p t)
  (save-buffer))
请帮我爱他 2024-09-16 00:57:18

您可以使用 Cx Cw 另存为。那应该无条件保存。您也可以只键入一个空格,然后在其上退格。 Emacs 足够聪明,能够意识到如果您撤消到目前为止所做的所有操作,缓冲区不会发生任何更改,但如果您进行更改然后手动反转它们,它将认为缓冲区已更改。

You can save as, with C-x C-w. That should save unconditionally. You can also just type a space then backspace over it. Emacs is smart enough to realize that if you undo everything you've done so far the buffer has no changes, but if you make changes and then manually reverse them it will consider the buffer to have been changed.

葬心 2024-09-16 00:57:18

您可以使用带有前缀 arg 的 Emacs-Lisp 函数 not-modified 将当前缓冲区标记为已修改,绑定到:

C-u M-~

如果您不直接调用新函数,则上述答案将不起作用。
如果你想无缝地改变 emacs 的保存行为。最好的解决方案是创建一个建议:

(defadvice save-buffer (before save-buffer-always activate)
  "always save buffer"
  (set-buffer-modified-p t))

You can mark the current buffer as modified using the Emacs-Lisp function not-modified with a prefix arg, bound to:

C-u M-~

The answer above won't work if you don't call the new function directly.
If you want to seamlessly change emacs saving behavior. The best solution is to create an advice:

(defadvice save-buffer (before save-buffer-always activate)
  "always save buffer"
  (set-buffer-modified-p t))
风流物 2024-09-16 00:57:18

作为 scottfrazer 答案的一个小替代方案:

(defun my-save-buffer-always-sometimes (prefix)
  "Save the buffer even if it is not modified."
  (interactive "P")
  (when prefix
    (set-buffer-modified-p t))
  (save-buffer))

当您想要使用前缀(Cu Cx Cs)时,您可以强制它,但不会不必要地更改文件。最后修改的时间戳非常有用(例如源代码控制),随意更改它似乎很可惜。 YMMV,当然。

As a slight alternative to scottfrazer's answer:

(defun my-save-buffer-always-sometimes (prefix)
  "Save the buffer even if it is not modified."
  (interactive "P")
  (when prefix
    (set-buffer-modified-p t))
  (save-buffer))

This was you can force it when you want to with a prefix (C-u C-x C-s) but not unnecessarily change a file otherwise. The last modified timestamp is very useful (e.g. source code control) that it seems a shame to change it arbitrarily. YMMV, of course.

谎言月老 2024-09-16 00:57:18

类似的问题让我上网寻找解决方案。然后我突然意识到,我所要做的就是输入一个空格(或任何字符)并将其删除,这将缓冲区标记为已更改。然后我可以像平常一样输入 Cx Cs。也许并不复杂或先进,但它确实有效。

A similar problem brought me online to look for a solution. Then it hits me that all I have to do is type a space (or any character) and delete it, which marks the buffer as changed. Then I can type C-x C-s as normal. Maybe not sophisticated or advanced, but it works.

赤濁 2024-09-16 00:57:18

正如 Tagore Smith 所说,您可以使用 Cx Cw 强制 Emacs 保存缓冲区。

如果您使用的是邪恶模式,您还可以通过在正常状态下键入 :w! 来实现此行为。与 Cx Cw 不同,:w! 不会提示您输入要保存到的文件名。

Like Tagore Smith said, you can force Emacs to save the buffer with C-x C-w.

If you're using Evil mode, you can also achieve this behavior by typing :w! in normal state. Unlike C-x C-w, :w! will not prompt you for the filename to save to.

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