1
votes

I use AHK to autoconvert a string like ]dd to the current date (see code below). When using this in most Windows text editors/areas, it works fine. But when I'm using gvim for Windows or vim in Ubuntu on WSL, I often have to type a "priming" character or try the hotstring a couple times for it to work. Searching the forum didn't return any hits on this particular issue.

#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

These work practically flawlessly in Notepad or other modeless text editors/areas I've used, but I really enjoy vim.

I'm guessing AHK is keying off of the space character and not CR/LF, so entering insert mode in a vim-mode editor (including the likes of PyCharm using the IdeaVim plugin) and hitting Enter doesn't let AHK know to start looking for muh hawtstrangs. I have to hit Space, and sometimes hit it a few times, to get the hotstring to be recognized.

I suppose I could just create hotkeys instead but I use this approach in Keyboard Maestro on macOS and keyboard settings in *NIXes and I value the muscle memory.

Is there a configuration somewhere that I'm overlooking, or is this just an edge case?

1

1 Answers

3
votes

To make it work reliably and consistently in Vim, in the hotstring option, include both question mark and asterisk (:*?:...) instead of just asterisk or empty option. This workaround is only thing that worked for me on Vim on Windows. Otherwise, Autohotkey would not expand when it enters the insert-mode for the first time.

So, it might look like:

...

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

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

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

However, this will change the behavior slightly since it will trigger the hotstring inside a word, so Hello]dd will trigger ]dd. But I could live with this and avoid the frustration of always checking if it triggered the hotstring or not.

Alternatively, in order to perfectly match the initial behavior, I'd recreate the text expansion in vim using UltiSnips plugin, but that'd be a little more work than necessary.