Developer refines jq clipboard-formatting alias for Linux with error handling
A blog post highlights a handy jq trick from Sequoia McDowell that pretty-prints JSON copied to the clipboard and pastes it back automatically, using pbpaste and pbcopy on macOS. The author adapts this for Linux using xclip, then identifies a flaw: if the clipboard doesn't contain valid JSON, jq fails silently and wipes the clipboard content. The fix adds a fallback so the original clipboard text is preserved when jq errors out.
GoKawiil's interpretation of the reporting above, not reported fact.
Small command-line utilities like this illustrate how developers iteratively harden simple scripts against edge cases that could silently destroy data. The fix demonstrates good shell scripting practice—checking exit codes and providing fallbacks—which is broadly applicable beyond this specific JSON-formatting use case.
- jq can be combined with clipboard tools to instantly reformat JSON.
- The original alias failed silently and erased clipboard contents on non-JSON input.
- Adding a fallback command preserves the clipboard's original text when jq fails.
O'Reilly "Classic Shell Scripting" Book — If you enjoy stringing together pipes, aliases, and error handling like the jq clipboard trick in this article, this book digs into the pipeline patterns and exit-code quirks that make those one-liners robust. It's a solid reference for anyone who lives in bash and wants their aliases to fail gracefully.
See O'Reilly "Classic Shell Scripting" Book on Amazon → Affiliate link — we may earn a commission on purchases, at no extra cost to you. Product picked by AI based on this article; it is not a tested recommendation.Source: chris48s.github.io, 2026-09-02
Published there as: “Using jq to format JSON on the clipboard”
Read the original report → The summary and analysis above are GoKawiil's own, written from reporting by the source above. Facts and quotes belong to the original publisher.