Skip to content

Instantly share code, notes, and snippets.

@Axlefublr
Created October 21, 2022 06:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Axlefublr/7805db65b3387e97aa99c104e07567d5 to your computer and use it in GitHub Desktop.
Save Axlefublr/7805db65b3387e97aa99c104e07567d5 to your computer and use it in GitHub Desktop.

So say you have installed v2 and now you want to set up VS Code for it

That used to be kinda complicated, but now there is a nice extension you can install

Search up "Autohotkey v2 language support" and install this extension

Well, thanks for watching and...

Nah, just kidding

Technically, you're done, but I want to actually show you what you're gonna get

That way you can use the tools provided to their full potential

First of all, make sure your v2 folder isn't renamed from what it was after the installation

If you renamed it already, rename it back to the default shown on the screen

Now go back to some v2 script

Ctrl shift p and search for "search language mode"

Configure for .ahk, autohotkey2

You might have noticed this button on the bottom left

You can use it to choose a version used to run your ahk scripts from vs code

One HUGE disclaimer: do NOT choose the UIA versions, for whatever reason, they fuck up the extension to the point that you can't even change it back and everything breaks

Goddammit Axle you should've said so sooner!

Don't worry, I have a solution for that

Search up settings json

If you just have a folder open, choose folder settings

If you set up a workspace, choose workspace settings

You'll end up in this json file that has the interpreter as a setting

Go ahead and manually change the exe to the one without UIA

Reload Vs Code, and everything should be fixed

Now, you don't actually have to click that button to choose your interpreter

There's a command you can look up to reach the same effect

So, if you want to, you can hide the button by right clicking the gutter and pressing hide

Now, there are a few really useful commands I recommend you use

By typing in "ahk2" in your command palette, you'll see all the commands from the extension

Definitely check all of them out, but I will go over the two most useful ones

Let's first write some example code and set some breakpoints

Make sure this extension is also installed

And now, we can debug our v2 scripts

Notice how I didn't need to set anything up?

Yeah, we don't actually need to make a launch json or anything of that nature, just use the command

On the right, you'll see your variables

Nothing appeared in the local section because my variable is actually global, let's change that

Now the age variable appeared in the local section as it should be

After we go over the line, the variable is initialized

One cool thing I haven't seen before is that we get the time it took to execute the line, so you can more easily see how well your code stands in terms of performance

When you debug, you can use the outputdebug command and it will actually appear in the debug console

By the way, your debug console is likely to be in the bottom panel by default

You can move it to where I have it if you want to, vs code is very customizable like that

All right, we went over the debug command, but you don't have to debug to just run a script, there's a command for that too

Search for "run script"

And the extension will run the active script with the interpreter we chose earlier

The output panel will pop up that'll say the script ran

And when that script exitapps, will say so with the exitcode and time the script was running for

You'd think outputdebug would work with this panel too, but unfortunately it only works in the debug console

Alright, lemme teach you how to color your code however you want

Search for tokens and press enter

You'll see this menu

What you need to look for is the topmost line in textmate scopes

Select and copy it, then go to your settings.json

First, editor token color customisations

Then, textMateRules

And here you'll color your code from now on

Let's create an object, it's going to have the scope of what we just copied

Now we create the settings object with two properties

Foreground is where you set the actual color

It accepts hex values for that

And don't worry if you don't know how to get a hex color, because in the actual settings, you'll be able to choose it in this gui

Now for the font style, leave it an empty string if you just want the normal font

But there's also bold, italic, strikethrough and underline

Now say you want to paint some code class the same color

And you don't want to create an object for every code class

Make your scopes an array

And go on, adding more and more classes

If you ever have errors in this process, remember that json cares about commas a lot

When you want to use a different color or a different fontstyle, you should create a new object, just like the last one

Alright, we're done coloring the code

There are a few settings for the extension that I recommend

Here they are

Check out the link in the description for this whole file

The extension provides a loooot of snippets and aids the autocompletetion pretty well

There are two snippets of the extension that I want to show off

First of all, zs Author generates a template for you to use to elaborate on the current file

This is nice, because it's a way to standardize script descriptions

There's one more snippet that I think is even more useful

While inside of a function, use "zs-Comment"

This snippet will generate a comment template for your function

The colored @'s you're seeing actually serve a purpose

The parameter hints for your functions are now far better

The explanation you leave for each parameter will only appear for that parameter

You can specify what the function returns, and easily have clearly separated multiple lines

To do that, you have to leave a blank line in between in your comment

As you can see, the two lines don't overlap, so you can format your comments nicely this way

You might have noticed that a part of the snippet I typed in is still there

Naturally, it's going to leave an error, and you might not notice that it's there, because you get jumped to the tab stops of the comment template

Alright, we can try fixing this by using the built in command instead

And that will probably be the better solution, but is still unreliable in the 1.6.2 version of the extension

To make sure the comment template isn't just a copy of what was before, I changed the name of function as well as the parameters

The variable currently highlighted was supposed to be "num4", but after using the command it became just "num"

I think this is because the command AND snippet send a few of "Undo"s

So for that reason, using the easier versions of this amazing feature might be pretty annoying

What's the actual solution?

Doing it yourself

When you type a slash and two asterisks, the extension generates that fancy block comment you saw in the snippet template

So you can type out everything yourself

And it will work the same exact way as before

The two commented curly brackets at the top of my script is actually also a feature of the extension

You can easily create a folding section between two commented curly brackets

This is really useful for me to not cause you annoyance with all the numerous directives I'm using

The comment features never stop, apparently

Because there's one more

If your comment starts with two semicolons instead of one, it becomes a symbol you can easily jump to

Ah right, the extension brings symbols into ahk

Instead of scrolling trying to find the right function or variable, you can "go to symbol" and end up at the definition of the symbol you tried to find

You don't even have to be in the same file

Because with a different command you can start searching through your entire workspace

And end up not just at the declaration of the symbol, but in a whole different file altogether

This is a feature of vs code that makes it incredibly easy to navigate through a lot of files and everything in them, and now finally it's available for ahk

Another thing the v2 extension improved massively is the "go to definition" command

It's far more robust, and works pretty much always

A big advantage is also the fact that arrow functions are now considered actual functions

So they have symbols of their own just as expected, and "go to definition" works for them

You just have to remember to put in the includes for everything you're going to use

Even if the includes happen somewhere else and it works, the extension has no way of knowing that

So be more elaborative with your includes to make sure autocomplete works

After all, no matter how many times you include a file, it will still only be included once

Now what am I talking about with all these "back then" and "used to"

Before I discovered this extension, I used to modify autohotkey plus plus to make it work for v2 instead

I've made, I think, three videos on that, which are now unlisted

So these amazing features really are a breath of fresh air in terms of quality

I don't need to make subpar solutions anymore to please the extension

And I no longer need to maintain those videos and the code I made along with them

Thank you very much for watching this video

And I hope you can enjoy this amazing extension as much as I do, bye-bye!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment