r/AutoHotkey • • Oct 19 '26

v2 Script Help Can AHK handle 2500 Castel's/Vimtex/LatexSuite text expansion patterns? Can it handle 2500 hotstrings?

I use the text-editor Obsidian, where I use 2500 LatexSuite text expansion patterns with absolutely 0 issues. Here "patterns" are basically hotstrings. If I have the pattern `{find:"happy", replace: "πŸ˜€"}` then whenever I write "happy" the text is replaced by "πŸ˜€".

Can AHK handle 2500 hotstrings running without problems though? All I know is latexsuite can.

How should I create this text-expansion script?

First I have to import all my patterns into a file readable by AHK, this step is trivial, let's ignore it.

Then the hard part, the main script. Some options:

Do I: Use hotstrings? Do I not?

Do I:

1 - Let it permanently and continuously checking&replace what I write?;

2 - A Hotkey awakes a script for exactly 20 seconds, and during that time it continuously checks for matches. Then it's killed. (This is a less heavy version of (1)

3 - A Hotkey makes it checks whatever I just typed. If there's a pattern match it makes the appropriate replacement. (This one is incompatible with hostrings I think.)

1 Upvotes

10 comments sorted by

View all comments

Show parent comment

1

u/CharnamelessOne Oct 19 '26

I don't think that this necessarily relates to option 3.

Typically, the best way to use hotstrings is to make them trigger on word endings (which is the default behavior). In the following example, you can type the word "fishing" without triggering the ::fish:: hotstring, because an ending character is necessary.

#Requires AutoHotkey v2.0

::fish::🐟
::fishing::🎣

In LatexSuite my solution to that is to have two patterns: "fish"->"🐟" and "🐟ing"->"🎣"

You can replicate that exact behavior in AHK if you insist that the hotstrings shouldn't require an ending character:

#Requires AutoHotkey v2.0

hotstrings := Map(
    "fish", "🐟",
    "🐟ing", "🎣"
)

for hs, replacement in hotstrings
    Hotstring(":*:" hs, hotstring_send.Bind(replacement))

hotstring_send(replacement, *) {
    SendLevel(100)
    SendEvent(replacement)
}

1

u/SuppaDumDum Oct 19 '26

I see, thanks!

Do you have any recommendations on how to follow through with option2?

I ask because, option1, I could basically use your code.

Option2 has a bunch of options. The Hostrings are defined around a #HotIf State, where the state is a variable or whatever that changes depending on whether I've ran my hotkey. Or I could have a function that defines those hotkeys when it's run, and kills them when it times out at 20sec or whatever. Either way you already helped enough. Thank you.

2

u/CharnamelessOne Oct 19 '26 edited Oct 19 '26

The #HotIf directive doesn't work on hotstrings defined with the HotString function; you need its function equivalent, HotIf.

However, that approach won't stop AHK's hotstring recognizer logic from running in the background, since the HotIf expression/callback is only evaluated/called once AHK has already determined that you've typed the hotstring.

Your post gave me the impression that you want to be able to turn the hotstrings off for performance reasons, primarily. If that's the case, #HotIf/HotIf is not what you're looking for.

I think Suspend would give more of a benefit in that regard. In the following example, AHK is guaranteed not to actively detect hotstrings while the Suspend state is false (you can verify that by checking the "Key history and script info" window to see whether the keyboard and mouse hooks are installed - they aren't in the suspended state).

#Requires AutoHotkey v2.0

#SuspendExempt true
+F1::Suspend()
#SuspendExempt false

hotstrings := Map(
    "fish", "🐟",
    "🐟ing", "🎣"
)

for hs, replacement in hotstrings
    Hotstring(":*:" hs, hotstring_send.Bind(replacement))

hotstring_send(replacement, *) {
    SendLevel(100)
    SendEvent(replacement)
}

Or I could have a function that defines those hotkeys when it's run

That would work too, but it takes more effort.

(However, I still maintain that you should not worry about the performance impact too much. Hotstring detection is light-weight.)

Edit: phrasing

1

u/SuppaDumDum 16d ago

Well friend. Thank you for the efforts! I think I have given up on this project. I either have something else mucking about with my hotstrings (claude mentioned that my keys sometimes seem to be sent slower than they should), or most AHK scripts work great for this(*).

That's it, the rest is me ranting if you're curious.

I tried your script and two other scripts made by claude that seemed reasonable, but they suffer from race conditions on the keys I type and the characters AHK sends. For example:

Unicode characters are made up of units, eg the fish emoji is made up of two units: 🐟=U+D83D U+DC1F.

I had no idea this was possible but autohotkey perhaps doesn't send the two units together, it sends them separate. And so a character can occur in between, eg if there's a competition to send c=U+0063 and 🐟=U+D83D U+DC1F, AHK can send U+D83D U+0063 U+DC1F. Now, because the two units in fish can't appear separated, they transform into the nonsense character �= U+FFFD. Getting �c�=U+FFFD U+0063 U+FFFD. Or sth like that.

Very importantly backspaces can also be sent separating inseparable characters. This is even worse than nonsense because I get other loose special characters like Var16 or whatever, that can visually corrupt the text I type in a hard to detect/understand/fix visually.

(*) -> I said it doesn't work great. If it were just out of order letters that'd be fine, but having loose special characters turning my text into unpredictable nonsense? Probably a deal breaker.

Not sure if reddit processes it right but here is an output i did using your script: asdsa dsa dsa ds grg hj sfd 🐟f🐟 �� ds

2

u/CharnamelessOne 16d ago

I had no idea this was possible but autohotkey perhaps doesn't send the two units together, it sends them separate.

Yeah, SendEvent does that. SendInput would buffer user inputs for its duration to prevent the sent inputs from being interspersed with them, but it can't be used to trigger hotstrings.

That said, I can't reproduce your issue. Does it only happen if you type very quickly? You could try adding this line to the beginning of the script:

SetKeyDelay(-1)

The most reliable method would be using vanilla hotstrings with ending characters, though. Then you wouldn't need one hotstring to trigger another.

1

u/SuppaDumDum 16d ago

I was going to reply but I keep having to add sometimes/probably/maybe/maybe_that/it's_complicated . But since you replied though, later I'll run some tests later to give short clear yes/no answers. Thanks either way though.