Vim:失去焦点时返回命令模式

发布于 2024-09-03 17:43:29 字数 131 浏览 3 评论 0原文

我喜欢我的 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 技术交流群。

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

发布评论

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

评论(2

℉服软 2024-09-10 17:43:29

下面的 自动命令 将是“显而易见”的选择。

au FocusLost,TabLeave * stopinsert

不幸的是,它似乎只适用于 TabLeave。 FocusLost 事件正在触发,但由于某种原因 stopinsert 命令实际上并未触发直到 Vim 重新获得焦点后接收到按键事件后才会生效。

相反,您可以利用 feedkeys 和“让我恢复正常”无论如何模式!” 组合键

au FocusLost,TabLeave * call feedkeys("\<C-\>\<C-n>")

唯一的缺点是 feedkeys () 至少需要 Vim 7。不过,这应该不是什么大问题,因为 Vim 7 早在 2006 年就发布了。

The following autocommand would be the "obvious" choice.

au FocusLost,TabLeave * stopinsert

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:

au FocusLost,TabLeave * call feedkeys("\<C-\>\<C-n>")

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.

你是我的挚爱i 2024-09-10 17:43:29

我本想添加评论,但无法格式化解决方案。

feedkeys 解决方案很棒,但有一个小问题,无论您处于哪种其他模式,它总是返回到正常模式。我不想取消命令行模式(用于 Windows 中的拖放文件),而且我不想不需要取消视觉模式,我只是想取消插入模式。

那么,解决方案如下所示:

autocmd FocusLost * call PopOutOfInsertMode()

function! PopOutOfInsertMode()
    if v:insertmode
        feedkeys("\<C-\>\<C-n>")
    endif
endfunction

换句话说,仅当您处于插入模式时才弹出。这可以进一步细化,因为 v:insertmode 在“正常插入”中将为“i”,在替换模式中为“r”,在虚拟替换模式中为“v”。对我来说,无论如何弹出都是好的,但用户可能想要进行编辑以适应。

如果这在 MacVim 中不起作用,请将 PopOutOfInsertMode 的内容替换为:

if v:insertmode == 'i' | call feedkeys("\<C-\>\<C-n>") | endif

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:

autocmd FocusLost * call PopOutOfInsertMode()

function! PopOutOfInsertMode()
    if v:insertmode
        feedkeys("\<C-\>\<C-n>")
    endif
endfunction

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:

if v:insertmode == 'i' | call feedkeys("\<C-\>\<C-n>") | endif
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文