3

Unfortunately I need to revisit my question from five years ago:

How do I sanitize the copy/paste buffer?

There is still no answer to this.

I'm using CopyQ as a clipboard manager now, and it works pretty well for most things.

From the CopyQ FAQ:

https://copyq.readthedocs.io/en/latest/faq.html#how-to-store-only-plain-text

Unfortunately, this solution simply does not work. I create a document in LibreOffice, change the font and font size, copy from LibreOffice and paste into gmail and it keeps the same font and font size.

I never want this to happen. I want copy/paste to be plain text only at all times.

My workaround for the past five years has been to paste into vim, then copy again, which is obviously time consuming and frustrating.

What's the solution here? Something at the x.org level would be ideal, so it would be independent of clipboard manager (and would work without any clipboard manager).

I don't want any extra keyboard shortcuts either, just simple ctrl+c, ctrl+v as usual.

2
  • 1
    Does Ctrl+Shift+V work? If you do not want to use Shift I guess you should bind Ctrl+V shortcut to Ctrl+Shift+V but I don't know if binding keyboard shortcuts would be possible. Nov 18, 2022 at 2:48
  • Since Xorg doesn't really store the clipboard contents, it just marks the owner (Libreoffice) and the requester (Gmail) of the clipboard, Iyou cannot do that at Xorg level, instead it seems that clients should agree on the format (thus a clipboard manager could set a preferred format as plain text). Evidence example: The constructor of AP_UnixClipboard in Abiword indicates that there is an order of preference in the receiving format.
    – Quasímodo
    Nov 19, 2022 at 13:30

1 Answer 1

3

If only a few programs are affected, you could rebind the paste command. For example, LibreOffice has the option to paste as plain text. Other programs may have similar options.

I did not find any clipboard manager with the ability to convert to plain text, though I vaguely recall some having the ability in the past. Some clipboard managers have the ability to run actions, but I could not figure out how to make them run automatically.

So you're left with trying to do it yourself. In most cases, the following command will convert the clipboard contents to plain text. The major exception is image data.

xclip -o -selection "clipboard" | xclip -i -selection "clipboard"

So you could run a script that checks the clipboard for changes and sanitizes the contents automatically. If you are using a clipboard manager, this will create duplicate entries. The formatted version and the non-formatted version.

#!/usr/bin/env bash

while true ; do
  clipsum_new=$(xclip -o -selection "clipboard" 2> /dev/null | md5sum --)

  if [[ $clipsum_new != $clipsum_old ]] ; then
    echo "Clipboard contents changed."

    clipboard=$(xclip -o -sel "clipboard" 2> /dev/null)
    if [[ $clipboard == "" ]] ; then
      echo "Clipboard empty or contains non-text data."
    elif [[ $clipboard =~ ^/.* ]] ; then
      echo "Clipboard may contain file paths."
    else
      echo "Sanitizing clipboard"
      xclip -o -selection "clipboard" | xclip -i -selection "clipboard"
    fi

    # save new checksum for future comparison
    clipsum_old=$(xclip -o -selection "clipboard" 2> /dev/null | md5sum --)
  fi

  # limit polling rate
  sleep 1
done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .