Ubuntu – Processing clipboard text through a script in between copy and paste

clipboardcommand linexclip

I want to intercept the paste event and run any text through my script. The use case is like this (assume I have a script somewhere which accepts text on stdin and turns 'world' into 'potato', writing on stdout).

  1. Highlight some text 'hello world!' with the cursor
  2. Middle-mouse click to paste from the selection buffer, and 'hello potato!' appears

There should be no manual step between 1 and 2, i.e. I want a hook on the paste event (or perhaps on the copy event) so that the text is processed automatically. I don't have anything malicious in mind; it's just about auto-formatting some source code when copying/pasting from/to the web.

My question concerns the selection buffer (middle-mouse clipboard), but I guess the question applies to other clipboard text in general too.

Best Answer

You can use clipnotify to get notified of clipboard changes without polling. From the project page:

while clipnotify; do
    [an event happened, do something with the selection]
done

The tool is only a few lines of C code that uses the XFIXES functionality.