Skip to content

Instantly share code, notes, and snippets.

@s3rvac
Created January 17, 2026 18:47
Show Gist options
  • Select an option

  • Save s3rvac/414c8b3e988738a8ec7a740316016de9 to your computer and use it in GitHub Desktop.

Select an option

Save s3rvac/414c8b3e988738a8ec7a740316016de9 to your computer and use it in GitHub Desktop.
CopyQ command for removing leading/trailing whitespace upon copying content
//
// A CopyQ (https://github.com/hluk/CopyQ) command for removing leading/trailing
// whitespace upon copying content. Works for both plain and Rich (HTML) text.
//
// Installation:
// - Open CopyQ, go to File -> Commands (or press F6).
// - Click "+ Add", select "New command". Then, select type "Automatic".
// - Copy the following code there and click on "Apply".
//
copyq:
// Trim plain text.
var text = str(clipboard(mimeText));
if (text) {
var trimmed = text.trim();
if (trimmed != text) {
copy(mimeText, trimmed)
}
}
// Trim HTML while preserving formatting.
var html = str(clipboard(mimeHtml));
if (html) {
var trimmed = html
.replace(/^(\s|&nbsp;|<br\s*\/?>)+/i, "")
.replace(/(\s|&nbsp;|<br\s*\/?>)+$/i, "");
if (trimmed != html) {
copy(mimeHtml, trimmed)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment