Emacs 自动保存切换缓冲区
你可以说我是瘸子,但我已经厌倦了潜意识中的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
扩展 Seth 的 答案,我会这样做:
检查
buffer-file-name
避免保存没有文件的缓冲区。您需要找出用于切换您关心的缓冲区的所有入口点(我还建议other-window
)。To expand on Seth's answer, I'd do this:
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 adviseother-window
).我自己对 emacs lisp 有点陌生,但这在我的测试中有效:
这有点烦人,因为它在每个缓冲区之后调用(例如 scratch)。因此,请将此答案视为一个提示。
当你想禁用它时,你需要调用:
I'm kind of new to emacs lisp myself but this works in my testing:
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:
几个想法。
首先,如果您发现自己以足够高的频率调用诸如 save 之类的命令,则可以考虑为该命令使用较短的键绑定。例如,我还发现自己有相同的“抽搐”,所以现在我使用
f2
而不是Cx Cs
来保存编辑。我绑定到 f2 的函数无条件保存每个未保存的缓冲区。您可能会发现它很有用:
现在,讨论主要问题。您可以尝试这样的操作(请注意,调用了
force-save-all
):当然,您也可以将切换缓冲区功能绑定到另一个键,例如功能键,这样它就是一个按操作。
我认为 @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 ofC-x C-s
for saving edits.The function that I bind to
f2
saves every unsaved buffer unconditionally. You might find it useful:Now, on to the main issue. You could try something like this (notice that
force-save-all
is called):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.