Replace the current clipboard selection with a modified version of the original selection

I read the present topics on AskUbuntu, but none of them did fit exactly.

Situation is:
I want to replace the currently selected text (by calling a bash script via EasyStroke) with a modified version of this text. The text is modified by RegEx'es or so... it's part of the script, so it doesn't really matter.

Current state:
I can read the selection from the ClipBoard with xclip, modify it and copy it back to the ClipBoard with the following command (part of the called bash script):

xclip -o | sed 's/ab/ef/g' | xclip -sel clip

The test string is

cdabcdabcdabcdab

And after applying the above command, the output (in the ClipBoard) is

cdefcdefcdefcdef

which is correct. But the result is still stored in the ClipBoard and not pasted back to the application. I have to paste it to the currently running application with Ctrl+V manually.

Question is:
How can I automate the last step of pressing Ctrl+V to immediately paste the result to the application in which the source text has been selected?

I tried xclip and xsel, but I was unable to make them work the way described above.

Any suggestions?

1

1 Answer

Thanks to @meuh's comment above, I was able to complete this task:

  1. Install these programs:

    sudo apt-get -y install xclip xdotool libnotify-bin
  2. Create a .bash script for the transformation:

    #!/bin/bash
    # Script to replace selection with a 'sed' transformed output (callable with easystroke or similar)
    IN=`xclip -o`
    # Example: Replace all 'ab' by 'ef'
    RESULT=`echo "$IN" | sed -e 's/ab/ef/g`
    # Output result to clipboard
    xdotool type "$RESULT"
    # Transform result to HTML for output with notify-send
    RESULT_HTML=`echo "$RESULT" | sed 's/&/\&amp;/g; s/</\&lt;/g; s/>/\&gt;/g'`
    # Create a notification in the lower right corner with the transformation
    notify-send "Changed clipboard text" "Input: $IN\nOutput: $RESULT_HTML"
  3. Call this script from easystroke or a similar program that processes key- or mouse-strokes.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like