首页 > 解决方案 > AutoHotKey Hotstring 不会在 Windows 上的 gvim 或 WSL Ubuntu 中的 vim 中触发

问题描述

我使用 AHK 将字符串自动转换为]dd当前日期(参见下面的代码)。在大多数 Windows 文本编辑器/区域中使用它时,它工作正常。但是当我在gvimWindows 或vimWSL 上的 Ubuntu 中使用时,我经常需要输入一个“启动”字符或尝试几次热字符串才能使其工作。搜索论坛并没有返回关于这个特定问题的任何点击。

#NoEnv  ; Recommended for performance and compatibility with future 

AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

; This allows me to quickly enter date and time stamps.
::]dd::
FormatTime, TimeString, , yyMMdd ; LongDate
Send, %TimeString%
Return

:*:]t::
FormatTime, TimeString, , HHmm
Send, %TimeString%
Return

:*:]dt::
FormatTime, TimeString, , yyMMdd HHmm
Send, %TimeString%
Return

这些在记事本或我使用过的其他无模式文本编辑器/区域中几乎完美无缺,但我真的很喜欢 vim。

我猜 AHK 正在关闭空格字符而不是 CR/LF,因此insert在 vim 模式编辑器中输入模式(包括使用 IdeaVim 插件的 PyCharm 之类的)并点击Enter不会让 AHK 知道开始寻找莫哈特斯特朗斯。我必须打Space,有时打几次,才能使热字符串被识别。

我想我可以只创建热键,但我在 macOS 上的 Keyboard Maestro 和 *NIXes 中的键盘设置中使用这种方法,并且我重视肌肉记忆。

是否有我忽略的配置,或者这只是一个边缘情况?

标签: windowsvimautohotkey

解决方案


为了使其在 Vim 中可靠且一致地工作,在 hotstring 选项中,包括问号和星号 ( :*?:...) 而不仅仅是星号或空选项。这种解决方法是唯一对我在 Windows 上的 Vim 上有效的方法。否则,Autohotkey 第一次进入插入模式时不会展开。

所以,它可能看起来像:

...

:*?:]dd::
FormatTime, TimeString, , yyMMdd ; LongDate
Send, %TimeString%
Return

:*?:]t::
FormatTime, TimeString, , HHmm
Send, %TimeString%
Return

:*?:]dt::
FormatTime, TimeString, , yyMMdd HHmm
Send, %TimeString%
Return

但是,这会稍微改变行为,因为它会触发单词中的热字串,所以Hello]dd会触发]dd。但我可以忍受这一点,避免总是检查它是否触发了热字符串的挫败感。

或者,为了完美匹配初始行为,我会使用UltiSnips插件在 vim 中重新创建文本扩展,但这会比必要的工作多一点。


推荐阅读