Vim:失去焦点时返回命令模式
我喜欢我的 vim 尽可能频繁地进入命令模式。我认为失去焦点将是实现这一目标的一个好事件。我发现的一切都是为了避免失去焦点。
我希望它在 macvim 中切换选项卡或 cmd+tab 切换到另一个应用程序时自动返回到 cmd 模式。
I like my vim to get itself into command mode as often as possible. I think losing focus would be a good event to make that happen. Everything I found is for saving on lost focus.
I'd like it to auto-return to cmd mode when switching tabs in macvim or when cmd+tabbing to another app.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面的 自动命令 将是“显而易见”的选择。
不幸的是,它似乎只适用于 TabLeave。 FocusLost 事件正在触发,但由于某种原因 stopinsert 命令实际上并未触发直到 Vim 重新获得焦点后接收到按键事件后才会生效。
相反,您可以利用 feedkeys 和“让我恢复正常”无论如何模式!” 组合键:
唯一的缺点是
feedkeys ()
至少需要 Vim 7。不过,这应该不是什么大问题,因为 Vim 7 早在 2006 年就发布了。The following autocommand would be the "obvious" choice.
Unfortunately, it only seems to be working properly for TabLeave. The FocusLost event is triggering but for some reason the stopinsert command isn't actually taking effect until after a key event is received once Vim has regained focus.
Instead, you can take advantage of feedkeys and the "Get me to normal mode no matter what!" key combo:
The only downside is that
feedkeys()
requires at least Vim 7. This shouldn't be a big deal, though, since Vim 7 was released back in 2006.我本想添加评论,但无法格式化解决方案。
feedkeys 解决方案很棒,但有一个小问题,无论您处于哪种其他模式,它总是返回到正常模式。我不想取消命令行模式(用于 Windows 中的拖放文件),而且我不想不需要取消视觉模式,我只是想取消插入模式。
那么,解决方案如下所示:
换句话说,仅当您处于插入模式时才弹出。这可以进一步细化,因为 v:insertmode 在“正常插入”中将为“i”,在替换模式中为“r”,在虚拟替换模式中为“v”。对我来说,无论如何弹出都是好的,但用户可能想要进行编辑以适应。
如果这在 MacVim 中不起作用,请将
PopOutOfInsertMode
的内容替换为:I would have added a comment, but I can't format the solution.
The feedkeys solution is great, with the small hitch that it ALWAYS goes back to normal mode, regardless of what other mode you were in. I don't want to cancel command line mode (for drag&drop files in Windows) and I don't need to cancel visual mode, I just wanted to cancel insert mode.
The solution, then, appears as:
In other words, only pop out if you're in an insert mode. This could be further refined, since v:insertmode will be 'i' in "normal insert", 'r' in Replace mode, and 'v' in Virtual Replace mode. For me, popping out regardless is good, but the user may want to edit to suit.
If this isn't working for you in MacVim, replace the contents of
PopOutOfInsertMode
with: