how to easily search ahk code in notepad++?

Scripting and setups with Notepad++ and AutoHotkey.
newcod3r
Posts: 505
Joined: 30 Sep 2021, 02:16

how to easily search ahk code in notepad++?

Post by newcod3r » 08 Jan 2022, 22:34

I often use these anchors as search references, For instance,

Code: Select all

#If WinActive("Save As ahk_exe notepad++.exe")
My question is how to use wildcards so that I can get all instances:

Code: Select all

#If WinActive("*notepad++.exe*")
image.png
image.png (31.12 KiB) Viewed 2784 times

User avatar
boiler
Posts: 16985
Joined: 21 Dec 2014, 02:44

Re: how to easily search ahk code in notepad++?

Post by boiler » 08 Jan 2022, 22:58

You have to use the "Regular expression" search mode, and then you would have to use regular expression symbols for wildcards (such as a dot for match any character) and quantities (such as an asterisk for 0 or more), and escape characters that would otherwise have a meaning in regular expressions. So this would go in the "Find what:" box:
#IfWinActive\(".*notepad\+\+.exe.*"\)

...which would find both of these:
#IfWinActive("ahk_exe notepad++.exe")
#IfWinActive("Save As ahk_exe notepad++.exe")

newcod3r
Posts: 505
Joined: 30 Sep 2021, 02:16

Re: how to easily search ahk code in notepad++?

Post by newcod3r » 08 Jan 2022, 23:16

Thank you. How do I get it to ignore the spaces though:
image.png
image.png (83.95 KiB) Viewed 2744 times
Sometimes I might use

Code: Select all

#If WinActive
or
#IfWinActive

User avatar
boiler
Posts: 16985
Joined: 21 Dec 2014, 02:44

Re: how to easily search ahk code in notepad++?

Post by boiler » 08 Jan 2022, 23:25

That can be done, but it wouldn't make sense with those. When you use #If WinActive, then WinActive is actually the start of the function call and its parameter(s), so the whole thing would be, for example:

Code: Select all

#If WinActive("ahk_exe notepad++.exe")
But if you use the directive #IfWinActive, you're not using the function and therefore not its expression syntax either, so the above line is:

Code: Select all

#IfWinActive, ahk_exe notepad++.exe

And the same RegEx looking to match quotation marks and parentheses wouldn't match both.


But to match only #If WinActive or #IfWinActive without regard to what follows it, it would be:
#If ?WinActive
(the ? after the space means to match 0 or 1 of them, i.e., it is optional)

newcod3r
Posts: 505
Joined: 30 Sep 2021, 02:16

Re: how to easily search ahk code in notepad++?

Post by newcod3r » 09 Jan 2022, 00:25

Understood. following the same example, why does it not work when I try to insert a wildcard in the middle of the filename?

Code: Select all

#If ?WinActive\(".*zoom*.exe.*"\)
This means I want to match either of this:

Code: Select all

#If WinActive("ahk_exe Zoom.exe")
#If WinActive("ahk_exe ZoomProOrWhateverItsCalled.exe") 

User avatar
boiler
Posts: 16985
Joined: 21 Dec 2014, 02:44

Re: how to easily search ahk code in notepad++?

Post by boiler » 09 Jan 2022, 03:05

You’re misunderstanding what the wildcard character is. It’s the dot, not the asterisk. The dot says to match any character. The asterisk says to match zero or more of the preceding, so together, .* make up what you are thinking of a wildcard character. By adding just the asterisk, you told it to match zero or more of the letter m that precedes it. It should be this:
#If ?WinActive\(".*zoom.*.exe.*"\)

Actually, the dot before the extension should be escaped in all of the above search patterns. Otherwise, it is matching any character there, not just a dot. So the above should be:
#If ?WinActive\(".*zoom.*\.exe.*"\)

newcod3r
Posts: 505
Joined: 30 Sep 2021, 02:16

Re: how to easily search ahk code in notepad++?

Post by newcod3r » 09 Jan 2022, 20:27

you are absolutely right. I thought the wildcard (asterisk) is universal (I use it a lot in search).. hence I thought the same applies to AutoHotkey regex. Thank you!

Post Reply

Return to “Notepad++”