From 2054f5b3cf2d3136b3704ddf01a6861eaa7e5ebf Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Sat, 10 Jan 2026 19:08:43 -0500 Subject: [PATCH] Use clip.exe for setting/clearing clipboard on Windows to avoid Powershell startup latency --- clip/copy_WIN.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/clip/copy_WIN.go b/clip/copy_WIN.go index eb46388..2e67658 100644 --- a/clip/copy_WIN.go +++ b/clip/copy_WIN.go @@ -4,14 +4,15 @@ package clip import ( "errors" - "fmt" "os/exec" - "strings" + + "github.com/rwinkhart/go-boilerplate/back" ) // CopyString copies a string to the clipboard. func CopyString(clearClipboardAutomatically bool, copySubject string) error { - cmd := exec.Command("powershell.exe", "-c", fmt.Sprintf("echo '%s' | Set-Clipboard", strings.ReplaceAll(copySubject, "'", "''"))) + cmd := exec.Command("clip.exe") + _ = back.WriteToStdin(cmd, copySubject) if err := cmd.Run(); err != nil { return errors.New("unable to copy to clipboard: " + err.Error()) } @@ -23,5 +24,7 @@ func CopyString(clearClipboardAutomatically bool, copySubject string) error { // getClipCommands returns the commands for pasting and clearing the clipboard contents. func getClipCommands() (*exec.Cmd, *exec.Cmd, error) { - return exec.Command("powershell.exe", "-c", "Get-Clipboard"), exec.Command("powershell.exe", "-c", "Set-Clipboard"), nil + clearCMD := exec.Command("clip.exe") + _ = back.WriteToStdin(clearCMD, "") + return exec.Command("powershell.exe", "-c", "Get-Clipboard"), clearCMD, nil }