AutoHotkey scripts

An AutoHotkey script is a plain text file with an .ahk extension. Lines run top to bottom when the file loads, and any hotkey definitions stay resident afterward, waiting for their key. That is the whole model — everything below is a variation on it.

Running a script

  1. Create a new text file and rename it so it ends in .ahk.
  2. Paste the code and save it as UTF-8.
  3. Double-click the file. A tray icon appears while it runs.
  4. Right-click the tray icon to reload after an edit, or to exit.

Launch anything from the keyboard

#Requires AutoHotkey v2.0

^!e::Run "explorer.exe"
^!t::Run "wt.exe"
^!b::Run "https://autohotkeytools.com/"

Type less with text expansion

::addr::1 Market Street, Suite 400
::brb::be right back
:*:sig::Best regards,`nAlex

The colons mark a hotstring. The * option fires without waiting for a space or punctuation, and `n inserts a line break.

Clean up whatever you copied

^!v::
{
    text := A_Clipboard
    text := StrReplace(text, "`r`n", " ")
    A_Clipboard := Trim(text)
    Send "^v"
}

This flattens a multi-line copy into one line before pasting it — useful when moving text out of a PDF.

Snap the active window

#Left::WinMove 0, 0, A_ScreenWidth / 2, A_ScreenHeight, "A"
#Right::WinMove A_ScreenWidth / 2, 0, A_ScreenWidth / 2, A_ScreenHeight, "A"

Make a script app-aware

#HotIf WinActive("ahk_exe chrome.exe")
^q::Send "^w"
#HotIf

Between the two #HotIf lines, the hotkey only exists while Chrome is in front. Everywhere else, Ctrl+Q keeps its normal meaning.

Habits that keep scripts working

More finished code lives on the examples page, and the productivity page groups scripts by the kind of work they save.

Frequently asked questions

How do I create an AutoHotkey script?

Save a text file with the .ahk extension, add your code, and double-click the file to run it.

Where should I keep my AHK scripts?

Any folder works. A single scripts folder, with a shortcut to your main file in the Startup folder, keeps things simple.

How do I reload a script after editing?

Right-click the tray icon and choose Reload, or add a hotkey that calls Reload.


AutoHotkeyTools.com is an independent Windows resource covering AutoHotkey tools, scripts, hotkeys, tutorials and desktop automation. It is not affiliated with the AutoHotkey project.