17

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

#!/bin/bash

# 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

SelectedText="$(xsel)"

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.

SidMan
  • 401

1 Answers1

13

Credit goes to Kenn.

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

Source.

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.

#!/bin/bash

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; do SelectedText="$(xsel)" CopiedText="$(xsel -b)" if [[ $SelectedText != "file:///" ]]; then ModifiedTextPrimary="$(echo "$SelectedText" | tr -s '\n' ' ')" echo -n "$ModifiedTextPrimary" | xsel -i fi if [[ $CopiedText != "file:///" ]]; then ModifiedTextClipboard="$(echo "$CopiedText" | tr -s '\n' ' ' )" echo -n "$ModifiedTextClipboard" | xsel -bi fi done

Dependencies

  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
    

To USE

  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 :
    .\copy_without_linebreaks.sh
  5. Copy text in pdf and paste it anywhere. Lines breaks will be removed.
Pablo Bianchi
  • 15,657
SidMan
  • 401
  • Please click grey check mark next to your answer in two days time. – WinEunuuchs2Unix Aug 20 '19 at 17:22
  • Does clipnotify support individual selection events? i.e. "primary", "secondary", "clipboard" or "buffer-cut". I commented a line in clipnotify.c to get events for only clipboard selection. i.e. https://i.imgur.com/FcY3REn.png – Akhil Jun 23 '21 at 17:43
  • Unfortunately, this does not help for detecting paste events (if there's even such an event type). I want to update the clipboard when a value has been pasted, so one can easily paste a sequence of values one at a time. – Max Spring Sep 01 '23 at 23:44