3 squares with green gradients. The have the letters A H and K respectively on them.

Snippets everywhere on Windows with autohotkey

31 December 2024

One of the big problems with doing anything on computers is getting the information from your brain into the computer. This is opposed to the other (bigger) problem of solving/understanding the problem in your brain. As developers, the biggest thing that separates us from non-developers is that we communicate to the machine through text. Like Steve Yegge said: Text is king.

Getting text into the computer is an important mechanical process and we want it to happen as quickly as possible. There are a lot of methods to speed this up; learning to touch type, learning vim (or any editor tooling), using large-language-model autocomplete (which can save a lot of typing regardless of the quality it produces). I‘ve been looking at an underrated productivity boon: having a bunch of text snippets at your disposal. There are so many cases where we are typing the same thing; for loops, React components, common SQL queries. The list goes on.

Any code editor worth its salt will have a snippet feature, but I found that I needed some of the same text snippets in programs outside my text editor. For instance; what if I need the same snippet in SSMS to query a database and in VSCode for a migration script? What if I need to use a JavaScript snippet in the browser devtools console as well as in my source code?

If you’re using a Mac, you can laugh your way over to Raycast and use their Snippets feature. Unfortunately for us Windows users, we don’t have Raycast (yet).

However, what we do have on Windows is autohotkey. This is a magical piece of software. I am very annoyed that I hadn’t heard of it before 2024, because it’s basically a way to make Windows programmable and useful. It’s an entire programming environment where you can create GUIs, mimic user typing/clicking, and open programs.

I have barely scratched the surface of what you can do with autohotkey. To use it, you create script files with the ahk extension, run them with the autohotkey program, then it will hijack certain hotkeys to do whatever you want. If you mess up, and want the old hotkeys back, you can just click into “show hidden icons” in the bottom right of your Windows dock and exit the script.

There are lots of ways to set up snippets with autohotkey. For example, you can program it so every time you time “hmf” it will get converted to “Hello, my friend. I hope this email finds you well.” with the following script:

    ; autohotkey v2
    
    ::hmf::Hello, my friend. I hope this email finds you well.

I’ve started with a comment at the top specifying that it’s the V2 version of the scripting language. This is in because autohotkey recently went through a huge overhaul (a little like Python’s 3.0 change). Unfortunately, this makes for a lot of churn; lots of old scripts that no longer work. However, it has drastically improved the language; see Somehow AutoHotKey is kinda good now. It’s a little annoying for me, as my work signed off the 1.1 version of autohotkey, but I’m using 2.0 at home.

Anyway, back to snippets. The code above isn’t how I prefer to create snippets. I don’t really want text that I might accidentally type suddenly be hijacked. Instead, I like to take a windows hotkey that I barely ever use and use it to open a prompt with the list of snippets I can choose from. For me, the best hotkey is shift + windows + p. By default this hotkey configures display settings for multiple monitors, but I usually get to this by right clicking the desktop and opening display settings. I’m configuring monitors at the most once at the start of the day. Text snippets are more important, so I’m happy to lose the normal hotkey.

    ; autohotkey v2
    
    #SingleInstance force
    
    MyMenu := Menu()
    MyMenuBar := MenuBar()
    MyMenu := Menu.Call()
    MyMenuBar := MenuBar.Call()
    
    MyMenu.Add("1 For loop",PrintForLoop)
    MyMenu.Add("2 JS Function",PrintFunction)
    MyMenu.Add("3 Get users",PrintSelectUsers)
    
    PrintForLoop(ItemName, ItemPos, MyMenu){
            PrintSnipp("for (let i = 0; i < array.length; i++){`n`n}")
    }
    
    PrintFunction(ItemName, ItemPos, MyMenu){
            PrintSnipp("function myFunc(){`n`n}")
    }
    
    PrintSelectUsers(ItemName, ItemPos, MyMenu){
            PrintSnipp("SELECT TOP 10 * FROM tblUsers")
    }
    
    PrintSnipp(text){
            ClipSaved := ClipboardAll()
            A_Clipboard := ""
            A_Clipboard := text 
            ClipWait(2)
            send("^v")
            Sleep(100)
            A_Clipboard := ClipSaved
    }
    
    #+p::MyMenu.Show()

Just in case you have to use autohotkey 1.1 sometimes like me, here’s the script to do this in V1.1:

    ; autohotkey v1.1
    
    #SingleInstance force
    
    Menu SnippetMenu, Add, 1 For loop, PrintSnippet1
    Menu SnippetMenu, Add, 2 JS Function, PrintSnippet2
    Menu SnippetMenu, Add, 3 Get users, PrintSnippet3
    
    PrintSnippet1:
            PrintSnipp("for (let i = 0; i < array.length; i++){`n`n}")
    return
    
    PrintSnippet2:
            PrintSnipp("function myFunc(){`n`n}")
    return
    
    PrintSnippet3:
            PrintSnipp("SELECT TOP 10 * FROM tblUsers")
    return
    
    PrintSnipp(text){
            ClipSaved := ClipboardAll
            clipBoard := ""
            clipBoard = %text% 
            ClipWait,2
            if (!ErrorLevel) 
                    send, ^v
            Sleep, 100
            clipboard := ClipSaved
    }
    
    #+p::Menu, SnippetMenu, Show

After you run one of these scripts with your preferred version of autohotkey, it will mean shift + windows + p will open up a little menu where you can press 1, 2, or 3 and it will paste in the snippet you need.

I previously had some text files deep in my documents folder where I’d store important and useful text snippets that I’d always forget. Now, I just add them onto my autohotkey snippets, so I can pull them out immediately whenever I need.

From here you can go a lot of places. You could add submenus: This would mean you could split the scripts up by different languages or projects. You could also add a text input search to search through your snippets. Like I said, I’m scratching the surface. If you have any improvements or any better ways of getting snippets on your Windows machine, hit me up on Bluesky.

Back to blog