当 Vim 失去焦点时,如何让 Vim 自动保存文件? [英] How can I make Vim autosave files when it loses focus?

查看:34
本文介绍了当 Vim 失去焦点时,如何让 Vim 自动保存文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我习惯了我的编辑器在失去焦点时自动保存文件.我最近切换到 MacVim,我无法重现这种行为.我试过这个:

I'm used to my editors autosaving files when they lose focus. I recently switched to MacVim, and I can't recreate that behavior. I tried this:

autocmd BufLeave,FocusLost * wall

但是如果在窗口或 MacVim 失去焦点时缓冲区未命名,我会收到如下错误:

but if a buffer is unnamed when the window or MacVim loses focus, I get an error like the following:

Error detected while processing BufLeave Auto commands for "*":
E141: No file name for buffer 1

我很困惑,因为 :wall 的文档说:

I'm confused, because :wall's documentation says:

Write all changed buffers.  Buffers without a file
name or which are readonly are not written.

所以我希望它跳过未命名的缓冲区.有没有更好的方法来做到这一点?

so I'd expect it to skip unnamed buffers. Is there a better way to do this?

编辑:

我很确定使用 BufLeave 可以更好地完成我的意图——当我在它们之间切换时保存缓冲区,所以我不会得到自上次更改后没有写入"-- 是设置'autowriteall'.问题仍然存在于 MacVim 何时失去对其他应用程序的关注 (FocusLost).

I'm pretty sure that the better way to do what I intended by using BufLeave -- saving buffers when I switch between them, so I don't get "No write since last change" -- is to set 'autowriteall'. The question still stands for when MacVim loses focus to other applications (FocusLost).

推荐答案

在这种情况下,您并不关心错误,因为无论如何您都无法合理地处理它们——尤其是在失去注意力的时候.所以就吞下它们:

You don’t care about errors in those circumstances since there is nothing you can reasonably do about them anyway – especially when losing focus. So just swallow them:

autocmd BufLeave,FocusLost * silent! wall

比复杂的舞蹈简单得多,以找出可能错误的位置以避免错误.

Much simpler than an elaborate dance to figure out where there would be an error in order to avoid it.

这篇关于当 Vim 失去焦点时,如何让 Vim 自动保存文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆