Emacs 自动保存切换缓冲区

发布于 2024-08-04 03:03:07 字数 267 浏览 2 评论 0原文

你可以说我是瘸子,但我已经厌倦了潜意识中的Cx Cs紧张抽搐。我经常切换缓冲区,并且我想在切换到另一个缓冲区时立即保存某个缓冲区。我还没有时间学习 Emacs-Lisp 基础知识。

有关如何执行此操作或更好的解决方案的任何提示?

(在相关说明中,我发现了一个 自动保存解决方法,它可以在您在给定的时间内处于空闲状态。)

Call me lame, but I'm tired of my subconscious C-x C-s nervous twitch. I am switching buffers often enough and I think I would like to save a certain buffer as soon as I switch to another. I have not had the time yet to learn Emacs-Lisp basics.

Any hints on how to do this, or better solutions?

(On a related note, I found an autosave workaround that can save the current buffer as soon as you are idle for a given amount of time.)

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

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

发布评论

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

评论(3

甜妞爱困 2024-08-11 03:03:07

扩展 Seth答案,我会这样做:

(defadvice switch-to-buffer (before save-buffer-now activate)
  (when buffer-file-name (save-buffer)))
(defadvice other-window (before other-window-now activate)
  (when buffer-file-name (save-buffer)))
(defadvice other-frame (before other-frame-now activate)
  (when buffer-file-name (save-buffer)))

检查buffer-file-name避免保存没有文件的缓冲区。您需要找出用于切换您关心的缓冲区的所有入口点(我还建议other-window)。

To expand on Seth's answer, I'd do this:

(defadvice switch-to-buffer (before save-buffer-now activate)
  (when buffer-file-name (save-buffer)))
(defadvice other-window (before other-window-now activate)
  (when buffer-file-name (save-buffer)))
(defadvice other-frame (before other-frame-now activate)
  (when buffer-file-name (save-buffer)))

The check for buffer-file-name avoids saving buffers w/out files. You need to figure out all the entry points you use for switching buffers that you care about (I'd also advise other-window).

我不吻晚风 2024-08-11 03:03:07

我自己对 emacs lisp 有点陌生,但这在我的测试中有效:

(defadvice switch-to-buffer (before save-buffer-now)
  (save-buffer))

(ad-activate 'switch-to-buffer)

这有点烦人,因为它在每个缓冲区之后调用(例如 scratch)。因此,请将此答案视为一个提示。

当你想禁用它时,你需要调用:

(ad-disable-advice 'switch-to-buffer 'before 'save-buffer-now)
(ad-activate 'switch-to-buffer)

I'm kind of new to emacs lisp myself but this works in my testing:

(defadvice switch-to-buffer (before save-buffer-now)
  (save-buffer))

(ad-activate 'switch-to-buffer)

It's kind of annoying though because it's called after EVERY buffer (like scratch). So, consider this answer a hint.

When you want to disable it, you'll need to call:

(ad-disable-advice 'switch-to-buffer 'before 'save-buffer-now)
(ad-activate 'switch-to-buffer)
瑶笙 2024-08-11 03:03:07

几个想法。

首先,如果您发现自己以足够高的频率调用诸如 save 之类的命令,则可以考虑为该命令使用较短的键绑定。例如,我还发现自己有相同的“抽搐”,所以现在我使用 f2 而不是 Cx Cs 来保存编辑。

我绑定到 f2 的函数无条件保存每个未保存的缓冲区。您可能会发现它很有用:

(defun force-save-all ()
    "Unconditionally saves all unsaved buffers."
    (interactive)
    (save-some-buffers t))

(global-set-key [f2] 'force-save-all)

现在,讨论主要问题。您可以尝试这样的操作(请注意,调用了 force-save-all):

(defun my-switch-to-buffer (buffer)
    (interactive (list (read-buffer "Switch to buffer: " (cadr buffer-name-history) nil)))
    (force-save-all)
    (switch-to-buffer buffer))

(global-set-key "\C-xb" 'my-switch-to-buffer)

当然,您也可以将切换缓冲区功能绑定到另一个键,例如功能键,这样它就是一个按操作。

我认为 @seth 对于使用建议有一个很好的主意,但我注意到 ELisp 手册建议 建议不要用于键绑定。我不太清楚为什么会出现这种情况,但这就是手册建议的仅供参考。

A couple of ideas.

First, if you find yourself invoking a command like save with a sufficiently high frequency, you might consider a shorter key binding for the command. For example, I also found myself having the same "twitch," so now I use f2 instead of C-x C-s for saving edits.

The function that I bind to f2 saves every unsaved buffer unconditionally. You might find it useful:

(defun force-save-all ()
    "Unconditionally saves all unsaved buffers."
    (interactive)
    (save-some-buffers t))

(global-set-key [f2] 'force-save-all)

Now, on to the main issue. You could try something like this (notice that force-save-all is called):

(defun my-switch-to-buffer (buffer)
    (interactive (list (read-buffer "Switch to buffer: " (cadr buffer-name-history) nil)))
    (force-save-all)
    (switch-to-buffer buffer))

(global-set-key "\C-xb" 'my-switch-to-buffer)

Of course, you could also bind the switch buffer functionality to another key, like a function key, so that it's a one press operation.

I thought that @seth had a great idea about using advice, but I noticed that the ELisp manual suggests that advice not be used for key bindings. I'm not quite sure why this is the case, but that's what the manual suggests FYI.

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