Ubuntu – Detect clipboard copy/paste event and modify clipboard contents


After something is copied to clipboard (using ctrl+c) I want a script (bash, python or any other language) to automatically detect that new entry is added to clipboard, change it's content and put it back to clipboard so when I paste it I get the modified text. The script should constantly run in background and monitor the clipboard for changes.

The following script describes the modification that is needed :
Source : https://superuser.com/questions/796292/is-there-an-efficient-way-to-copy-text-from-a-pdf-without-the-line-breaks


# title: copy_without_linebreaks
# author: Glutanimate (github.com/glutanimate)
# license: MIT license

# Parses currently selected text and removes 
# newlines that aren't preceded by a full stop


ModifiedText="$(echo "$SelectedText" | \
    sed 's/\.$/.|/g' | sed 's/^\s*$/|/g' | tr '\n' ' ' | tr '|' '\n')"

#   - first sed command: replace end-of-line full stops with '|' delimiter and keep original periods.
#   - second sed command: replace empty lines with same delimiter (e.g.
#     to separate text headings from text)
#   - subsequent tr commands: remove existing newlines; replace delimiter with
#     newlines
# This is less than elegant but it works.

echo "$ModifiedText" | xsel -bi

I do not want to use shortcut key binding to run the script.

Best Answer

Credit goes to Kenn.

I modified the script to my requirements and added the function to detect clipboard copy event and modify its contents.


Remove Line Breaks when copying text from PDF (Linux)

This bash script removes line breaks when copying text from PDF. It works for both Primary Selection and Clipboard of Linux.


# title: copy_without_linebreaks
# author: Glutanimate (github.com/glutanimate)
# modifier: Siddharth (github.com/SidMan2001)
# license: MIT license

# Parses currently selected text and removes 
# newlines

while ./clipnotify;
  CopiedText="$(xsel -b)"
  if [[ $SelectedText != *"file:///"* ]]; then
    ModifiedTextPrimary="$(echo "$SelectedText" | tr -s '\n' ' ')"
    echo -n "$ModifiedTextPrimary" | xsel -i
  if [[ $CopiedText != *"file:///"* ]]; then
    ModifiedTextClipboard="$(echo "$CopiedText" | tr -s '\n' ' '  )"
    echo -n "$ModifiedTextClipboard" | xsel -bi


  1. xsel: sudo apt install xsel
  2. clipnotify. You can use the pre-compiled clipnotify provided in the repository or compile yourself.
    To compile clipnotify yourself
    sudo apt install git build-essential libx11-dev libxtst-dev
    git clone https://github.com/cdown/clipnotify.git
    cd clipnotify
    sudo make


  1. Download this repository as zip or copy and paste the script in a text editor and save it as copy_without_linebreaks.sh.
  2. Make sure that script and clipnotify (downloaded or precompiled) are in the same folder.
  3. Open terminal in script's folder and set permission
    chmod +x "copy_without_linebreaks.sh"
  4. Double-click the script or run by entering in terminal :
  5. Copy text in pdf and paste it anywhere. Lines breaks will be removed.