AutoHotkey v2 Clipboard Scripts: 5 Copy-Paste Examples
AutoHotkey v2 controls the Windows clipboard through a few built-ins: A_Clipboard reads and writes clipboard text, ClipboardAll() saves and restores everything on it, ClipWait waits for a copy to arrive, and OnClipboardChange runs a function every time the clipboard changes. The five scripts below use them to paste as plain text, append to the clipboard, clean links as you copy, pick from a clipboard history menu, and log every copy to a file.
Every script is written for AutoHotkey v2 and starts with #Requires AutoHotkey v2.0, so it stops with a clear message if you run it with v1. Old v1 scripts from forums won't run unchanged in v2.
The clipboard built-ins in AutoHotkey v2
| Built-in | What it does |
|---|---|
A_Clipboard | The clipboard's contents as text. Assign to it to set the clipboard, assign "" to empty it. |
A_Clipboard := A_Clipboard | Converts whatever is on the clipboard (HTML, rich text, copied files) to plain text. |
ClipboardAll() | Saves every format on the clipboard. Assign the result back to A_Clipboard to restore it. |
ClipWait(Timeout) | Waits until the clipboard contains text (or files). Returns false if it times out. |
OnClipboardChange(Func) | Calls Func on every clipboard change. Its parameter is 0 for empty, 1 for text or files, 2 for other data such as images. |
To run a script, install AutoHotkey v2, save the code as a .ahk file, and double-click it. Its green "H" icon appears in the notification area; right-click it to reload or exit. Put several scripts in one file if you like, but keep only one #Requires line at the top.
Script 1: Paste as plain text with Ctrl+Shift+Alt+V
This converts the clipboard to plain text, pastes it, then puts the original formatted contents back, so nothing is lost.
#Requires AutoHotkey v2.0
^+!v:: {
saved := ClipboardAll() ; keep every format of the current clipboard
A_Clipboard := A_Clipboard ; reduce it to plain text
Send "^v" ; paste
Sleep 250 ; give the target app time to read the clipboard
A_Clipboard := saved ; restore the original
saved := "" ; free the memory
}
It works in any app that accepts Ctrl+V, including ones without a "paste as plain text" option. If the clipboard holds an image rather than text, the paste is empty and the image is restored afterwards. If a slow app pastes the formatted version, raise the Sleep value. How to paste without formatting on Windows lists the built-in alternatives.
Script 2: Append the selection to the clipboard
Normally each copy replaces the last one. This hotkey adds the selected text to the end of what's already on the clipboard, separated by a line break, so you can collect several passages and paste them once.
#Requires AutoHotkey v2.0
^+!c:: {
previous := A_Clipboard
A_Clipboard := "" ; empty it so ClipWait can detect the new copy
Send "^c"
if !ClipWait(1) { ; nothing arrived within one second
A_Clipboard := previous
return
}
if (previous != "")
A_Clipboard := previous "`r`n" A_Clipboard
}
Emptying the clipboard before sending Ctrl+C matters. Without it, ClipWait returns immediately because the old text is still there, and you'd append the previous clip to itself.
Ctrl+Shift+Alt+C was chosen to avoid common shortcuts such as Ctrl+Shift+C, which opens the element picker in Chrome and Edge DevTools. A hotkey in AutoHotkey overrides the same shortcut in every app, so pick a combination you don't use elsewhere.
Script 3: Remove tracking parameters from links as you copy
This runs on every copy. When the clipboard holds a single web address, it strips utm_ tracking parameters and trims stray spaces; anything else is left alone.
#Requires AutoHotkey v2.0
OnClipboardChange(CleanLink)
CleanLink(DataType) {
if (DataType != 1) ; text only
return
if DllCall("IsClipboardFormatAvailable", "UInt", 15) ; CF_HDROP: files copied in Explorer
return
text := Trim(A_Clipboard)
if !RegExMatch(text, "^https?://\S+quot;) ; only a single URL
return
clean := RegExReplace(text, "(?<=[?&])utm_[^&#]*&?", "")
clean := RegExReplace(clean, "[?&](?=#|$)", "")
if (clean !== A_Clipboard)
A_Clipboard := clean
}
Setting the clipboard from inside the callback triggers the callback again. That second run finds nothing left to clean, so the clean !== A_Clipboard check stops it from writing a second time. The check for format 15 (CF_HDROP) makes sure files copied in File Explorer are never turned into text.
Because the script replaces the clipboard with plain text, a copied link loses any rich formatting. Extend the second pattern to strip other parameters you see often, such as fbclid. How to copy a clean link without tracking parameters covers the manual and browser options.
Script 4: A clipboard history menu on Ctrl+Alt+H
This keeps your last 20 text copies in memory and shows them as a menu at the mouse pointer. Click an entry to paste it.
#Requires AutoHotkey v2.0
history := []
maxItems := 20
OnClipboardChange(SaveClip)
SaveClip(DataType) {
if (DataType != 1)
return
text := A_Clipboard
if (text = "")
return
for i, item in history { ; move a repeat to the top instead of duplicating it
if (item == text) {
history.RemoveAt(i)
break
}
}
history.InsertAt(1, text)
if (history.Length > maxItems)
history.Pop()
}
^!h:: {
if (history.Length = 0) {
ToolTip "Clipboard history is empty"
SetTimer () => ToolTip(), -1500
return
}
target := WinExist("A") ; remember the window to paste into
m := Menu()
for i, text in history {
label := SubStr(RegExReplace(text, "\s+", " "), 1, 60)
label := StrReplace(label, "&", "&&") ; show & literally in the menu
m.Add(i ". " label, PasteItem.Bind(text, target))
}
m.Show()
}
PasteItem(text, target, *) {
A_Clipboard := text
if WinExist(target)
WinActivate(target)
Send "^v"
}
The history lives only in memory, so it's gone when the script exits or the PC restarts. It holds text only: no images, no search and no pinning. That's where a dedicated manager earns its place; Ditto vs CopyQ compares the two most popular free ones.
Script 5: Log every text copy to a file
For research sessions where you need a record of what you copied and when, this appends each text copy to a dated log in your Documents folder.
#Requires AutoHotkey v2.0
OnClipboardChange(LogClip)
LogClip(DataType) {
if (DataType != 1)
return
stamp := FormatTime(, "yyyy-MM-dd HH:mm:ss")
FileAppend(stamp " | " A_Clipboard "`r`n", A_MyDocuments "\clipboard-log.txt", "UTF-8")
}
The log is a plain text file holding everything you copy, including any password or token you copy while the script runs. Exit the script before handling secrets, and delete the file when the session is over. How to pause clipboard capture when you're copying secrets has the wider checklist.
Common mistakes in clipboard scripts
- Not emptying the clipboard before a scripted copy. Set
A_Clipboard := ""beforeSend "^c", then callClipWaitwith a timeout. Otherwise the script may read the old contents. - Calling ClipWait without a timeout. With no timeout it waits forever if nothing is selected.
ClipWait(1)waits one second. - Callbacks that change the clipboard every time. Writing to
A_Clipboardinside anOnClipboardChangefunction triggers it again. Only write when the value actually changes, as Script 3 does. - Restoring the clipboard too soon. Some apps read the clipboard a moment after Ctrl+V. If pastes come out wrong, increase the
Sleepbefore restoring.
AutoHotkey or a clipboard manager?
Use AutoHotkey for transformations you do over and over: paste as plain text, clean links, append, reformat. A script does exactly what you write and nothing else.
Use a clipboard manager for retrieval: searching last week's copies, previewing images, pinning snippets you reuse. Ditto and CopyQ are free and keyboard-driven, and both keep history across restarts.
They can run together, but avoid having two programs rewrite the clipboard on every copy, or they may react to each other's changes. Can you run two clipboard tools at once? covers what conflicts and what doesn't. If you'd rather script the clipboard from a terminal, see the PowerShell clipboard cheat sheet; for a no-code route, Power Automate Desktop's clipboard actions handle similar cleanup in a visual flow.
FAQ
Do these scripts work with AutoHotkey v1?
No. They use v2 syntax such as A_Clipboard, ClipWait(1) and fat-arrow functions. The #Requires AutoHotkey v2.0 line makes AutoHotkey v1 stop with an error instead of running them.
How do I start a script automatically when Windows starts?
Put a shortcut to the .ahk file in your Startup folder. Press Win+R, type shell:startup, and press Enter to open it.
Can AutoHotkey save images from the clipboard?
ClipboardAll() can save and restore all clipboard formats, including images, to a variable or a file. Turning a clipboard image into a PNG file needs extra code or a library.
Related reading
- PowerShell Clipboard Cheat Sheet: Get-Clipboard and Set-Clipboard
- How to Remove Hidden Characters From Copied Text
- How to Clean Up Pasted Text With Find and Replace
- CopyQ Scripting: 5 Automations Worth Setting Up
Sources
- A_Clipboard (AutoHotkey v2 docs) — reading, setting, emptying and converting to plain text.
- ClipboardAll (AutoHotkey v2 docs) — saving and restoring the whole clipboard.
- ClipWait (AutoHotkey v2 docs) — timeout and return value.
- OnClipboardChange (AutoHotkey v2 docs) — callback data types and behaviour when the script changes the clipboard.
- Menu object (AutoHotkey v2 docs) — Add, Show, callbacks and ampersands.
- FormatTime and FileAppend (AutoHotkey v2 docs) — timestamp format and encoding option.
- Standard Clipboard Formats (Microsoft Learn) — CF_HDROP is format 15.
- Open Chrome DevTools — Ctrl+Shift+C opens the element picker.
Deepender Yadav created Edge Drop and builds and tests it, from the edge-hover shelf to each release. A B.Tech Computer Science Engineering student, Deepender writes the Edge Drop guides, comparisons and developer posts, and checks product details against the app's source code.
GitHub · LinkedInCopy. Stack. Drop.
Transform your clipboard into an interactive edge shelf. Stack, pin, and drag assets into any app with zero friction.
Download for Windows Get from Microsoft Store
How to Install Guide · First 10 Minutes Guide · Drag & Drop Guide · Edge Drop vs Win+V · Support
Free · Lightweight · Privacy First