当 Vim 失去焦点时,如何让 Vim 自动保存文件?

编程入门 行业动态 更新时间:2024-10-26 17:35:46
本文介绍了当 Vim 失去焦点时,如何让 Vim 自动保存文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我习惯了我的编辑器在失去焦点时自动保存文件.我最近切换到 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屋!

更多推荐

[db:关键词]

本文发布于:2023-04-27 18:22:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1159810.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:保存文件   焦点   Vim

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!